单击按钮加载用户控件 [英] Loading user control from it's button click

查看:66
本文介绍了单击按钮加载用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的VB.NET Windows应用程序中,我有一个Windows和一个用户控件.

1)当用户单击用户控件的按钮(不在窗体按钮中)时,我想将用户控件本身加载到Windows窗体面板中.
我在用户控件的按钮单击事件上编写以下代码.为此,我编写了代码,但未加载控件

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

Dim obj1 As New UserControl1
obj1.AutoSize = True
obj1.AutoScroll = True
frmMain.pnlUser.Controls.Add(obj1)


如果在Form上放置一个按钮,则此代码有效,但我想从usercontrol1.vb进行操作.

有帮助吗?
[edit]已添加代码块-OriginalGriff [/edit]
代码中的[edit]格式,句子结构和VB颜色-SM [/edit]


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]

推荐答案

您希望它如何工作?

当用户单击尚未加载到窗体中的控件上的按钮时,控件如何将自身加载到它不了解的窗体中,它什么都不知道的面板中不存在吗?

就像尝试在建筑物的基础铺设好之前在顶层公寓里做饭...
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...


是的,您可以从usercontrol1加载它,为此您需要您的名为pnlUser修饰符的面板,用于公开" 看看
转到面板的属性,并将修改器朋友" 设置为公共"
然后转到您的usercontrol表单,并在按钮
的点击事件中使用给定的代码
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
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()



现在,当您从用户控件中单击按钮时,它将在frmMain中打开自身



Now when you click on button from usercontrol it open itself in frmMain


这篇关于单击按钮加载用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