【问题标题】:how to reset the focus to last entered textbox in windows application如何在Windows应用程序中将焦点重置为最后输入的文本框
【发布时间】:2013-12-02 04:38:01
【问题描述】:

我在表单中有两个TextBoxes 和一个按钮控件。单击按钮时,最后输入的TextBox 的名称应显示在MessageBox 中。同时我需要将焦点重置为上次输入的TextBox

string str=string.Empty;
bool foc;

在按钮点击中我写了以下代码

    if (MessageBox.Show("You want to reset or continue", "control", 
           MessageBoxButtons.OKCancel) == DialogResult.Cancel)
    {
        if (foc== true)
        {
            textBox1.Focus();
        }
        else
        {
            textBox2.Focus();
        }
    }

当我点击取消按钮时,焦点应该在最后输入的文本框中

    private void textBox1_Enter(object sender, EventArgs e)
    {
        str = textBox1.Name;
        foc= textBox1.Focus();
    }

    private void textBox2_Enter(object sender, EventArgs e)
    {
        str= textBox2.Name;
        foc= false;
    }

除了上述代码行之外,还有其他可能关注文本框,但是当文本框数量增加时,我需要如何编写条件。 如果我在表单中有文本框、组合框、列表框、复选框或任何其他控件,那么如何找到用户最后输入的控件并通过使用任何函数而不是在每个控件中写入来将焦点设置到该控件输入事件

【问题讨论】:

    标签: c#


    【解决方案1】:

    您可以处理TextBoxesLeave 事件来存储最后一个TextBox 控件。

    试试这个:

        this.btnSubmit.Click += new System.EventHandler(this.Submit_Click);
        this.btnCancel.Click += new System.EventHandler(this.Cancel_Click);
        this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave);
        this.textBox2.Leave += new System.EventHandler(this.textBox2_Leave);
        TextBox txtLast = new TextBox();
    
    
        private void textBox1_Leave(object sender, EventArgs e)
        {           
            txtLast = (TextBox)sender;
        }
        private void textBox2_Leave(object sender, EventArgs e)
        {            
            txtLast = (TextBox)sender;
        }
        private void Submit_Click(object sender, EventArgs e)
        {
            MessageBox.Show(txtLast.Text);
        }
    
        private void Cancel_Click(object sender, EventArgs e)
        {
            txtLast.Focus();
        }
    

    【讨论】:

    • @user2897388 :不客气 :) ,如果对您有帮助,请将其标记为答案。
    • 如果我在表单中有文本框、组合框、列表框、复选框或任何其他控件,那么如何查找用户最后输入的控件并使用任何函数而不是设置焦点到该控件写在每个控制离开事件中
    【解决方案2】:
     public bool textBox1WasLastFocused = false, textBox2WasLastFocused = false; // Global Declaration
     void textBox2_GotFocus(object sender, EventArgs e)
     {
        textBox2WasLastFocused = true;
        textBox1WasLastFocused = false;
     }
    
     void textBox1_GotFocus(object sender, EventArgs e)
     {
         textBox1WasLastFocused = true;
         textBox2WasLastFocused = false;
     }
    
     private void button1_Click(object sender, EventArgs e)
     {
         if (textBox1WasLastFocused)
            MessageBox.Show("textbox1 was ladst focused !");
         else if(textBox2WasLastFocused)
            MessageBox.Show("textbox2 was ladst focused !");
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-17
      相关资源
      最近更新 更多