通过单击按钮来处理UserControl [英] Handling UserControl by Clicking on the Button

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

问题描述

你好,
我是C#的新手,在处理用户控件时遇到了一些问题

我做了以下步骤:

< b>当我打开一个新窗口项目时.我在其中创建了两个面板.一个用于按钮,另一个用于用户控件</b>

< b>在panel1中添加了两个按钮,并将用户控件命名为usercontrol1& usercontrol2</b>

我的想法是,当我单击button1时,usercontrol1应该显示在第二个面板上,而当我单击button2时,usercontrol2应该显示在第二个面板上.

请帮助我处理它们的事件.

Hello,
i am new to C#, i am having a little problem in handling user controls

i did following steps:

<b>when i opened a new window project. i created two panels in it. one for buttons and other for user controls</b>

<b>added two button in panel1 and user controls named usercontrol1 & usercontrol2</b>

my idea to do this is, when i click on the button1, usercontrol1 should display on the second panel, and when i click on the button2, usercontrol2 should display on the second panel.

kindly help me handling their events.

推荐答案

建议使用 ^ ].它的一侧的侧面(通常在顶部)已经内置了按钮.
Suggest using a TabControl[^]. It has the buttons already built-in in a row on one of its sides, usually at the top.


仅将两个用户控件放置在panel2内,并将可视性属性设置为Visible. ="false".在按钮的单击事件上,只需将相关用户控件的可见性属性设置为true/false.

如果解决了您的问题,请标记为答案.
Place both of your user controls inside panel2 only, with setting the visibility property to Visible="false". on the click events of your buttons, just set visibility property to true/false for the relevant user controls.

mark as answer if solves ur problem.


将您的UserControls放在用户控件面板中.他们不需要坐在任何特别的地方,只是为了在您需要更改某些内容时,您可以在设计模式下接触到他们中的任何一个.

启动应用程序时,告诉它们每个都覆盖整个用户控制面板,但同时变得不可见.
Place your UserControls within the user controls panel. They don''t need to sit anywhere special, just so you reach any of them in design mode in case you want to change something.

When starting your application, tell each of them to cover the whole user controls panel, but at the same time go invisible.
public class Form1
{
    protected override OnLoad( object sender, EventArgs e)
    {
        foreach( Control control in userControlsPanel.Controls)
        {
            control.Dock = DockStyle.Fill;
            control.Visible = false;
        }
    }
}

然后为按钮创建事件处理程序方法.其中之一可能看起来像这样

Then create event handler methods for your buttons. One of them could look like this

public class Form1
{
    private void Button1_Click( object sender, EventArgs e)
    {
        foreach( Control control in userControlPanel.Controls)
        {
            control.Visible = AreAssociated( Button1, control);
        }
    }
}

有几种方法可以将按钮与用户控件之一相关联.

您可以通过以下方式使用按钮的Tag属性:

There are several ways how you could associate a button with one of the user controls.

You could use the button''s Tag property in such a way:

Button1.Tag = UserControl1;

然后,以上代码将更改为
control.Visible = ( control.Equals(Button1.Tag));
此外,最好不要每个按钮有一个事件处理程序,而是有一个事件处理程序来处理相似使用的按钮的所有Click事件.您可以附加一个事件处理程序,以便附加多个按钮单击事件.然后,处理程序可能看起来像

Above code would then change to
control.Visible = ( control.Equals(Button1.Tag));
Furthermore, it would be nicer not to have one event handler per button, but to have one event handler that handles all the Click events of the similarly used buttons. You can attach one event handler so several button click events. The handler could then look like

public class Form1
{
    private void AnyButton_Click( object sender, EventArgs e)
    {
        if(!(sender is Button))
        {
            throw new ArgumentException("AnyButton_Click() has to be called by a Button.");
        }


        foreach( Control control in userControlPanel.Controls)
        {
            control.Visible = control.Equals((Button)sender).Tag;
        }
    }
}


这篇关于通过单击按钮来处理UserControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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