简体   繁体   中英

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).

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. If I click it one more time it then clears the FirstOperatorTextBox.

How would I clear all 3 text-boxes with 1 button, and only clear selected with another button?

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.

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).

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. 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 ". This control can simply have a TextBox and a button with an image capable of clearing the 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.
  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.

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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