Home All Groups Group Topic Archive Search About

Usercontrol: is this possible?

Author
11 Jan 2006 4:48 PM
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

Author
11 Jan 2006 5:17 PM
Larry Lard
johnb41 wrote:
Show quoteHide quote
> 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.

--
Larry Lard
Replies to group please
Author
11 Jan 2006 5:23 PM
Chris
Larry Lard wrote:
Show quoteHide quote
> johnb41 wrote:
>
>>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.
>

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
Author
11 Jan 2006 5:48 PM
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
Author
11 Jan 2006 9:57 PM
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
Show quoteHide quote
"johnb41" <jsbuchm***@gmail.com> wrote in message
news:1136998137.791604.15880@g14g2000cwa.googlegroups.com...
>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
>