简体   繁体   English

清除文本框C#

[英]Clearing textboxes c#

So I have: 所以我有:

3 text-boxes, 1 Clear all button (which should clear ALL text-boxes), and 1 clear selected text-box (which clears only the selected text-box). 3个文本框,1清除所有按钮(应清除所有文本框),以及1清除选定的文本框(仅清除选定的文本框)。

How would I go about doing this? 我将如何去做呢? Here is my code: 这是我的代码:

        private void ClearAllTextBox_Click(object sender, EventArgs e)
    {
        foreach (Control c in Controls)
        {
            if (c is TextBox)
            {
                c.Text = "";
            }
        }
    }

I have not posted code for the "Clear Current Field" button. 我尚未为“清除当前字段”按钮发布代码。

With this, the first time I press the button it only clears the SecondOperatorTextBox and the ResultTextBox. 有了这个,我第一次按下按钮时,它只会清除SecondOperatorTextBox和ResultTextBox。 If I click it one more time it then clears the FirstOperatorTextBox. 如果我再单击一次,它将清除FirstOperatorTextBox。

How would I clear all 3 text-boxes with 1 button, and only clear selected with another button? 如何用一个按钮清除所有3个文本框,而仅用另一个按钮清除所选的文本框?

Your ClearAllTextBoxes_Click event handler seems to clear all the three Textboxes, so I will try to answer the clear "selected textbox" with one button. 您的ClearAllTextBoxes_Click事件处理程序似乎清除了所有三个文本框,因此我将尝试用一个按钮来回答清除的“选定的文本框”。

The problem I see here is what do you mean by selected text box? 我在这里看到的问题是所选文本框是什么意思? It may mean focused TextBox (which also allows editing if enabled and not readonly), but the focus is lost when you go to another control (ie when pushing a button). 这可能意味着聚焦的TextBox(如果启用了文本框,还允许进行编辑,但不是只读的),但是当您转到另一个控件时(即,按下按钮时),焦点就会丢失。

I think the most user friendly approach is to have a clear button next to each textbox and pushing it will clear that particular textbox. 我认为最用户友好的方法是在每个文本框旁边都有一个清除按钮,然后按该按钮将清除该特定文本框。 In order to keep your UI as clean as possible, the button should not contain text, but an image. 为了使您的UI尽可能整洁,该按钮不应包含文本,而应包含图像。 Check this for more details about doing this. 选中以获取有关执行此操作的更多详细信息。

Also, since you have three textboxes and possibly this number may grow in the future, you may consider creating a custom control, something like a " ClearableTextBox ". 另外,由于您有三个文本框,并且这个数字将来可能会增加,因此您可以考虑创建一个自定义控件,例如“ ClearableTextBox ”。 This control can simply have a TextBox and a button with an image capable of clearing the TextBox. 该控件可以简单地具有一个TextBox和一个带有能够清除TextBox的图像的按钮。 More information about how to create such a custom control (actually a composite one since you just put standard controls together) can be found here . 此处可以找到有关如何创建这样的自定义控件(实际上是一个复合控件,因为您只是将标准控件放在一起)的更多信息。

Here's one way: 这是一种方法:

  1. Place a private variable in your form to remember the last TextBox that was selected (aka focused). 在您的窗体中放置一个私有变量,以记住最后一个被选中的文本框(也称为焦点)。
  2. Wire up all the text boxes that you want to be able to execute "clear selected" on to the "Enter" event. 将您希望能够执行“清除选中”的所有文本框连接到“ Enter”事件。
  3. When the enter event occurs, set the value of the private variable to the sender that fired the event. 输入事件发生时,将私有变量的值设置为触发事件的发送者。
  4. When you click "Clear Selected", just call the .Clear() method on the item that was last selected. 当您单击“清除选定的内容”时,只需对最后选定的项目调用.Clear()方法。

And now some code: 现在有一些代码:

public partial class MyForm : Form
{
    private TextBox _lastSelected = null;

    public MyForm()
    {
        InitializeComponent();

        textBox1.Enter += textBox_Enter;
        textBox2.Enter += textBox_Enter;
        textBox3.Enter += textBox_Enter;
    }

    private void buttonClearAll_Click(object sender, EventArgs e)
    {
        textBox1.Clear();
        textBox2.Clear();
        textBox3.Clear();
    }

    private void buttonClearSelected_Click(object sender, EventArgs e)
    {
        if (_lastSelected == null) return;

        _lastSelected.Clear();
    }

    private void textBox_Enter(object sender, EventArgs e)
    {
        _lastSelected = (TextBox)sender;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM