C# How to disable button when the value is zero - c#

I'm a beginner to C# programming and I really need some help :)
I made 2 buttons that will increment or decrement the value in the text box which is initially zero.
private void bminus_Click(object sender, EventArgs e)
{
NUMBER--;
textBox2.Text = NUMBER.ToString();
}
private void bplus_Click(object sender, EventArgs e)
{
NUMBER++;
textBox2.Text = NUMBER.ToString();
}
Will there be a way that the decrement button will be disabled if the value is 0 so that there won't be negative numbers? Thank you, I'd really appreciate anyone's help! :)

private void bminus_Click(object sender, EventArgs e)
{
NUMBER--;
textBox2.Text = NUMBER.ToString();
if(NUMBER == 0){
bminus.Enabled = false;
}
}
private void bplus_Click(object sender, EventArgs e)
{
NUMBER++;
textBox2.Text = NUMBER.ToString();
bminus.Enabled = true;
}
That should do.

You can just directly insert logical state NUMBER not being 0 to your textBox2 Enabled property:
textBox2.Enabled = NUMBER != 0;
Or make a separate function from this:
private bool IsNotZero(double n)
{
return n != 0;
}
then:
textBox2.Enabled = IsNotZero(NUMBER);

All you need to do is add the following to the textBox2.TextChanged event handler and it should work with your current code.
private void textBox2_TextChanged(object sender, EventArgs e)
{
int output;
if (int.TryParse(textBox2.Text, out output))
{
bminus.Enabled = int.Parse(textBox2.Text) > 0;
}
}
NOTE: No one here has specified checking for an integer so I added int.TryParse and int.Parse for this purpose.

Button have a property named Enabled which you can set to true or false based on your logic.

Check the value using if condition. If the value is null or zero you can use button.enabled = false; code.if(value == zero || value == null) { button.enabled=false;} else {button.enabled = true;}

Related

Decrementing a double value using a button puts it back to 0. C#

I am using a text box to show quantity. Using 2 buttons I want to increment or decrements a value. So far increment works but when I try to decrement the same value it puts it back to 0 instead of going from 5 to 4.
private void button3_Click(object sender, EventArgs e)
{
double val = double.Parse(textBox1.Text);
while (val != 0)
{
val--;
}
textBox1.Text = val.ToString();
}
This is the code I am using.
You are using a while loop. That means it will decrement the value until it becomes 0.
Change your while to an if and it should work as expected.
private void button3_Click(object sender, EventArgs e)
{
double val = double.Parse(textBox1.Text);
if (val != 0)
val--;
textBox1.Text = val.ToString();
}
You are using while construction that does val-- util it is 0
In your case you simply need to do this:
private void button3_Click(object sender, EventArgs e) {
double val = double.Parse(textBox1.Text);
if (val > 0)
val--;
textBox1.Text = val.ToString();
}
In addition I recommend you to use double.TryParse method. So you don't need to add try ... catch statement:
private void button3_Click(object sender, EventArgs e) {
double val;
bool perseOk = double.TryParse(textBox1.Text, out val);
if (perseOk && val > 0) {
val--;
} else {
val = 0;
}
textBox1.Text = val.ToString();
}

Fill a textbox based on another textbox text

I have TextBoxA and TextBoxB. What i want to do is , whenever i put a number (yes, both of the textboxes values are always integers) in TextBoxA , TextBoxB should "autocomplete" with value (100-TextBoxA). Same thing goes for TextBoxB. The sum of TextBoxA and TextBoxB should always be 100.
Here's what i've already tried:
static void TextBoxA_TextChanged()...
{
int a = Convert.ToInt32(TextBoxA.Text);
int b = Convert.ToInt32(TextBoxB.Text);
string text = (100-a).ToString();
TextBoxB.Text = text;
}
Static void TextBoxB_TextChanged()...
{
int a = Convert.ToInt32(TextBoxA.Text);
int b = Convert.ToInt32(TextBoxB.Text);
string text = (100-b).ToString();
TextBoxA.Text = text;
}
But it doesn't work.
Here's what you can try:
private void TextBoxA_TextChanged(object sender, EventArgs e)
{
int num = 0;
if (int.TryParse(TextBoxA.Text, out num))
{
string text = (100 - num).ToString();
TextBoxB.Text = text;
}
}
private void TextBoxB_TextChanged(object sender, EventArgs e)
{
int num = 0;
if (int.TryParse(TextBoxB.Text, out num))
{
string text = (100 - num).ToString();
TextBoxA.Text = text;
}
}
This will autocomplete on either TextBox on TextChanged Event.
First, i dont know why your event handlers are declared static.. its usually got to be :
private void TextBoxA_TextChanged(object sender, EventArgs e) { }
Secondly, you know if you have 2 textboxs, and each one triggers the other, you'll never go out of the TextChanged event.
To understand me more, here's an example :
1- You set TextBoxB.text = "1";2- TextBoxB.TextChanged triggers, it
sets TextBoxA.Text = "2"; 3- TextBoxA.TextChanged triggers, it sets
TextBoxB.Text = "1";
And it continues like this until i believe you'll get an Exception of memory.
EDIT : The opertator '-' works on numbers. You can't substract a number from a string. they have to be both numbers, so convert them first.
EDIT 2 :
Here's a code i wrote that works fine
private void textBox1_TextChanged(object sender, EventArgs e)
{
int n;
if (int.TryParse(textBox1.Text, out n)) // Check if the text value is a number
{
if (n > 100) // Since you want a sum of 100
return;
int m = 100 - n; // remaining
if (textBox2.Text != m.ToString()) // to not re-trigger the TextChanged event
textBox2.Text = m.ToString();
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
int n;
if (int.TryParse(textBox2.Text, out n)) // Check if the text value is a number
{
if (n > 100) // Since you want a sum of 100
return;
int m = 100 - n; // remaining
if (textBox1.Text != m.ToString()) // to not re-trigger the TextChanged event
textBox1.Text = m.ToString();
}
}
Try this. This is a more efficient and elegant way I'd choose using lambda expressions, without repeating the method:
private void onChangeDoSum(object sender, EventArgs e,
TextBox substractNumber, TextBox sumNumber)
{
sumNumber.Text = (100 - Int32.Parse(substractNumber.Text)).ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.TextChanged += (a, b) => onChangeDoSum(sender, e, textBox1, textBox2);
textBox2.TextChanged += (a, b) => onChangeDoSum(sender, e, textBox2, textBox1);
}
Alternatively use Int32.TryParse to prevent unexpected results.
I think you can do something like this
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Text = (100 - Int32.Parse(textBox1.Text)).ToString();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
textBox1.Text = (100 - Int32.Parse(textBox2.Text)).ToString();
}
when I see that static word I think you have tried to create these method by yourself and you forgot to add
textBox1.TextChanged += textBox1_Changed;
// I saw other post where you wrote that it could raise exception and fall. Of course it can. You can use if condition like
textBox1.Text != null && textBox1.Text != ""
before value would be changed or TryParse() method

