Passing a usercontrol as a form parameter

C

Curtis Justus

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
C

Curtis Justus

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 said:
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]
- (e-mail address removed)

Curtis Justus said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top