[Solved] How to set values of text box using values of other text boxes [closed]


I think what you want is Control.Leave event or Control.KeyPress or you may also use Control.TextChanged as suggested by @LevZ…

Here is some code for Control.Leave Event:

TextBoxFirstNumber.Leave += TextBoxFirstNumber_Leave;
TextBoxSecondNumber.Leave += TextBoxSecondNumber_Leave;

void TextBoxFirstNumber_Leave(object sender, EventArgs e)
{
    if (TextBoxFirstNumber.Text != "" && TextBoxSecondNumber.Text != "")
    {
        TextBoxAnswer.Text = int.Parse(TextBoxFirstNumber.Text) * int.Parse(TextBoxSecondNumber.Text);
    }
}

void TextBoxSecondNumber_Leave(object sender, KeyPressEventArgs e)
{
    if (TextBoxFirstNumber.Text != "" && TextBoxSecondNumber.Text != "")
    {
        TextBoxAnswer.Text = int.Parse(TextBoxFirstNumber.Text) * int.Parse(TextBoxSecondNumber.Text);
    }
}

This will fill the TextBoxAnswer when the user puts his values in both of the TextBoxes and leaves it…

Here is some code for Control.KeyPress Event:

TextBoxFirstNumber.KeyPress += TextBoxFirstNumber_KeyPress;
TextBoxSecondNumber.KeyPress += TextBoxSecondNumber_KeyPress;

void TextBoxFirstNumber_KeyPress(object sender, EventArgs e)
{
    if (e.KeyChar == (char)Keys.Enter)
    {
        if (TextBoxFirstNumber.Text != "" && TextBoxSecondNumber.Text != "")
        {
            TextBoxAnswer.Text = int.Parse(TextBoxFirstNumber.Text) * int.Parse(TextBoxSecondNumber.Text);
        }
    }
}

void TextBoxSecondNumber_KeyPress(object sender, KeyPressEventArgs e)
{   
    if (e.KeyChar == (char)Keys.Enter)
    {
        if (TextBoxFirstNumber.Text != "" && TextBoxSecondNumber.Text != "")
        {
            TextBoxAnswer.Text = int.Parse(TextBoxFirstNumber.Text) * int.Parse(TextBoxSecondNumber.Text);
        }
    }
}

Now the user enters numbers in both TextBoxes and presses enter on any of them TextBoxAnswer will show product…

Here is some code for Control.TextChanged Event:

TextBoxFirstNumber.TextChanged += TextBoxFirstNumber_TextChanged;
TextBoxSecondNumber.TextChanged += TextBoxSecondNumber_TextChanged;

void TextBoxFirstNumber_TextChanged(object sender, EventArgs e)
{
    if (TextBoxFirstNumber.Text != "" && TextBoxSecondNumber.Text != "")
    {
        TextBoxAnswer.Text = int.Parse(TextBoxFirstNumber.Text) * int.Parse(TextBoxSecondNumber.Text);
    }
}

void TextBoxSecondNumber_TextChanged(object sender, KeyPressEventArgs e)
{
    if (TextBoxFirstNumber.Text != "" && TextBoxSecondNumber.Text != "")
    {
        TextBoxAnswer.Text = int.Parse(TextBoxFirstNumber.Text) * int.Parse(TextBoxSecondNumber.Text);
    }
}

Now as the user will enter the numbers for event it will trigger TextChanged event and TextBoxAnswer will show output.

You may also read this Official Documentation for Control Events and use the ones that suits your needs the most.

0

solved How to set values of text box using values of other text boxes [closed]