get focus on first form - c#

I have two forms form1 and form2. I navigate to form2 using a button in form1. In form2 I have a button control; on button click, I show messageBox. when messageBox comes then it also lost focus of form1 but I want that it should not lost focus of form1. I have no concern with form2.

There's no way a message box doesn't show like a dialog. Like Eliran Pe'er said, you should make a Form like a messagebox with a label and a button and use it like this.
MessageForm form = new MessageForm.Show();
If you use ShowDialog it's going to be the same thing as MessageBox.
In your form 1 you can use TopMost property = true in order to keep it in front all the time no matter what. But this is going to keep your form on top of all other open programs.
Another workaround would be after messagebox is closed by the user (this is not a bad option) you can call form 1 to BringToFront(). To do this, you can pass the instance of form1 to the form2 in the Show method. Use that parameter in your form2 constructor.

I don't think there's an easy way to prevent MessageBox from taking focus, and thats because a MessageBox is a dialog. (dialogs take focus from the program until they being closed)
The only way I can think of is creating new form that looks like a MessageBox, and using it instead.

try this
if(MessageBox.Show("something")==DialogResult.OK)
{
form1.Focus();
}
Or
if(MessageBox.Show("something")==DialogResult.OK)
{
form1.Select();
}

Are you using ShowDialog() method or Show() method to show your form2?
If you are using ShowDialog() method, modify it as Show().
Because ShowDialog() method will not allow you to change the focus to main form (form1), until you close the sub form (form2)
Make sure you are using method,
form2.Show()
to Display form2.

Related

How to prevent clicks outside the current form?

I want the form which will open when the user click a button, I don't want the user to click anywhere else outside this form(preventing him from clicking any other buttons on the parent form), I want to restrict his actions inside this form until he close it (For sure this form is top most), and I want the form to make alert(flicker) to any clicks outside it so the user will understand that he have to close it first.
When opening the form instead of
Form form = new Form();
form.Show();
use
Form form = new Form();
form.ShowDialog()
Super easy fix!
I assume your code to show the form right now is
form.Show();
To create/show an instance of the form that is modal (restricts clicking to that form) all you need to change is that line to:
form.ShowDialog();
Liam

C# How to pause Form1 while Form2 is active?

As the title says; that's all I need. I have 2 forms : Form1 and Form2. At some point of my code I want to do Form2.ShowDialog() and after Form2 is closed I want to resume Form1. How can i do this?
I tried using Thread.Sleep(sometime) but this will just disable any controls,timers, etc from Form1 and resume after the period passed. The problem is that I cant know how much time it will take for my user to press something in Form2.
As per KDecker mentioned in a comment to your question:
If you want to show something Modally (that is the form behind is unusable) use the ShowDialog() method. This will make it so you can only use the form that ShowDialog() was called on.
If you want to show it Modeless, then just use the Show() method on Form. This will make it so you can use both forms
See the MSDN documentation for reference:
https://msdn.microsoft.com/en-us/library/aa984358(v=vs.71).aspx

Focus textbox on main form after closing a second form

How do I focus a TextBox element on the main form after closing a second form which is opened by calling ShowDialog()?
I have tried to call MainForm.TextBox.Focus() in the closing and closed event of the second form, but this won't focus the textbox.
I am using System.Windows.Forms on the compact framework.
Thanks.
From your second form, make a button (or another control) return a DialogResult by going into Properties. When you want the second form to close (ie after you press a button) make it return a specific DialogResult. In your main form you can do this:
if(secondform.ShowDialog() == DialogResult.OK)
{
textBox.Focus();
...
}
Calling ShowDialog() will block until it is closed so you could simply do:
secondform.ShowDialog();
textbox.Focus()
However the first example is for when you only want to make the textbox have focus after you press a certain button or do an action on the second form.
ShowDialog means, it's a modal window and the focus won't go back to main form until you close second form. You can set focus back in the same code which you used to open the second form.
SecondFrm.ShowDialog();
Textbox.Focus();
ShowDialog() will only return when the second form is closed, so you can write MyTextBox.Select() right after the call.
SomeForm form1 = new SomeForm();
form1.ShowDialog();
here you're showing the new form.
When you close it, you will execute methods after that, so add
yourTextbox.Focus();
so, its:
SomeForm form1 = new SomeForm();
form1.ShowDialog(); // do what you want in your form, then close it
yourTextbox.Focus();

Showing a form via Form.Show is not given focus?

I two forms, Form1, and a UserControl which hosts Form2. Within that UserControl on Form1 I call Form2.Show();. I have also tried Form2.Show(this);. Either way, the focus is not given to the form. I have to click once within the new form to give it focus, and then I can click items within that form.
I figured that control is passing back to my main control/form, and thus the focus is getting lost. So I am waiting until Form2 is closed via:
while (form2.Visible == true)
{
System.Threading.Thread.Sleep(100);
Application.DoEvents();
}
This seems to work. However after I close the form, now the reverse holds true. Form1 is not given focus (even if I call this.Focus()) until I click once within the main form window.
Any ideas how to handle this properly. I want to show a child form (modeless) and immediatley be able to click on it, and when that form is closed, immediately be able to take action back on the parent form.
You should probably use .ShowDialog(), this can also be extended to give your response on if the user performed Form2's operation properly or aborted early.
This does make the form locked in focus up front and will halt your code execution on the first form till that form is closed.
use this.Activate(); in place of this.Focus();
Not sure I follow completely, but from your UC try opening Form2 like this:
form2.Show(Parent);
This should specify the UC's Parent form as form2's owner.
This came from the fact that I was overriding WndProc in order to display the form. When I received the CBN_DROPDOWN message, I would display the form. I fixed this by instead Invoke'ing the method that shows the form and it fixed it.
case CBN_DROPDOWN:
Invoke(new MethodInvoker(Show_DropDown));
return;

csharp winform modal window, able to click on main window

language c#, winform
how do i create a modal window or such a thing that when it still showing
i would still be able to click or interact with the main window..
thanks.
put some code please or links..
Make the dialog non-modal (use Show instead of ShowDialog), and make it top-most (TopMost = true)
Just use the overload of Form.Show() that takes a form as a parameter, like this:
Form f = new Form();
f.Show(this);
This will keep the form always on top of the form that calls it, but still let you click and access the calling form.
Some confusion here I think;
Modal is when the window blocks the underlying window, and must be closed to enable the underlying window to regain control. Form.ShowDialog(owner) is used to accomplish this.
Non-Modal is a window that is opened "in parallell" to the underlying window. Both windows can be accessed and respond to mouse and key events. Form.Show(owner) to accomplish this.
Modality by definition means that you are not able to click anywhere else. You can create another form and show it with Show() method.
Show() Method allows you to click anywhere while ShowDialog() won't

Categories