473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Passing a usercontrol as a form parameter

Hi,

I currently have a control that is on a form [FormA] and I want to pass that
exact instance of the control to another form [FormB] (a child form that
appears on a button click). The control has state, etc. that works with
both forms.

In my button click on FormA, I take my control and pass it to a constructor
in FormB. Here is the code in the constructor:

public FormB(MyControl ctrl)
{
InitializeComponent();
_passedInControl = ctrl; /* used later to set the parent property
back to the original */
_oldParent = ctrl.Parent; /* used later to set the parent property
back to the original */
ctrl.Parent = this;
this.Controls.Add(ctrl);
}

I also have in the Form Closing event:

private void FormB_Closing(object sender, CancelEventArgs e)
{
if (this._passedInControl != null)
_passedInControl.Parent = _oldParent;
}

Here is my problem: my control doesn't appear on FormB. It disappears from
FormA (which should happen because the parent is changing) and it also
reappears on FormA after I close FormB. What do I need to do to get it to
appear on FormB?

Thanks in advance,
cj

Nov 15 '05 #1
2 2686
Curtis,

You shouldn't be passing the control to another form. Rather, the
control should use a memento pattern to store its state into an object that
can be passed around easily. Basically, the control should expose an object
that exposes the state and then you can pass that to your new form.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Curtis Justus" <se*****@nospam.ser.it.is.com> wrote in message
news:e%***************@TK2MSFTNGP11.phx.gbl...
Hi,

I currently have a control that is on a form [FormA] and I want to pass that exact instance of the control to another form [FormB] (a child form that
appears on a button click). The control has state, etc. that works with
both forms.

In my button click on FormA, I take my control and pass it to a constructor in FormB. Here is the code in the constructor:

public FormB(MyControl ctrl)
{
InitializeComponent();
_passedInControl = ctrl; /* used later to set the parent property
back to the original */
_oldParent = ctrl.Parent; /* used later to set the parent property
back to the original */
ctrl.Parent = this;
this.Controls.Add(ctrl);
}

I also have in the Form Closing event:

private void FormB_Closing(object sender, CancelEventArgs e)
{
if (this._passedInControl != null)
_passedInControl.Parent = _oldParent;
}

Here is my problem: my control doesn't appear on FormB. It disappears from FormA (which should happen because the parent is changing) and it also
reappears on FormA after I close FormB. What do I need to do to get it to
appear on FormB?

Thanks in advance,
cj

Nov 15 '05 #2
Nicholas,

Thanks for the help. I was going to do that as a last resort. The reason
why is that the object I would be passing around is a document viewer that
holds and instance of an Internet Explorer control. It takes some time to
fire that up and it takes up some resources. It will probably be displaying
a multi-page TIFF image that is used on a previous screen (this entire part
of the app is related to data entry off of an electronic image). I thought
I could avoid grabbing the state from the viewer in FormA and passing the
state to FormB. Since there doesn't seem to be another way to do it, that
will be what I'll have to do. Oh well.

Thanks again...

"Nicholas Paldino [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote
in message news:#1**************@tk2msftngp13.phx.gbl...
Curtis,

You shouldn't be passing the control to another form. Rather, the
control should use a memento pattern to store its state into an object that can be passed around easily. Basically, the control should expose an object that exposes the state and then you can pass that to your new form.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Curtis Justus" <se*****@nospam.ser.it.is.com> wrote in message
news:e%***************@TK2MSFTNGP11.phx.gbl...
Hi,

I currently have a control that is on a form [FormA] and I want to pass

that
exact instance of the control to another form [FormB] (a child form that
appears on a button click). The control has state, etc. that works with
both forms.

In my button click on FormA, I take my control and pass it to a

constructor
in FormB. Here is the code in the constructor:

public FormB(MyControl ctrl)
{
InitializeComponent();
_passedInControl = ctrl; /* used later to set the parent property back to the original */
_oldParent = ctrl.Parent; /* used later to set the parent property back to the original */
ctrl.Parent = this;
this.Controls.Add(ctrl);
}

I also have in the Form Closing event:

private void FormB_Closing(object sender, CancelEventArgs e)
{
if (this._passedInControl != null)
_passedInControl.Parent = _oldParent;
}

Here is my problem: my control doesn't appear on FormB. It disappears

from
FormA (which should happen because the parent is changing) and it also
reappears on FormA after I close FormB. What do I need to do to get it to appear on FormB?

Thanks in advance,
cj


Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ron Rohrssen | last post by:
I want to show a dialog and when the form (dialog) is closed, return to the calling form. The calling form should then be able to pass the child form to another object with the form as a...
1
by: Maria | last post by:
Hello! I am new to Crystal reports an I have problems passing parameters form outside to Crystal report an creating a report with data from more than one table This is the problem: I have to...
9
by: Peter | last post by:
I have an Usercontrol on a WebForm, on this Usercontrol is a button, I want to save data that's residing on the WebForm when user clicks on the button that's on the UserControl. How do I call a...
1
by: Josh | last post by:
Hi Guys, I have been having a big problem with trying to pass parameters into a user control when the user control is dynamically loaded into a placholder. I am developing in c#. I have get...
1
by: billy | last post by:
Ok, here's the situation... I have a user control that contains two textboxes (one for a from date/time and one for a to date/time) and two image buttons. The user control itself is supposed to...
41
by: JohnR | last post by:
In it's simplest form, assume that I have created a usercontrol, WSToolBarButton that contains a button. I would like to eventually create copies of WSToolBarButton dynamically at run time based...
2
by: Benedict | last post by:
Hello: What is the correct way to target a method on the MainForm from a UserControl. For example: namespace SampleApp { public partial class MainForm : Form { internal void ShowControl()
3
by: Jeff | last post by:
Hey ASP.NET 2.0 This the ObjectDataSource in my UserControl, <asp:ObjectDataSource ID="odsMessage" runat="server" SelectMethod="ExecuteMessage" TypeName="AH.MyNetwork.BLL.Network.Message">...
9
by: Chris | last post by:
Ok, so I have this sub I wrote, and I create a new instance of a UserControl: ctrlAPs tempctrl = new ctrlAPs(); Now, I would like to be able to use this sub I wrote for more than one...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.