0

How do I enable a button is one of my two radio buttons is checked, and textBox1 has a value?

I've tried this:

if (textBox1.Text != null && (radioButton1.Checked == true || radioButton2.Checked == true))
{
    button1.Enabled = true;
}
else
{
    button1.Enabled = false;
}

By the way, how can I limit the text box to receive only numbers?

davidsbro
  • 2,761
  • 4
  • 23
  • 33
avi12
  • 83
  • 1
  • 5

5 Answers5

3

If you are using C# winForms:

regarding using only numbers, I'd use a "Numeric Up-down" control because it also gives you the option of floating point numbers without the hassel of key checking and events and all those stuff and does all the background check-for you.

Therefore I'd go with:

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
    if ((numericUpDown1.Value > 0) && //or some verification on your input variable
        ((radioButton1.Checked) || (radioButton2.Checked)))
    {
        button1.Enabled = true;
    }
    else
    {
        button1.Enabled = false;
    }
}

By the way I am not sure why are using the radio buttons, if you want your user to choose one of the two options and you want to enable the button regardless of which radio button is being checked, I would use a combo-box and would enable the button based on my textbox (or numeric up/down) and just use the comboBox.SelectedIndex as the option.

After Edit:

Then, please have a look at this thread for numbers only in a text box:How do I make a textbox that only accepts numbers?

Community
  • 1
  • 1
AleX_
  • 508
  • 1
  • 6
  • 20
1

Use string.IsNullOrEmpty, Indicates whether the specified string is Nothing or an Empty string.

if (!string.IsNullOrEmpty(textBox1.Text) && (radioButton1.Checked || radioButton2.Checked ))
{
    button1.Enabled = true;
}
else
{
    button1.Enabled = false;
}

Personally I would like to use String.IsNullOrWhiteSpace

Satpal
  • 132,252
  • 13
  • 159
  • 168
  • Thanks a lot for your answer. The result isn't working exactly how I wanted. Only when the text box is filled more than once, the button is turning to be enabled. – avi12 Nov 11 '13 at 17:31
1
 if (!string.IsNullOrEmpty(textBox1.Text) && (radioButton1.Checked || radioButton2.Checked))
     button1.Enabled = true;
 else
     button1.Enabled = false;

As far as requiring a number, I'd go with a masked textbox, assuming you're using WinForms. I don't know the ASP equivalent.

Marginally beaten by Satpal :(

IsNullOrEmpty will check to make sure that it, as the name implies, is not null AND has something entered into it, so it's not blank aka an Empty String.

sab669
  • 3,984
  • 8
  • 38
  • 75
  • Thanks a lot for your answer. The result isn't working exactly how I wanted. Only when the text box is filled more than once, the button is turning to be enabled. – avi12 Nov 11 '13 at 17:32
  • @avi12 What do you mean by "filled more than once"? Put a breakpoint and step through it and make sure there's proper values for your Text and Checked states. – sab669 Nov 11 '13 at 17:33
1

Solution 1: this is solution for your first problem - Enabling or disabling the button based on Radio buttons.

if ((String.IsNullOrEmpty(textBox1.Text.ToString().Trim())) && (radioButton1.Checked  || radioButton2.Checked))
{
    button1.Enabled = true;
}
else
{
    button1.Enabled = false;
}

Solution 2: this is the solution for only accepting numbers in Textbox control you need to handle write the below code in TextBox KeyPress event.

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar))
                e.Handled = true;
        }
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
0

To make the text box take only numbers

Import this namespace

using System.Text.RegularExpressions;

then in the form load add

Regex numbers_only= new Regex("^[0-9]{3}$");

While checking for textbox not being null, also check if its only numbers

if(textBox1.Text != null & numbers_only.IsMatch(textBox1.Text))
{ your code }

note: the {3} is the length of number acceptable.

Cuado
  • 501
  • 1
  • 5
  • 13