Appropriate Method of Retrieving Data from a Form - c#

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.

Related

WinForms - Keep child form open in case user cancels closing parent form

Background:
I am creating a WinForms app using C# 4.0 and VS2013. My app has a main form named ParentForm and a secondary form named ChildForm. The application follows an Model-View-Presenter design, so I want my presenter class P to control all the form opening, closing, button clicks, and generally anything the user requests. (Both forms are actually abstracted behind "view" interfaces, but the problem is specifically with the WinForms implementation, so let's ignore the interfaces.)
Problem:
Currently P is handling FormClosing events for both ParentForm and ChildForm, and calling Form.Close or Form.Dispose if it determines that the user is allowed to close these forms.
The problem I'm having is if ChildForm is open and the user clicks the 'X' button on ParentForm. In this scenario, FormClosing is called first on ChildForm, then on ParentForm. P doesn't have any way of knowing whether the user clicked 'X' on ChildForm (which P should react to by closing ChildForm), or the user clicked 'X' on ParentForm (which P should react to by confirming if the user really wants to exit, before closing any forms).
Questions:
Is there a way to tell if FormClosing is being called in reaction to a parent form closing?
Is there any way to treat the 'X' button like a normal button?
Is there a way to prevent child form closes until after the parent form's FormClosing event (and possible user cancellation)?
You can use FormClosingEventArgs.CloseReason property, which according to MSDN
Gets a value that indicates why the form is being closed.
See the CloseReason Enumeration values, in particular FormOwnerClosing and MdiFormClosing.

C# Pass Values from Modeless Form

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.

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

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;

C# Callback problems

i have a prioblem with a callback because my Form1 open Form2 and send data to Form2 after this return information to another form...please help
i can send object from form1 to form2, but the result of the form2 method must be returned by callback to another form(example form 3).
i hope that you understood my question..
The fact that you have multiple forms operating on the same data means that a better option is to encapsulate that data in a set of "model" classes that can handle both handing out information to your forms and persisting any changes to storage as necessary.
The advantage of this is when you have multiple forms that need to deal with the same data, you can publish callbacks on the model objects for change notification. Each form subscribes to the events in the model that it cares about and it means any number of forms can manipulate your model and all the forms can maintain current state by reacting to notifications.
When does this way you don't care about which forms are manipulating the data and you don't need to pass anything more than the model class when launching a new form. Likewise, when a form requests a save, all the forms can update the state so they don't show the pending change.
When passing data between multiple forms, its often useful to store a refernceto the other forms as private variables within forms, populated only by the constructor of the form.
However, be aware that this can give you memory problems, partuicularly with events still being wired up to forms held in memory on other forms.
You could probably have your Form3 listen on the FormClosed event from Form2, then have some code to ask for the return data from Form2. Alternatively, you could create and event, FormClosedWithReturnValue(object sender, SomeArgsThatContainsReturnData data) in Form2 and have Form3 listen on that event. Hope that helps.

Categories