一键调用2个用户控件,在Winforms中单击 [英] calling 2 user controls with one button click in winforms
问题描述
我试图一键调用2个用户控件.我可以使它与一个用户控件一起使用,但是当我尝试添加第二个用户控件时,它给了我一些问题.
我正在使用:
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:
Dim obj1 As New UserControl1
obj1.AutoSize = True
obj1.AutoScroll = True
frmMain.pnlUser.Controls.Add(obj1)
当我尝试调用第二个时,我只是在同一按钮下使用相同的编码,但是将obj1
更改为obj2
,将usercontol1
更改为usercontol2
,并告诉它什么面板.
我是新来的,所以VB编码对您有什么帮助.
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.
推荐答案
您是说要通过一次单击创建两个用户控件吗? br/> 我使用您的代码创建了自己的示例,并且效果很好.我使用了两个面板,两个用户控件和一个按钮.通过代码的外观,我假定frmMain是对传递给按钮单击处理程序所在的第二个窗体的主窗体的引用,因此我也这样做.这是我的代码,如果对您不起作用,它对我来说就很好,我认为您只是忽略了一些小细节.
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.
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.
这篇关于一键调用2个用户控件,在Winforms中单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!