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();
Related
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
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.
I have two forms. I show my second form with this code:
Form2 f = new Form2();
f.ShowDialog();
I need to understand when focus returns to main form. I tried Activate event but that's not it.
The call to ShowDialog() is blocking the parent form.
When your code exit from ShowDialog() your current form become active again,
For example:
using(Form2 f = new Form2())
{
// At the moment of the call of ShowDialog, the Net Framework start its work to
// pass the focus to the first control in the Form2 instance
if(DialogResult.OK == f.ShowDialog())
{
// Form confirmed, do your stuff
}
}
// At the end of the using block the parent form is again the active form with the focus on
// the button that has started this process (or the last control that had focus)
When you type
Form2 f = new Form2();
f.ShowDialog();
it waits until you manually close the Form2 instance and then returns to the main form. This happens if you use ShowDialog() method.
In some cases, where you just want to pop-up another form for a fraction of a second and return to the main form you should use formInstance.Show() method. The Show() method does not wait for the second form to be closed, it executes immediately and passes the control to the next statement after the Show() statement.
Hope you understood it.
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;
In my program I will press a button, and it will load form2. I do not want the program in form1 to continue running until the user closes form2. Then program execution continues in form1 right after the line that loads form2.
i think you want to show a modalled dialog.
public class MyForm1 : Form
{
public void ShowDialog2()
{
MyForm2 form2 = new MyForm2();
form2.ShowDialog(this);
}
}
You can use
Form.ShowDialog Method (IWin32Window)
You call the .ShowDialog function on the form you want to show.
.Show simply shows the new form. Calling .ShowDialog causes the calling form to block until execution returns from the shown form.
The result returned from the call to .ShowDialog will tell you if the user hit 'Okay' or 'No' or cancelled the form by clicking the x in the corner.