如何在单击按钮时在表单中显示UserControl? [英] How to show UserControl in a form when a button is clicked?

查看:74
本文介绍了如何在单击按钮时在表单中显示UserControl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

我只想在单击按钮时以Windows窗体打开Usercontrol(包含按钮和文本框)。



我用button1和botton2创建了form1,并创建了Usercontrol1和Usercontrol2。现在在form1中我想在按下button1时调用Usercontrol1(在form1中打开它),然后按button2显示Usercontrol2并隐藏Usercontrol1但我不知道该怎么做。我在form1中为Usercontrol1创建了一个对象(Usercontrol1.visible = true),但它没有用。

请帮忙。

谢谢

解决方案

尝试:

 MyUserControl1Name.Visible =  true ; 
MyUserControl2Name.Visible = false ;

显示1并隐藏2,并且:

 MyUserControl1Name.Visible =  false ; 
MyUserControl2Name.Visible = true ;

隐藏1并显示2.

这两个名字必须是你给设计器中表单上放置的控件实例的那些 - 所以如果你的控件类是UserControlA和UserControlB,那么实例可能叫做userControlA1和userControlB1(但是改变它们以反映用法,你永远不应该离开控件使用VS指定的默认名称 - 与Button不应该是button1但butShowControlA的方式相同。



您可能需要设置两者控件的Visible属性在开头的设计器中为false。


用户控件的运行时创建和选址



1关键要素



a。创建实例:



UserControl1 myUC1 = new UserControl1();



b。将实例添加到Form或其他ContainerControl的ControlCollection:



form1.Controls.Add(myUC1);



c。设置添加的UserControl的位置和/或z顺序:



 myUC1.Location = new Point(200,200);< br / > 
myUC1.BringToFront(); //确保它在前面





如果你真的想销毁/清除现有的UserControl以确保每次创建UserControl在运行时你有一个新鲜的实例:

 UserControl1 myUC1; 

private void button1_Click(对象发​​件人,EventArgs e)
{
如果(myUC1!= null )myUC1.Dispose();
if (myUC2!= null )myUC2.Dispose();

myUC1 = new UserControl1();
Controls.Add(myUC1);
myUC1.BringToFront();
}

UserControl2 myUC2;

private void button2_Click(对象发​​件人,EventArgs e)
{
如果(myUC1!= null )myUC1.Dispose();
if (myUC2!= null )myUC2.Dispose();

myUC2 = new UserControl2();
Controls.Add(myUC2);

myUC2.BringToFront();
}

重新使用Design-Time选址的UserControls



我建议你创建两个UserControls 在设计时将它们从ToolBox拖放到您希望它们出现的Form上。在将它们显示在工具箱中之前,您需要编译一次项目。



将它们放置在您希望它们在运行时出现的位置。



然后,对于两个UserControl,在其属性窗口中将其可见属性设置为false。这样,最初它们将不可见。



所以:在你的代码中按钮Click事件:

  //  假设您有userControl11和userControl21实例: 

// 承载UserControls实例的Form中的代码

private void btnShowUC1_Click( object sender,EventArgs e)
{
// 隐藏userControl21?
userControl21.Hide();

userControl11.Show();
userControl11.BringToFront();
}

private void btnShowUC2_Click( object sender,EventArgs e)
{

// hide userControl11
userControl11.Hide();

userControl21.Show();
userControl21.BringToFront();
}

我可以看到重新使用设计时创建UserControls的唯一缺点就是当你需要清除任何字段,或者在UserControls中输入控件的任何用户数据时,在你重新使用它们之前。



例如,如果你有一个带有TextBox的UserControl和一个CheckBox(CheckBox的ThreeState属性设置为'true) ),您可以从位于UserControl的表单中调用这样的方法:

  //   UserControl中的代码 
public void Clear()
{
textBox1.Clear();
checkBox1.CheckState = CheckState.Indeterminate;
}

或者,你可以为UserControls的'VisibleChanged事件定义一个EventHandler,当UserControl变为可见时,然后清除字段,重新设置控件等。



由于显示Form或UserControl时可能希望做的事情可能有所不同,并且可能包括想要显示初始值,您将如何处理字段和控件 - 使用它们会有所不同。



你可以改变这里显示的'清除示例,而不是清除,重置任何预先定义的状态或值。


Hi
I just want to open a Usercontrol (which contains buttons and textboxes) in a windows form when I click a button.

I created form1 with button1 and botton2 and also created Usercontrol1 and Usercontrol2 . now in form1 I want to call Usercontrol1 (open it in form1) when button1 is pressed and then press button2 to show Usercontrol2 and hide Usercontrol1 but I dont know how to do it. I created an object for Usercontrol1 in form1 (Usercontrol1.visible = true) but it didnt work.
please help.
Thank you

解决方案

Try:

MyUserControl1Name.Visible = true;
MyUserControl2Name.Visible = false;

To show 1 and hide 2, and:

MyUserControl1Name.Visible = false;
MyUserControl2Name.Visible = true;

To hide 1 and show 2.
The two names must be those you gave to the control instances that you dropped on the form in the designer - so if your control classes are UserControlA and UserControlB, the instances are probably called userControlA1 and userControlB1 (but change them to reflect the use, you should never leave controls with the default names that VS assigns - in the same way that a Button shouldn't be "button1" but "butShowControlA").

You probably need to set both of the controls Visible property to false in the designer to start with.


Run-Time creation and siting of UserControls

1. key elements

a. create the instance:

UserControl1 myUC1 = new UserControl1();

b. add the instance to a Form or other ContainerControl's ControlCollection:

form1.Controls.Add(myUC1);

c. set the location and/or z-order of the added UserControl:

myUC1.Location = new Point(200,200);<br />
myUC1.BringToFront(); // make sure it's in front



If you really want to destroy/clear an existing UserControl to make sure each time you create the UserControl at run-time you have a "fresh" instance:

UserControl1 myUC1;

private void button1_Click(object sender, EventArgs e)
{
    if(myUC1 != null) myUC1.Dispose();
    if(myUC2 != null) myUC2.Dispose();

    myUC1 = new UserControl1();
    Controls.Add(myUC1);
    myUC1.BringToFront();
}

UserControl2 myUC2;

private void button2_Click(object sender, EventArgs e)
{
    if(myUC1 != null) myUC1.Dispose();
    if(myUC2 != null) myUC2.Dispose();

    myUC2 = new UserControl2();
    Controls.Add(myUC2);
    
    myUC2.BringToFront();
        }

Re-using Design-Time sited UserControls

I suggest you create both UserControls at design-time by drag-dropping them from the ToolBox onto the Form you wish them to appear on. You will need to compile your project once before they will appear in the ToolBox.

Position them where you want them to appear at run-time.

Then, for both UserControls, set their 'Visible property to 'false in their Properties Window. That way, initially they will not be visible.

So: in your code for the Buttons Click Event:

// assuming you have userControl11, and userControl21 instances:

// code in the Form that hosts the instances of the UserControls

private void btnShowUC1_Click(object sender, EventArgs e)
{
     // hide userControl21 ?
     userControl21.Hide();

     userControl11.Show();
     userControl11.BringToFront();
}

private void btnShowUC2_Click(object sender, EventArgs e)
{

     // hide userControl11
     userControl11.Hide();

     userControl21.Show();
     userControl21.BringToFront();
}

The only "downside" I can see to re-using design-time created UserControls like this is when you need to clear any fields, or any user data entered into Controls in the UserControls , before you re-use them.

For example, if you had a UserControl with a TextBox, and a CheckBox (with the CheckBox 'ThreeState property set to 'true), you could call a method like this from the Form where the UserControl is sited:

// code in the UserControl
public void Clear()
{
    textBox1.Clear();
    checkBox1.CheckState = CheckState.Indeterminate;
}

Or, you could define an EventHandler for the 'VisibleChanged event of the UserControls, and, when the UserControl became visible, then clear field, re-set Controls, etc.

Since what one may wish to do when a Form, or UserControl is shown may vary, and may include wanting to show initial values, how you will handle fields and Controls as you re-use them will vary.

You could change the 'Clear example shown here, and, instead of clearing, reset whatever to pre-defined states, or values.


这篇关于如何在单击按钮时在表单中显示UserControl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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