ExCluSiv3
ExCluSiv3

Reputation: 184

Canceling textboxes if I type in other textbox

I got few textboxes in my form. I would like to type in one textbox and the other textboxes will be automatically canceled (Canceled so the user wont be able to type anymore in the other textboxes). If the user delete the word he typed in the textbox (or in other words: if the textbox is empty) all other textboxes will be enabled again.

Here is what I got so far:

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (textBox1.Text.Trim().Length > 0)
        {
            // Disable other textboxes (I have no idea how can I do that).
        }
        else if (textBox1.Text.Trim().Length == 0)
        {
            // Enable other textboxes
        }
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (textBox1.Text.Trim().Length > 0)
        {
            // Keep this comboBox enabled while typing in textBox1.
        }
    }

How can I disable other textboxes while typing in priticillar textbox? (also keeping comboBox enabled).

DO NOTICE: I would like to do the same on textBox2 (when I type in textBox2, textbox 1 and 3 will be disabled) and textBox3.

Upvotes: 0

Views: 125

Answers (2)

WhoIsRich
WhoIsRich

Reputation: 4163

You don't need a condition on your 'else if', just 'else' will do, as if the length is more than 0, the only other possibility is 0. Also you can use the sender instead of hardcoding the control name.

Then set the Enabled property for the textbox's you want disabled. You can loop through all the textbox's on the form, excluding the one you are typing into, or just manually list them. SImpler is putting the textbox's in a groupbox, then if you disable the groupbox it will disable the controls with in it.

private void textBox1_TextChanged(object sender, EventArgs e)
{
    var senderTextBox    = (TextBox)sender;
    var textBoxesEnabled = senderTextBox.Text.Trim().Length == 0;

    textBox2.Enabled = textBoxesEnabled;
    textBox3.Enabled = textBoxesEnabled;

    // OR

    groupBox1.Enabled = textBoxesEnabled;
}

REPLY EDIT: You can chain of textbox's, say 4 of them, disable the last 3, then:

void TextBox1TextChanged(object sender, System.EventArgs e)
{
    var isTextEmpty  = ((TextBox)sender).Text.Trim() == "";
    textBox2.Enabled = !isTextEmpty;
}
void TextBox2TextChanged(object sender, System.EventArgs e)
{
    var isTextEmpty  = ((TextBox)sender).Text.Trim() == "";
    textBox1.Enabled = isTextEmpty;
    textBox3.Enabled = !isTextEmpty;
}
void TextBox3TextChanged(object sender, System.EventArgs e)
{
    var isTextEmpty  = ((TextBox)sender).Text.Trim() == "";
    textBox2.Enabled = isTextEmpty;
    textBox4.Enabled = !isTextEmpty;
}
void TextBox4TextChanged(object sender, System.EventArgs e)
{
    var isTextEmpty  = ((TextBox)sender).Text.Trim() == "";
    textBox3.Enabled = isTextEmpty;
}

But for a large amount of textbox's, another alternative is having multiple textbox share the same TextChanged event. You need to click on each TextBox control, go into the Events list and manually select the method for TextChanged. Here is the method:

private void TextBoxGroup_TextChanged(object sender, EventArgs e)
{
    var groupOrder    = new List<TextBox>() { textBox1, textBox2, textBox3, textBox4 };
    var senderTextBox = (TextBox)sender;
    var senderIndex   = groupOrder.IndexOf(senderTextBox);

    var isTextEmpty = senderTextBox.Text.Trim() == "";
    if (senderIndex != 0) groupOrder[senderIndex - 1].Enabled = isTextEmpty;
    if (senderIndex != groupOrder.Count - 1) groupOrder[senderIndex + 1].Enabled = !isTextEmpty;
}

Upvotes: 1

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101681

Use Controls collection and some LINQ:

if (textBox1.Text.Trim().Length > 0)
{ 
     var textboxes = this.Controls.OfType<TextBox>().Where(x => x.Name != "textBox1");
     foreach(var tBox in textboxes)
         tBox.Enabled = false;
}

Upvotes: 1

Related Questions