Validate number INTEGER on text boxes IN windows forms issue

i have textbox on a windows form and i want to be insert only numbers in this textbox.i use the following c# code for that
private void ChildAge_TextChanged(object sender, EventArgs e)
{
int i;
if (!int.TryParse(ChildAge.Text, out i))
{
MessageBox.Show("Plaese enter a valid Age");
}
}
it is working, but the problem is that , after showing the Message, when i Backspace the content and text box become null, on that situation also this message box shows again.
Do a little test like bellow:
int i = 0;
if(!string.IsNullOrEmpty(ChildAge.Text) &&
!int.TryParse(ChildAge.Text, out i)
)
{
MessageBox.Show("Enter Valid Age");
}
Try Using Below. This works better.
bool m_BackPressed = false;
private void ChildAge_TextChanged(object sender, EventArgs e)
{
int i;
if (!m_BackPressed)
{
if (!int.TryParse(ChildAge.Text, out i))
{
MessageBox.Show("Plaese enter a valid Age");
}
}
}
private void ChildAge_KeyPress(object sender, KeyPressEventArgs e)
{
m_BackPressed = (e.KeyChar.Equals((char)Keys.Back)) ? true : false;
}

unable to clear a textbox data

I'm not able to delete textbox data with the code below
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(char.IsDigit(e.KeyChar)==false)
{
count++;
}
if (count == 1)
{
textBox1.Text = ("");
count = 0;
}
}
tried using clear method as well the alphabet i entered stays in the textbox and when i type any key it get overwritten but i want the textbox to be empty the second time and the prev data to be removed
you just need to say you've handled the event:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsDigit(e.KeyChar) == false)
{
count++;
}
if (count == 1)
{
textBox1.Text = ("");
count = 0;
e.Handled = true; // this bit fixes it
}
}
use textBox1.Text = ""; OR textBox1.clear();
This will clear your textbox.
You are doing it wrong. You can just paste in a bunch of letters with Ctrl+V. Delete the KeyDown event and create a TextChanged event. This code should accomplish what you are attempting. Please tell me if there is any more details and I will add to my answer.
private void textBox1_TextChanged(object sender, EventArgs e)
{
foreach (char c in textBox1.Text)
if (!char.IsDigit(c)) { textBox1.Clear(); break; }
}
Add this to your text box key press event your problem will be solved
e.handle = true;

To Disable Negative Values Inside a TextBox

I m Working On A windows Form.. I Need my TextBox Not To Accept negative Values ..How Can I Do this..
IS There Any Property Availiable For Doing The same...
You need to write keypress event of textbox like :
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
You can also user numeric updown control to prevent negetive values.
UPDATE :
Ref: Sai Kalyan Akshinthala
My code will not handle the case of copy/paste. User can enter negative values by copy/paste. So I think Sai Kalyan Akshinthala's answer is correct for that case except one small change of Length >= 2.
private void textBox1_TextChanged(object sender, EventArgs e)
{
if(textBox1.Text.Length >= 2)
{
int acceptednumber = Convert.ToInt32(textBox1.Text);
if(acceptednumber < 0)
{
textBox1.Text = "";
MessageBox.Show("-ve values are not allowed");
}
else
{
textBox1.Text = textBox1.Text;
}
}
}
yes you can do write the following code part in textchanged event of textbox
if(textBox1.Text.Length >= 2)
{
int acceptednumber = Convert.ToInt32(textBox1.Text);
if(acceptednumber < 0)
{
textBox1.Text = "";
MessageBox.Show("-ve values are not allowed");
}
else
{
textBox1.Text = textBox1.Text;
}
}
just use min and pattern will not allow to enter a minus value
min="0" pattern="^[0-9]+$" in input type

Categories

Resources