【问题标题】:Recognizing the last button that was clicked C#识别最后一个被点击的按钮 C#
【发布时间】:2019-02-02 05:57:23
【问题描述】:

我有 3 个按钮 btn1 btn2 btn3 和一个文本框 当我单击任何按钮时,它将专注于文本框,我的问题是,当我按下转义键时,我想专注于被点击的最后一个按钮。例如,当我按下 esc 时单击 btn2,它将重新聚焦在 btn2 上。

【问题讨论】:

    标签: c# .net button textbox focus


    【解决方案1】:

    试试这个。这里所有三个按钮都与常见的点击事件“btn_Click”相关联。

    public partial class Form1 : Form
    {
        private string controlName = "";
        public Form1()
        {
            InitializeComponent();
        }
    
        private void btn_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            controlName = btn.Name;
            txtBox.Focus();
        }
    
        private void txtBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Controls[controlName].Focus();
            }
        }
    }
    

    【讨论】:

    • tnx 男人帮了大忙
    • 当你解决你的问题时,你应该选择一个答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多