清除两个文本框文本中的一个 [英] Clearing Two Textbox text either one which is selected

查看:54
本文介绍了清除两个文本框文本中的一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Windows 窗体中的文本框,我有一个清除按钮,它基本上一个字符一个字符地清除文本(即一个一个,而不是一次全部清除),我的问题是我想在这个窗体中添加另一个文本框并且会喜欢用清除按钮控制两个文本框意味着清除应该只清除我选择或点击的文本框,我尝试这样做,但我可以同时清除两个文本框或只清除文本框我的单个文本框代码是

I have a textbox in a Windows form, I have clear button which basically clears the text char by char(that is one by one not all at once), my problem is i want to add another textbox to this form and would like to control both the textboxes with clear button meaning the clear should only clear the textbox which i have selected or clicked on, i tried doing it but either i am able to clear both the textboxes simultaneously or clear only textbox my code for single textbox is

private void clearBtn_Click(object sender, EventArgs e)
{
  string s = txtID.Text;
  if (s.Length > 0) txtID.Text = s.Substring(0, s.Length - 1);
}

推荐答案

您可以设置哪个控件具有焦点,然后使用它来查看需要删除哪个控件.

You can set the which control has focus upon focus and then use that to see which one needs to be removed.

private Textbox SelectedTextBox;

protected void Form_Load(object sender, EventArgs e)
{
    TextBox1.GotFocus += TextBox_GotFocus;
    TextBox2.GotFocus += TextBox_GotFocus;
}

private void clearBtn_Click(object sender, EventArgs e)
{
    if(this.SelectedTextBox == null) return;
    string s = this.SelectedTextBox.Text;
    if (s.Length > 0) this.SelectedTextBox.Text = s.Substring(0, s.Length - 1);
}

private void TextBox_GotFocus(object sender, EventArgs e)
{
    this.SelectedTextBox = (Textbox)sender;
}

这篇关于清除两个文本框文本中的一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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