Interaction between user controls and win forms

  • Thread starter Thread starter Anita C
  • Start date Start date
A

Anita C

Hi,
I have a 'UserControl1', which has comboboxes, texboxes and buttons. In the
click event for the 'Ok' button on 'UserControl1', I have code to do some
processing with the data collected from the textboxes etc. At the end of all
the processing I want to be able to close the form hosting UserControl1 and
then bring up Form2 which hosts the 'UserControl2' (which is basically a
dialog notifying completion of the task). How do I achieve the above?

Thanks in advance
 
Hi Anita.

You can define some kind of a global data type where you can store the information that you want to display in Form2. In Form1's 'OK' button event handler, you can do 2 things: (1) Update the global data type based on the data that you collect from Form1 controls (textboxes, etc...); (2) call Close() method on it-self

After your form closes, you can begin another message loop with Form2 (Application.Run(Form2)). Inside Form2's constructor you can read all the data the you have previously stored from the global data type, and then display that information on Form2.

Hope this helps.

Thanks,
Igor Zinkovsky
Visual Studio Build/Release Team

-----------------------------------------------------------------------------------------------------------------------------------------
This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm
-----------------------------------------------------------------------------------------------------------------------------------------

----- Anita C wrote: -----

Hi,
I have a 'UserControl1', which has comboboxes, texboxes and buttons. In the
click event for the 'Ok' button on 'UserControl1', I have code to do some
processing with the data collected from the textboxes etc. At the end of all
the processing I want to be able to close the form hosting UserControl1 and
then bring up Form2 which hosts the 'UserControl2' (which is basically a
dialog notifying completion of the task). How do I achieve the above?

Thanks in advance
 
Anita C said:
Hi,
I have a 'UserControl1', which has comboboxes, texboxes and buttons. In the
click event for the 'Ok' button on 'UserControl1', I have code to do some
processing with the data collected from the textboxes etc. At the end of all
the processing I want to be able to close the form hosting UserControl1 and
then bring up Form2 which hosts the 'UserControl2' (which is basically a
dialog notifying completion of the task). How do I achieve the above?

Thanks in advance

Hi Anita,

The easiest way to communicate "up" the object hierarchy is to use
events. Fire an event from your UserControl1 indicating the
processing is done, and either:
a) handle the event in Form1 by hiding Form1 and showing Form2, or
b) let the event bubble further up the object hierarchy if you have a
form or object that actually contains Form1 and close Form1 / show
Form2 from there.

Here is an MSDN reference:
http://msdn.microsoft.com/library/d...y/en-us/cpguide/html/cpconeventsdelegates.asp

HTH,
Darren
 
Back
Top