Johny Deff
Johny Deff

Reputation: 1

Textbox difficulty

I am trying to get data from two TextBox fields and add them to a third TextBox using a simple button. It can be done easily.

Where I got stuck is a scenario like this. The button may first be disabled as nothing is provided in the text fields, and get enabled as the user type any digit in the text field.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        if (textBox1.Text != null)
        {
            button1.Enabled = true;
        }
        else
        {
            button1.Enabled = false;
        }
    }
    private void button1_Click(object sender, EventArgs e)
    {   
       int a = Convert.ToInt32(textBox1.Text);
       int b = Convert.ToInt32(textBox2.Text);
       textBox3.Text = (a + b).ToString();
    }
}

Upvotes: 0

Views: 50

Answers (2)

Pyromancer
Pyromancer

Reputation: 2509

I wonder why did you put your codes under Form1, the code will just activate after the form has loaded. To fix this you first need to create a textBox1_TextChange Event. Don't forget to disable the button in the property. enabled=false Like this:

private void textBox1_TextChanged(object sender, EventArgs e)

You place the code you write on that event block.

To trigger the button when the 2 textBoxes already has numbers. use this:

select the two textboxes and on the event tab beside the properties look for KeyPress then name it as numbersTB_KeyPress

private void numbersTB_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (sender as TextBox != null)
            {
                if (char.IsDigit(e.KeyChar))
                    button1.Enabled = true;
                else
                    button1.Enabled = false;
            }
        }

Upvotes: 0

Chief Wiggum
Chief Wiggum

Reputation: 2934

Something like that should do the trick (but it's not very elegant):

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        this.SetButtonEnableState();
    }

    private void SetButtonEnableState()
    {
        button1.Enabled = !(string.IsNullOrWhiteSpace(textBox1.Text) || string.IsNullOrWhiteSpace(textBox2.Text));

    }

    private void button1_Click(object sender, EventArgs e)
    {
        int a = Convert.ToInt32(textBox1.Text);
        int b = Convert.ToInt32(textBox2.Text);
        textBox3.Text = (a + b).ToString();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        this.SetButtonEnableState();
    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        this.SetButtonEnableState();
    }
}

Update:

In your case you might want to check if the textbox values are actually int values and if this is the case, then enable the button. So why not update the method above with this?

    private void SetButtonEnableState()
    {
        int n;

        button1.Enabled = int.TryParse(textBox1.Text, out n) && int.TryParse(textBox2.Text, out n);
    }

Upvotes: 1

Related Questions