Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
80 views
in Technique[技术] by (71.8m points)

How do I make a combobox selection changes do a calculation in C#

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think I have solution for you. You can use SelectedIndexChanged event for combobox. If you want update calculation on textchanged you can also use that update UI method. Please check the code->

public class MyType
{
    public string Name { get; set; }
    public int ID { get; set; }
}
void LoadData()
{
    List<MyType> list = new List<MyType>();
    list.Add(new MyType() { ID = 1, Name = "Centimeter" });
    list.Add(new MyType() { ID = 2, Name = "Meter" });
    list.Add(new MyType() { ID = 3, Name = "KiloMeter" });

    fromCB.DataSource = list.ToList();
    fromCB.DisplayMember = "Name";
    fromCB.ValueMember = "ID";

    toCB.DataSource = list.ToList();
    toCB.DisplayMember = "Name";
    toCB.ValueMember = "ID";
}
private void fromCB_SelectedIndexChanged(object sender, EventArgs e)
{
    UpdateUI();
}
private void toCB_SelectedIndexChanged(object sender, EventArgs e)
{
    UpdateUI();
}
void UpdateUI()
{
    if (fromCB.SelectedItem != null && toCB.SelectedItem != null)
    {
        var fromID = ((MyType)fromCB.SelectedItem).ID;
        var toID = ((MyType)toCB.SelectedItem).ID;
        if (!string.IsNullOrEmpty(fromTB.Text) && !string.IsNullOrWhiteSpace(fromTB.Text))
        {
            float value = -1;
            if (float.TryParse(fromTB.Text, out value))
            {
                if (fromID == toID)
                {
                    toTB.Text = value.ToString();
                }
                else if (fromID == 1 && toID == 2)
                {
                    toTB.Text = (value / 100).ToString();
                }
                else if (fromID == 1 && toID == 3)
                {
                    toTB.Text = (value / 100000).ToString("0.#########");
                }
                else if (fromID == 2 && toID == 1)
                {
                    toTB.Text = (value * 100).ToString();
                }
                else if (fromID == 2 && toID == 2)
                {
                    toTB.Text = value.ToString();
                }
                else if (fromID == 2 && toID == 3)
                {
                    toTB.Text = (value * 0.001).ToString();
                }
                else if (fromID == 3 && toID == 1)
                {
                    toTB.Text = (value * 100000).ToString();
                }
                else if (fromID == 3 && toID == 2)
                {
                    toTB.Text = (value * 1000).ToString();
                }
                else if (fromID == 2 && toID == 3)
                {
                    toTB.Text = value.ToString();
                }
            }
            else
            {
                MessageBox.Show("Input is not a number");
            }
        }
    }
}

Note: This is little bit long code. Please check if I missed something.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...