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.
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.
I am stuck on understanding how after starting the message loop with Application.Run(Form) i can receive back data from the internal app.
My question is, How can I retrieve messages, objects, strings, etc? Basically I want that my internal application to send back an object to the main winform to be able to utilize the main form code to execute a USB print. Is this possible? and if it is can someone help me with a very simple example of string printText = "hello there"; and grab that onto the main winform for processing?
Sorry I know it is not the prettiest of questions, but I should be able to start from there.
My winform application is executing some legacy code of windows CE, and the data I am processing in it I need to be able to print to a USB printer.
I assume that when you say internal app, you mean child form. so best thing to do is to create event on child form, this event will be handled by the main form. so whenever you need some data in main form then your child form can pass this data in event argument. now your main form will handle this event and to whatever with this data.
In main form
var childForm=new ChildForm();
childForm.DataGenerated+=DataHandler;
childForm.Show();
private void DataHandler(object data)
{
//do something with data
}
I need some help on a thing that I can't resolve.
I've a Windows Form with a DataGridView who is populated with a large amount of data.
This data is stored in a SQL Server Database and retrieved by a simple piece of ADO.NET code.
I'm already using the BackGroundWorker class to perform this kind of operation but the form still freezes for 1-2 second.
Is there a way to delay the showing of the form? Like a show form only when all data in loaded? I've tried to make it not visible or to use Hide() and Show() method but still not have any results.
I've found a easy work-around.
Change the modifiers of the BackGroundWorker to public.
Create the instance of the form, run Form.CreateControl() and call the BackGroundWorker.RunWorkerAsync() method.
In the DoWork event I put: data retrieving and population of the controls (DataGridView, combobox and textbox).
In the RunWorkerCompleted just the Form.Show() method.
That's all. Is this a nice solution?
Remove InitializeComponent(); from constructor of your form and call this method when data has loaded
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.
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.