0
You'll need to have some way of deciding whether the number selected in the ComboBox should go in the first or the second TextBox. Try this:
private bool useSecond = false;
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == -1) return;
string selected = comboBox1.SelectedItem.ToString();
if (!useSecond)
{
textBox1.Text = selected;
textBox2.Text = "";
textBox3.Text = "";
}
else
{
int number1 = int.Parse(textBox1.Text);
int number2 = int.Parse(selected);
textBox2.Text = number2.ToString();
textBox3.Text = (number1 * number2).ToString();
}
useSecond = !useSecond;
}
Accepted 0
how to make when i select two different number from combobox and this will be displayed in two different textbox, once it displayed both number should get multiplied and should display in third textbox
0
In that case you don't need to handle any textbox events at all - you can do everything in the comboBox's SelectedIndexChanged eventhandler and the code is simpler because you
know that the selected item will be an integer:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex > -1)
{
int i = int.Parse(comboBox1.SelectedItem.ToString());
textBox1.Text = i.ToString();
int j = i * 2; // or whatever
textBox2.Text = j.ToString();
}
}
0
I have a combobox and it contains items like 1,2,3,4,5
when i select the 3 from the combobox that value should display in the textbox1 and its answer display in textbox2
Is this correct??????
I am selecting the value from combo but not displaying in the textbox1
private void textBox1_Leave(object sender, EventArgs e)
{
double d;
bool isValid = double.TryParse(textBox1.Text, out d);
if (!isValid)
{
MessageBox.Show("Not a valid number. Please amend");
textBox1.Focus();
return;
}
d = 2 * d; // or any other calculation
textBox2.Text = d.ToString();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.textBox1.Text = this.comboBox1.Text.ToString();
}
0
Hi Rashmi,
Another way
private void textBox1_TextChanged(object sender, EventArgs e)
{
decimal a, b;
a = Convert.ToDecimal(textBox1.Text);
b = a * 2;
textBox2.Text = b.ToString();
}
Thanks
0
Assuming you want to do the calculation after the whole value has been entered in the first textbox (rather than as each character is entered), then I'd handle its Leave event on the following lines:
private void textBox1_Leave(object sender, EventArgs e)
{
double d;
bool isValid = double.TryParse(textBox1.Text, out d);
if (!isValid)
{
MessageBox.Show("Not a valid number. Please amend");
textBox1.Focus();
return;
}
d = 2 * d; // or any other calculation
textBox2.Text = d.ToString();
}