我有一个Windows窗体中的文本框,我有清除按钮,它基本上清除了字符char的字符(一个一个不是一次全部),我的问题是我想在此表单中添加另一个文本框喜欢使用清除按钮控制两个文本框意味着清除应该只清除我选择或点击的文本框,我尝试这样做,但要么我能够同时清除文本框或只清除文本框我的单个文本框的代码是
private void clearBtn_Click(object sender, EventArgs e)
{
string s = txtID.Text;
if (s.Length > 0) txtID.Text = s.Substring(0, s.Length - 1);
}
答案 0 :(得分:2)
您可以设置哪个控件在焦点上具有焦点,然后使用它来查看哪个需要被删除。
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;
}
答案 1 :(得分:0)
private void clearBtn_Click(object sender,EventArgs e) {
sting textbox1text = textbox1.tostring();
string textbox2text = textbox2.tostring();
if (textbox1text.length > 0){
textbox1text = textbox1text.Remove(textbox1text.length - 1)
}
if (textbox2text.length > 0){
textbox2text = textbox2text.Remove(textbox1text.length - 1)
}
}
将"退格"每个文本框的一个字符。 如果要对上次更新的文本框进行清除,请将其添加到每个if语句
if (textbox2text.length > 0 && textbox2text == textbox2text)
将检查文本框在清除之前是否已更新,如果没有更新则不会清除