C# Pass Values from Modeless Form - c#

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.

Related

working with different forms within mdi application

I have an application using C# wherein I have a form which is MdiContainer form named parentmdiform in which all my child form gets open.
From submenu of this parentmdiform a form named studentmasterform gets open.from this form on a click of a button a new form named existingstudent form gets open.
When I want go back to studentmasterform again from Existingstudentform, a click of a button on which studentmasterform gets open.But the problem is my earlier opened studentmasterform from parent form does not get focused or disposed off.That means I get two separate object of same form that is Studentmasterform which exists in my parentmdiform.
What I want is ,that same form object should get either focused or disposed of when i click on any other forms within my MDI application to access earlier form.
Can anyone tell me how?
You need to track the creation of studentmasterform. And if it is there, created, don't create new one, do Keyboard.Focus(oldOne);
P.S. There is studentmasterform.Closed event to help you keep the track.
I hope I understand what are you want to do.
First, if you want that a Form closes if it loses focus, take a look at the "Deactivate" event of the Form class. [MSDN Deactive Event]: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.deactivate.aspx
When you need a Form as only once opened, you can check "parentmdiform.MdiChildren" to get all the children of your MDI From. You can give your Forms an unique name, so it's easier to focus the right.
regards, C#er

What is the best way to invoke action in Form1 When Form2 is closed without using ShowDialogue in C# Windows Form Application?

I've a project that contain 2 forms e.g. (Form1 , Form2 ).The user should be able to use any of the 2 forms while both of them are visible(i.e. Can use Form1 While Form2 is Open).At the same time when Form2 is closed there is an action that should be invoked in Form1 related to From2_close. I can't use Form2.ShowDialogue as it trap focus only to Form2 but user should be able to edit some data in Form1 while Form2 is loaded. Hope the question is clear, please help. Thanks
Your question is not entirely clear, but I think you are trying to send a notification to a given set of forms based on any other forms actions. This type of notification scenerio can be very easily handled by implementing the Observer Pattern. Basically you have a single object that is the "observer" and it watches subscribers that register with it for messages. When the observer receives a message from one of its subscribers (ie a form) it then notifies the other subscribers that it received a message.
Hope this helps.
Handle either the FormClosed or VisibileChanged event. When one of those events fires you know the form has been closed or hidden and can react accordingly.
ShowDialog creates a modal dialog, which means that no other code in the calling thread will execute until the form closes. In this case, the dialog is blocking the GUI thread on your other form, which means that the user can't use it.
Instead of using ShowDialog to create an instance of Form1 from Form2 (or vice-versa), as I'm guessing you are currently doing it, just instantiate the other form.
Form1's Load handler:
Form2 form2 = new Form2();
form2.Show();
*EDIT
To allow each form to be closed without the other closing or the application exiting, do this:
In Program.cs, you'll see a call to Application.Run that creates a new instance of Form1 and starts the application. Replace that with the code from above, repeated twice, to create Form1 and Form2. After those two calls, add in a plain Application.Run() call.
Then, in each form, handle the FormClosing event by instead exiting the application if both forms are closed.

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

Appropriate Method of Retrieving Data from a Form

New to C# and .NET.
In a windows application I place a form to query a user for some input. The user fills out the form and presses 'ok'.
What is the "correct" or "appropriate" method of retrieving that data from the form? Do forms have a return statement? Do I send a message?
Thanks in advance.
you can have the parent form subscribe to events from the child form and still do the same thing or put the form results into an event arg statement
you should listen to the click event of the button and you will get a call back and you can simply read the properties from the form objects (txtBox.Text, etc . . )
After they press OK, the form will close, but (as long as the form variable is still in scope), its members will still contain all of the data. Save the input to a member variable and access it from your main program using an accessor. If the form is not modal, your main program can subscribe to the form closing event and get the information then.

Categories