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
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
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()). .Where(tb => tb.Enabled && !tb.ReadOnly). 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.
Jump to Post— Momerath 1,327Without 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 …
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;
}
thanks gyes this is what i was thinking
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.