如何以窗口形式调用用户控件? [英] How to call user control in window form?

查看:74
本文介绍了如何以窗口形式调用用户控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户控件,在该用户控件中我有一个按钮,如果按钮点击它加载另一个用户控件但我不知道该怎么做。



我尝试过:



公共部分类ManageTenant:UserControl

{

public ManageTenant()

{



InitializeComponent();



}

private void btnAddNew_Click(object sender,EventArgs e)

{

//这是我的第二个用户控件名是TenantSetup

var control = new TenantSetup();



}



}

i have a user control and in that user control i have a button if button is click it load another user control but i do not know how to do this.

What I have tried:

public partial class ManageTenant : UserControl
{
public ManageTenant()
{

InitializeComponent();

}
private void btnAddNew_Click(object sender, EventArgs e)
{
// this is my second User Control Name is TenantSetup
var control = new TenantSetup();

}

}

推荐答案

你想要做什么取决于新用户控件应该去哪里:如果它在你现有的控件里面,那就没问题了:

Exactly what you want to do depends on where the new usercontrol is supposed to go: if it's inside your existing control, then it's no problem:
TenantSetup ts = new TenantSetup();
ts.Location = new Point(100,100);
Controls.Add(ts);

如果你想将它添加到包含原始UserControl的表单中,那么这有点复杂,因为你需要问一下这样做是为了做到这一点您。请看这里:在两个表格之间传递信息,第2部分:孩子到父母 [ ^ ] - 它是基于表单的,但是如果你用我的UserControl替换子表单,那就完全相同的程序和相同的代码。

If you want it added to the form that contains the original UserControl, then that's a little more complicated, because you need to ask teh for to do that for you. Have a look here: Transferring information between two forms, Part 2: Child to Parent[^] - it's forms based, but if you mentaly replace "child form" with "my UserControl" it's exactly the same procedure, and the same code as well.


每当你创建一个代码中的UserControl 的实例在运行时使用'new,然后你需要 UserControl添加到具有Controls集合的其他对象。这可以是Form,Panel或其他UserControl,或者UserControl可以作为元素插入到DataGridView,TableLayoutPanel等中。添加新创建的UserControl实例后,您需要在托管表单/控件中设置其位置。



您可以在设计时创建Project的UserControls实例 - 通过将ToolBox图标拖放到窗体上,或拖放到允许托管其他控件的窗体上的另一个控件上;拖放调用UserControl的Load事件,您可以在其中建立与所需托管表单/控件的任何通信,并且已删除的UserControl会自动添加到主机控件的ControlCollection中。



在任何一种情况下,您都需要让UserControl发布...提供一种访问...数据或事件的方法,您希望托管表单/控件使用。这可以通过公共字段,属性,定义公共事件等多种方式完成。



如果您澄清您的问题以更清楚地解释如何使用管理两个UserControl,我想你会得到更具体的反馈。描述您正在使用的表单,以及何时/如何显示和使用UserControl。
Whenever you create an instance of a UserControl in code at run-time using 'new, you then need to add that UserControl to some other object that has a Controls collection. That could be a Form, a Panel, or another UserControl, or the UserControl could be inserted into a DataGridView, a TableLayoutPanel, etc. as an element. After you add the newly created instance of the UserControl, you will need to set its location in the hosting Form/Control.

You create instances of your Project's UserControls at design-time by drag-dropping their ToolBox icon onto a Form, or onto another Control on the Form that allows hosting other Controls; the drag-drop invokes the Load event of the UserControl in which you can establish whatever communication with the hosting Form/Control you need, and the dropped UserControl is automatically added to the hosting control's ControlCollection.

In either case you need have the UserControl "publish" ... provide a means of access to ... data, or events, that you will want the hosting Form/Control to use. This can be done in many ways, by public Fields, Properties, defining public Events, etc.

If you clarify your question to more clearly explain how the use of the two UserControls is managed, I think you'll get more specific feedback. Describe the Form you are using, and when/how the UserControls are shown, and used.


这篇关于如何以窗口形式调用用户控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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