Usercontrol: is this possible?

J

johnb41

I created a UserControl (basically containing a datagridview for
showing some data). I need this UserControl to be visible on 2 forms
at the same time.

Here's a stripped down version of some code:

Dim Myusercontrol as UserControlDataGrid
Myusercontrol = New UserControlDataGrid

Dim myForm1 as new Form1
myForm1.controls.add(Myusercontrol)
myForm1.show

Dim myForm2 as new Form2
myForm2.controls.add(Myusercontrol)
myForm2.show


What is happening is myForm2 gets the usercontrol, but myForm1 doesn't.
If i flip the code, then, the reverse is true.

How can I get BOTH myForm1 AND myForm2 to get the exact same
usercontrol?

Thanks for all your help!

John
 
L

Larry Lard

johnb41 said:
I created a UserControl (basically containing a datagridview for
showing some data). I need this UserControl to be visible on 2 forms
at the same time.

Here's a stripped down version of some code:

Dim Myusercontrol as UserControlDataGrid
Myusercontrol = New UserControlDataGrid

Dim myForm1 as new Form1
myForm1.controls.add(Myusercontrol)
myForm1.show

Dim myForm2 as new Form2
myForm2.controls.add(Myusercontrol)
myForm2.show


What is happening is myForm2 gets the usercontrol, but myForm1 doesn't.
If i flip the code, then, the reverse is true.

How can I get BOTH myForm1 AND myForm2 to get the exact same
usercontrol?

You can't. Controls only have one parent. A quick look at the docs
doesn't turn up an explicit statement of this, but it's true. By way of
poor evidence, consider the Control's Parent property, which is a
single value.

Now, to actually solve your problem: One way might be to make the
'backend' (data-side) parts of your control Shared (not 100% if you can
even do this), so that when you add one grid to form1, and another to
form2, they both have the same data source. This would limit you to
only ever having one such across the whole app though. There are
probably better ways.
 
C

Chris

Larry said:
You can't. Controls only have one parent. A quick look at the docs
doesn't turn up an explicit statement of this, but it's true. By way of
poor evidence, consider the Control's Parent property, which is a
single value.

Now, to actually solve your problem: One way might be to make the
'backend' (data-side) parts of your control Shared (not 100% if you can
even do this), so that when you add one grid to form1, and another to
form2, they both have the same data source. This would limit you to
only ever having one such across the whole app though. There are
probably better ways.

I was thinking about this... Could you remove the control from Form1,
and add the instance to Form2 when Form2 is activated and back when
Form1 is activated? I know it isn't clean, but it was just the idea I
had when I read this...

Chris
 
J

johnb41

Too bad this isn't really possible. But i found a way that will work
ok for my needs (your post gave me the idea):

On form1, i click a button and form2 loads. The Usercontrol from form1
disappears and re-appears on form2. I guess I can deal with that.

Now, when i close form2, i change the parent of the usercontrol to be
back to Form1.

This will work. Thanks for your help Larry and Chris!

John
 
B

Bob

Have you tried
Dim Myusercontrol1 as UserControlDataGrid
Myusercontrol1 = New UserControlDataGrid
Dim Myusercontrol2 as UserControlDataGrid
Myusercontrol2 = New UserControlDataGrid

Dim myForm1 as new Form1
myForm1.controls.add(Myusercontrol1)
myForm1.show

Dim myForm2 as new Form2
myForm2.controls.add(Myusercontrol2)
myForm2.show

Would that do it for you?
HTH
Bob
 

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