What I am trying to do is , I have made a Unit Converter. It has 2 comboBoxes (fromCb and toCB )and 2 Textboxes (fromTb and toTb ). What I want to do is for example if I select Kilometers in fromCB and enter a value in fromTB , and when I select Meters in toCB , I want the value to be calculated and shown in toTB as soon as I select Meters in toCB. With the code below the value is calculated if both the selections(Kilometers and meters are already made) and then I type a value in the fromTB. I want to make only one selection that is only the fromCB selection (Kilometers)and type value for it and the calculation should be done whenever I change the toTB selections(Meters , centimeters).
EDIT —
My current program runs this way :
1- I select Kilometers to convert from
2- I select Meters to convert to
3 - I put value in Convert from textbox
4 - The value is converted and shown in convert to textbox
I have to select both the selections first and then it converts.
How I want it to run :
1- I select Kilometers to convert from
2- I put the value to convert from
3- I select meters / centimetres to convert to ( I can change it continuously and I want the value to be converted with it )
4- Value is converted and shown in convert to textbox
Here I want to select one selection , put the value and the conversion should show as I change the second selection
EDIT 2-
Ok see my code is working it has no error , its just not working the way i want to. What I want to do is. I want to select kilometers in fromCB and then put a value in fromTB. Now this is my input. Now for my output as soon as I select any selection (for example - meters) in toCB , the value should populate automatically into toTB , and if I change my output selection (for example centimeters) , it should populate automatically in toTB again without altering the input. What my code is doing is , I have to select both selections(Km and cm) and then when I input any value , the output is populated and if i want to change the output selection(for example to meters) the output value does not change automatically , I have to input the value again
if (fromCB.SelectedItem == "Kilometers [km]")
{
if (fromTB.Text == "")
{
MessageBox.Show("Please enter a value to convert");
}
if(toCB.SelectedItem=="Meters [m]")
{
float tb1;
float tb2;
tb1 = int.Parse(fromTB.Text);
tb2 = tb1 * 1000;
toTB.Text = tb2.ToString();
}
question from:
https://stackoverflow.com/questions/66052234/how-do-i-make-a-combobox-selection-changes-do-a-calculation-in-c-sharp 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…