473,396 Members | 2,020 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,396 software developers and data experts.

Usercontrol: is this possible?

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

Jan 11 '06 #1
4 1173

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.

--
Larry Lard
Replies to group please

Jan 11 '06 #2
Larry Lard wrote:
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
Jan 11 '06 #3
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

Jan 11 '06 #4
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
"johnb41" <js********@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.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

Jan 11 '06 #5

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

Similar topics

3
by: ZhangZQ | last post by:
Supposing I have a class "UserControl1 : System.Windows.Forms.UserControl" in an Assembly "UI.dll", now I create an Application project and want to use the "UserControl1" in another AppDomain, ...
2
by: Lokhan Wong | last post by:
My question is whether it's possible to change the properties of a panel, that resides on the webform containing the usercontrol, in the usercontrol itself. Like this: <form> <asp:panel...
5
by: Lau Lei Cheong | last post by:
Hello, I'm writing a usercontrol to be included in my project. The usercontrol have a few javascript function to do the client-side tasks. However, the controls on the usercontrol seems to...
12
by: Joe | last post by:
Hello All: Do I have to use the LoadControl method of the Page to load a UserControl? I have a class which contains three methods (one public and two private). The class acts as a control...
9
by: Marcelo Cabrera | last post by:
Hi, I have a user control that in turn creates a bunch of webcontrols dynamically and handles the events these webcontrols raise. It used to work fine on ASP .Net 1.1 but when compiled on 2.0 it...
5
by: EqDev | last post by:
I have a class that is a control derived from UserControl. I want to use serialization and deserialization with this calss but I get an exception "Cannot serialize member...
4
by: Michael | last post by:
Hi I know it sounds grazy to create an ActiveX Usercontrol with VB.NET, where a native tool for this already exists, VB6. The existing ActiveX Control Containers should be able to load...
1
by: Giovanni | last post by:
Dear Friends/Gurus, I have exhausted myself and have yet no solution to the following: I have an ASP.NET 2.0 Survey type application. On a page, I have placed a GridView which is bound to an...
0
by: somequestion | last post by:
now ...there are 2 control A and B A is Form control and B is UserControl A : Form {} B: UserControl {} i'd like to make inherit both userControl Form
0
by: Arthur Milfait | last post by:
hey there, i'd like to do some wild thing like this: imagine a simple page with a navigation menu and an iframe. clicking on links in the menu loads the according page in the iframe. the menu...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.