动态将按钮及其事件添加到WinForms [英] Dynamically add button and its event to WinForms

查看:81
本文介绍了动态将按钮及其事件添加到WinForms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Form项目,该项目必须动态添加一个按钮,并且在一个事件中必须首先将Form背景颜色更改为红色,然后将其第二次更改为绿色.
下面是代码



公共局部类Form1:Form
{
公共Form1()
{
InitializeComponent();
}
private int X = 100;
private int Y = 200;
Button btn = new Button();
私有void Form1_Load(对象发送者,EventArgs e)
{
this.Controls.Add(btn);
btn.Location =新Point(X,Y);
btn.Text =点击";
btn.Click + = new EventHandler(Mouse_Click);
}


私有void Mouse_Click(Object s,EventArgs e)
{
//在这里怎么做?
}


}

I''m creating a Form project which must add a button dynamically and within an event it must change Form background color to red with first click, and then, with the second click to green.
Below is the code



public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int X = 100;
private int Y = 200;
Button btn = new Button();
private void Form1_Load(object sender, EventArgs e)
{
this.Controls.Add(btn);
btn.Location = new Point(X, Y);
btn.Text = "Click";
btn.Click += new EventHandler(Mouse_Click);
}


private void Mouse_Click(Object s, EventArgs e)
{
// how to do here?
}


}

推荐答案

假设您希望Button在每次单击时都继续切换颜色,并且您无需在启动时保留原始的Form BackColor:
Assuming you want the Button to continue switching colors with every Click, and that you do not need to preserve the original Form BackColor on start-up:
Color firstColor = Color.Red;
Color secondColor = Color.Green

bool whichColor = true;

private void btn_Click(Object s, EventArgs e)
{
    BackColor = whichColor ? firstColor : secondColor;

    whichColor = ! whichColor;
}

,或者,您可以将其简化为:

Or, you could simplify this to:

private void button1_Click(object sender, EventArgs e)
{
    BackColor = (whichColor = ! whichColor) ? firstColor : secondColor;
}

如果您确定不需要在运行时在其创建范围/上下文之外访问所创建的Button,则可以进一步简化操作:

You could simplify even further if you are sure you don''t need access to the created Button outside the scope/context in which it was created at run-time:

private void SomeForm_Load(object sender, EventArgs e)
{
    // outside of the Load Event you will have no access
    // to the Button you create here !
    Button newButton = new Button();
    
    newButton.AutoSize = true;
    newButton.Location = new Point(400, 100);
    newButton.Text = "Click to Change Form Background Color";
    newButton.ForeColor = Color.White;
    
    // anonymous EventHandler defined with Lambda expression
    newButton.Click += (obj, eArgs) =>
    {
        BackColor = (BackColor == Color.Red) ? Color.Green: Color.Red;
    };
    
    Controls.Add(newButton);
}

请注意一个有趣的事实,如果您创建的控件(作为Form的子控件)没有在运行时创建时设置的BackColor,它将继承"该控件的BackGround颜色.窗体,并且随着窗体BackColor的更改,Button也将更改BackGround Color:我从未见过Microsoft正式"描述的这种行为!

但是,这种行为正是我将Button的ForeColor属性设置为白色的原因:这样,无论Button BackColor是红色还是绿色,都可以看到:)

Note the interesting fact that if the Control you create, as a child Control of a Form, does not have its BackColor, set when you create it at run-time, it will "inherit" the BackGround Color of the Form, and as the form BackColor is changed, the Button will change BackGround Color also: I have never seen this behavior "officially" described by Microsoft !

But, that behavior was exactly why I set the ForeColor property of the Button to white: so that it would be visible whether the Button BackColor was red, or green :)


添加一个变量,建议点击次数.
然后在提交的函数中,编写如下:


int ClickTime = 0;
私有void Mouse_Click(Object s,EventArgs e)
{
if(ClickTime == 0)
{
this.BackColor = Color.Red;
ClickTime ++;
}
if(ClickTime == 1)
{
this.BackColor = Color.Green;
ClickTime ++;
}
if(ClickTime == 2)
{
//在这里添加您自己的代码
}
}
Add a variable which suggests the click times.
Then in the function filed, write like this:


int ClickTime=0;
private void Mouse_Click(Object s, EventArgs e)
{
if(ClickTime==0)
{
this.BackColor=Color.Red;
ClickTime++;
}
if(ClickTime==1)
{
this.BackColor=Color.Green;
ClickTime++;
}
if(ClickTime==2)
{
//add your own codes here
}
}


这篇关于动态将按钮及其事件添加到WinForms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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