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

本文探讨了在C#中button控件的两种点击方法:button.PerformClick()和button_Click(sender,e)。这两种方法均可模拟按钮点击事件,但具体使用时存在细微差别,主要体现在是否受button控件Enabled属性的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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改变
        }
    }
}

using System; using System.Data; using System.Drawing; using System.IO; using System.Windows.Forms; namespace Session1 { public partial class Form1 : Form { private static string ConfigFilePath = "login.config"; public Form1() { InitializeComponent(); InitializeRememberMe(); } private void InitializeRememberMe() { try { if(File.Exists(ConfigFilePath)) { string[] lines = File.ReadAllLines(ConfigFilePath); if (lines.Length >= 2) { txt_user.Text = lines[0]; txt_pw.Text = lines[1]; ck_km.Checked = true; } } }catch(Exception ex) { MessageBox.Show("读取配置文件错误", "消息", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void SaveLoginInfo() { try { if (ck_km.Checked) { File.WriteAllLines(ConfigFilePath, new[] { txt_user.Text, txt_pw.Text }); }else if (File.Exists(ConfigFilePath)) { File.Delete(ConfigFilePath); } } catch (Exception ex) { MessageBox.Show("读取配置文件错误", "消息", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void button2_Click(object sender, EventArgs e) { this.Close(); } private void btn_login_Click(object sender, EventArgs e) { if (txt_user.Text != "" && txt_pw.Text != "") { Dao dao = new Dao(); string sql = $"select * from Users where FullName='{txt_user.Text}' and Password='{txt_pw.Text}'"; IDataReader dc = dao.read(sql); if (dc.Read()) { Data.name = dc["FullName"].ToString(); Data.Pw = dc["Password"].ToString(); Data.usid = dc["UserTypeID"].ToString(); if (Data.usid == "1") { Formptyh from = new Formptyh(); this.Hide(); from.ShowDialog(); this.Show(); }if (Data.usid == "2") { Formfdyh from = new Formfdyh(); this.Hide(); from.ShowDialog(); this.Show(); } if (Data.usid == "3") { FormAdmin from = new FormAdmin(); this.Hide(); from.ShowDialog(); this.Show(); } } else { MessageBox.Show("账号或密码错误!"); } } else { MessageBox.Show("用户名或密码不能为空!"); } } private void ck_pw_CheckedChanged(object sender, EventArgs e) { txt_pw.PasswordChar = ck_pw.Checked ? '\0' : '*'; } private void ck_km_CheckedChanged(object sender, EventArgs e) { SaveLoginInfo(); } } }在记住密码里加入自动登录功能
06-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值