Member Avatar for Member #837239

Hi gyes

my problem is this:i have some textboxes and one button that is disabled and i want when all the textboxes are filled than the button to become active or enabled

Dani AI

Generated

Good start and — both answers point the OP in the right direction. For a bit more robustness and to avoid hard‑coding textboxes, collect the textboxes dynamically, ignore whitespace-only entries, and handle controls nested inside panels/groupboxes.

A compact WinForms pattern (uses LINQ and a small recursive control enumerator):

using System.Linq;

IEnumerable<Control> GetAllControls(Control parent)
{
    foreach (Control c in parent.Controls)
    {
        yield return c;
        foreach (var child in GetAllControls(c))
            yield return child;
    }
}

private void Form1_Load(object sender, EventArgs e)
{
    foreach (var tb in GetAllControls(this).OfType<TextBox>())
        tb.TextChanged += (o, ea) => UpdateButtonState();
    UpdateButtonState();
}

private void UpdateButtonState()
{
    bool allFilled = GetAllControls(this)
        .OfType<TextBox>()
        .All(tb => !string.IsNullOrWhiteSpace(tb.Text)); // .NET 4+
    button1.Enabled = allFilled;
}

Notes and quick tips:

  • String.IsNullOrWhiteSpace requires .NET 4+. For older frameworks use string.IsNullOrEmpty(tb.Text.Trim()).
  • Skip read‑only/disabled boxes with .Where(tb => tb.Enabled && !tb.ReadOnly).
  • Call the updater on form load so a prefilled form sets the button state.
  • If certain fields are optional, filter them out instead of checking every textbox.

If the original intent was a web form (thread tagged javascript), use the input event and trim() to ignore whitespace. Example approach: attach listeners to inputs, check every value, and toggle the submit button. This provides immediate feedback and complements server-side validation.

Recommended Answers

All 3 Replies

Without knowing anything about how you've named and collected your textboxes you could do something like this (and yes, there are better ways):

private void EnableButton() {
    Boolean enable = true;

    if (TextBox1.Text.Length == 0) enable = false;
    if (TextBox2.Text.Length == 0) enable = false;
    if (TextBox3.Text.Length == 0) enable = false;
    // repeat until all the textboxes you want to check are done;

    Button1.Enabled = enable;
}

Try this code. It will enable button as soon as all buttons will have some text inside.

public Form1()
        {
            InitializeComponent();
            button1.Enabled = false;
            TextBox[] tbs = { textBox1, textBox2, textBox3 }; //put all the textBoxes you want in here
            foreach (TextBox tb in tbs)
                tb.TextChanged += new EventHandler(tb_TextChanged);
        }

        private void tb_TextChanged(object sender, EventArgs e)
        {
            EnablingButton();
        }

        private void EnablingButton()
        {
            TextBox[] tbs = { textBox1, textBox2, textBox3 };//put all the textBoxes you want in here
            bool bEnableButton = true;
            foreach (TextBox tb in tbs)
            {
                if (tb.Text.Equals(String.Empty))
                {
                    bEnableButton = false;
                    break;
                }
            }
            if (bEnableButton)
                button1.Enabled = true;
            else
                button1.Enabled = false;
        }
Member Avatar for Member #837239

thanks gyes this is what i was thinking

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.