First of all sorry for my bad english. I'm beginner at C# and i made a Windows forms application but i can't disable one button if a textbox is empty. I tried some of the Enabled methods but they didn't work. Hope someone can help me fix this. Thank you very much
public partial class ModulusForm : Form
{
public double nje;
public double dy;
public double pergjigja;
public double rezultati;
public ModulusForm()
{
InitializeComponent();
Button btn = new Button();
btn.Click += new EventHandler(butoniGjenero_Click);
}
private void butoniPerfundo_Click(object sender, EventArgs e)
{
this.Close();
}
private void butoniGjenero_Click(object sender, EventArgs e)
{
Random random = new Random();
nje = random.Next(1, 100);
dy = random.Next(1, 100);
if (nje > dy)
{ textboxPyetja.Text = "X = " + nje + " " + "dhe" + " " + "Y = " + dy; }
else if (nje > dy)
{
nje = random.Next(1, 100);
dy = random.Next(1, 100);
}
rezultati = nje / dy;
}
private void butoniPastro_Click(object sender, EventArgs e)
{
textboxPyetja.Clear();
textboxPergjigja.Clear();
textboxPergjigjaSakt.Clear();
}
private void butoniVerteto_Click(object sender, EventArgs e)
{
try
{
pergjigja = double.Parse(textboxPergjigja.Text);
}
catch
{
var informim = MessageBox.Show("Rishiko fushat!", "Verejtje", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
if (textboxPergjigja.Text == "")
{
//nothin' baby
}
else
{
if (textboxPyetja.Text == "")
{
var informim = MessageBox.Show("Fusha e pyetjes eshte null!", "Verejtje", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (pergjigja == rezultati)
{
textboxPergjigjaSakt.Text = "Pergjigja eshte e sakte";
}
else
{
textboxPergjigjaSakt.Text = "Gabim." + " " + "Pergjigja e sakte eshte: " + "" + rezultati;
}
comboboxVargu.Items.Add(nje + " / " + dy + " = " + rezultati);
}
}
}
}
}
Hope it work!
Credit to @Cody Gray for already suggesting this; I have just expanded it, so you can see how to implement and how it works
Overview
You can wire up an event handler for when your
textboxPergjigja.Text
's text has changed.In the handler you create, you can then evaluate whether your button should be
Enabled
or not - using thestring.IsNullOrWhiteSpace()
check to set this.First:
In your constructor for the form, subscribe to the
textboxPergjigja.Text
text box'sTextChanged
event.Like this:
Next:
Add a handler that matches the correct delegate signature for that event.
Like this:
This way, whenever the text in the
textBoxPergjigja
text box is changed; the evaluation is run and your button will always be enabled/disabled correctly.Hope this helps! :)
Additional Info
You can also use
textBox.Text.IsNullOrEmpty()
, and it will still work - as suggested by @CodyI have used
string.IsNullOrWhiteSpace()
, as opposed totextBox.Text.IsNullOrEmpty()
for the following reasons:.IsNullOrEmpty()
method only checks if thetextBox.Text
is eithernull
or the total amount of characters is equal to 0.The problem this might pose is, if the user only enters a space in the textbox, it is no longer
Empty
ornull
; thus this check will returntrue
. If the logic of the program requires that an actual value be entered into the textbox, this logic can be flawed.string.IsNullOrWhiteSpace()
check will check on 3 conditions - if the inputstring
isnull
,Empty
and contains only whitespace characters (space, newline etc.), also.I hope this adds a little bit of extra fluff to give you an informed decision for future.
Try this:
Handle it on TextChanged event of your TextBox. (double click on your textbox control when in designed, which will automatically create the text changed event for you).
add an event for edit text in the txtbox. when the text changes, enable the button