Click here to Skip to main content
15,911,132 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to call 2 user controls with one button click. I can get it to work with one user control, but when I try adding the second user control it gives me issues.

I am using:
VB
Dim obj1 As New UserControl1
obj1.AutoSize = True
obj1.AutoScroll = True
frmMain.pnlUser.Controls.Add(obj1)

When I try to call the second one I just use the same coding under the same button but change the obj1 to obj2 and the usercontol1 to usercontol2 and tell it what panel.
I am new to this so any help would be great with the VB coding.
Posted
Updated 13-Nov-11 8:01am
v4

1 solution

Are you saying that you want to create two users controls using one button click?
I create my own example using your code and it worked fine. I used two panels, two user controls, and a button. By the look of your code I assumed frmMain to be a reference to your main form passed to a second form where the button click handler was located so I did this as well. Here is my code and it worked just fine for me if it is not working for you I assume you are just overlooking some small detail.

VB
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim obj1 As New UserControl1
    obj1.AutoSize = True
    obj1.AutoScroll = True
    frmMain.Panel1.Controls.Add(obj1)

    Dim obj2 As New UserControl2
    obj2.AutoSize = True
    obj2.AutoScroll = True
    frmMain.Panel2.Controls.Add(obj2)
End Sub


Let me know.

Also if you are getting any errors please post them.
 
Share this answer
 
v2
Comments
Zachary.shupp 13-Nov-11 14:03pm    
I was over looking the step and got it to work but the other issue i have is getting it to work in a wpf application with vb. Would you know how to call the usercontrol to a dock panel or should i use something other than a dock panel. I realize that in wpf they have detailed panels unlink winform. Thanks for the help.
LanFanNinja 13-Nov-11 20:32pm    
This should work as far as adding to DockPanel.

Dim obj1 As New UserControl1
obj1.Width = 50
obj1.Height = 50
DockPanel1.Children.Add(obj1)

I have only used WPF a couple of times so I am not sure how you would do the AutoSize and AutoScroll. AutoSize is the default for user controls in WPF I think. Hope this helps.

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