button.PerformClick()与button_Click(sender,e)的小区别

1、button.PerformClick()和button_Click(sender,e)的功能:

   两者都是实现单击button控件功能。

2、两者存在的一点小差异

如以下

示例1(部分代码):

private void button1_Click (Object sender,  EventArgs e)
{
    // If myVar is an even number, click Button2.
    if(myVar %2 == 0)
    {

       //下边“A”和“B”的功能是一样的,但“B”处需保证button2的Enabled事件为True,而“A”却不用

       button2_Click(sender,e);//A(此处为调用button2_Click方法,故不受Enabled是ture还是false限制)
       button2.PerformClick();//B(此处为模拟button2的Click事件,需保证button2.Enabled==true)
       // Display the status of Button2's Click event.
       MessageBox.Show("button2 was clicked ");
    }
    else
    {
       // Display the status of Button2's Click event.
       MessageBox.Show("button2 was NOT clicked");
    }
    // Increment myVar.  
    myVar = myVar + 1;
}
示例2(全部代码):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SuperPlane
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("button1");//弹出窗口
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //无论button1.Enabled为true还是为false都会弹出窗口
            button1_Click(sender, e);//以button.Click(sender,e)调用button1.Click方法
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //只有当button1.Enabled为true时才会弹出窗口
            button1.PerformClick();//以button1.PerformClick()模拟button1单击事件
        }

        private void button4_Click(object sender, EventArgs e)
        {
            button1.Enabled = !button1.Enabled;//改变button1的Enabled改变
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值