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;
Related
I am currently in the process of changing my modal form to modeless. The modal form was at the beginning of a method and I utilized the values within the form in this method. Now that I'm changing it to modeless, I have it set to open the form, then in the closed event I call that method. But my problem is, how do I retrieve the values from the form? Since it's closed, I can't use the simple form.Value like I was when the dialog was modal.
Thanks so much!
Subscribe to the 'FormClosing' event and save the data from the form.
I'm fairly certain the 'FormClosing' is the event you need because
The form is not yet disposed when you reach this event.
To cut a long story short...
I need Form_1 to open Form_2.
I need Form_2 to execute code, AFTER Form_1 has been hidden.
(without user interation)
At the moment, my Form_1 code is thus....
function myFunction(){
// Create a new instance of the child form class
Form2 F2 = new Form2(this,d);
// Minimise current form
this.Hide();
// Show the child form
F2.Show();
}
F2.Show then initiates further code from Form2_Shown event.
Problem is that I need the Form2_Shown code to run after Form1 has completely hidden.
Because Form1.myFunction calls F2.Show(), then Form2_Shown will allways occur whilst Form1 is not 'retired' completely.
Is there a way to seperate this chain of events to get the desired outcome?
Open Form_2
Close Form_1
Run code in Form_2 with no remaining connections to Form_1 ?
Threads ?? (he says in ignroance of them)
And before you ask ... this is for a screen capture application.
Form_1 is the UI with buttons.
Form_2 is a fullscreen transparent form.
Because Form_1 is hanging in there, it causes black images.
If I use the same model, but trigger the screen capture off of a keypress in Form_2, then everything works perfectly.
Hope that makes sense.
I can of course post reams of code, if required.
Many Thanks.
You could pass in a reference to Form1 in Form2's constructor. Then from the constructor of Form2 call the Form1.Hide() method. Then, from Form2's constructor call the Show() method on Form2.
i got application, it shows 3 forms: log window, status window, and option window, option window calls some other forms and some of these forms are required to be called using ShowDialog() to return dialog result value for further decision making.
Using ShowDialog() raises problem, cause form called by that method excluding other forms from being accessible.
So my problem is i would like to be able to make atleast log window accessible no matter how much other forms has been called. Is there a way to make log window to independent form other forms, or could it be taken over by form called as last?
EDIT:
I failed to mention that the behaviour provided by ShowDialog() is quite usefull in my app and that i only lack ability to free that one or two forms from being locked. Switching to Show() is not option I'm considering as best while other forms, that are parent to form called by ShowDialog() are required to be still locked.
You'll have to make a choice between the two.
Use ShowDialog(), so that your parent form pauses execution, and only resumes when the second form is closed, or
Use Show(), so that your parent form continues execution after displaying the second form.
If you want to take some action, or read values from the second form when it's closed, then subscribe to its Closed event before you show it.
Let's assume your second form has a "First Name" TextBox, and a property to return that value:
public string FirstName
{
get { return yourFirstNameTextBox.Text; }
}
In your first form, you can subscribe to an event to take some action when the event occurs, like this:
var f2 = new SecondForm();
f2.Closed += (s, e) => MessageBox.Show(f2.FirstName);
f2.Show();
Now the user can continue on their way with both forms, and when the second form is closed, a message box will display the value of the "First Name" TextBox.
You'll probably want to do something more meaningful than this. Instead of displaying a message box, you could update a field on the first form, or take some any other action or set of actions that you want.
f2.Closed += (s, e) =>
{
MessageBox.Show(f2.FirstName);
nameLabel.Text = f2.FirstName;
// another action
// yet another action
};
So I now modeled your situation and found solution:
Use Show() instead ShowDialog() for dialog.
Write some code in the dialog to close it window after press the button (Ok, Cancel etc.). Because auto-closing works with ShowDialog only. But you don't need to set DialogResult manually.
At the Options form subscribe to FormClosed event to get DialogResult. For example:
dlg.FormClosed += (o, a) => { this.Text = dlg.DialogResult.ToString(); };
Handle Activate event of Options form to prevent focusing on parent window when dialog is opened:
if (dlg != null) dlg.Focus();
This solution has some difference with system behaviour of ShowDialog, but it works good.
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.
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();