By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,481 Members | 3,078 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,481 IT Pros & Developers. It's quick & easy.

Passing a usercontrol as a form parameter

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.