Click here to Skip to main content
16,010,523 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my VB.NET windows application I have a windows from and a user control.

1)when the user click on the button of user control(Not in the forms button), I want to load the user control itself to windows form panel.
I write the following code on the button click event of user control. For this, I wrote a code but it doesn't load the control
VB
Dim obj1 As New UserControl1
obj1.AutoSize = True
obj1.AutoScroll = True
frmMain.pnlUser.Controls.Add(obj1)

This code works if I put a button on Form, but I want to work it from usercontrol1.vb.

Any help?
[edit]Code block added - OriginalGriff[/edit]
[edit]formatted, sentence structure and VB color in code - SM[/edit]
Posted
Updated 26-Mar-11 20:26pm
v4

How do you expect that to work?

How can the control load itself into a panel it knows nothing about, of a form it doesn't know anything about, when the user clicks a button on a control that hasn't been loaded into the form yet, and thus doesn't exist?

That is like trying to cook your dinner in your penthouse apartment before the foundations for the building have been laid...
 
Share this answer
 
yeah you can load it from usercontrol1 for this you need to make your panel named pnlUser modifier to "public" take a look how
go to properties of panel and set Modifier "Friend" to "public"
and then go to your usercontrol form and use given code at click event of button
VB
Dim obj As New frmMain()
      Me.AutoSize = True
      Me.AutoScroll = True
      Me.Dock = DockStyle.Fill
      obj.pnlUser.Controls.Add(Me) 'pnluser is accebile because it set as public
      obj.Show()


Now when you click on button from usercontrol it open itself in frmMain
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900