如何为每个组合框元素分配一个整数值 - c#

时间:2022-09-27 23:15:27

I am making a program that calculates energy usage. I have a combobox of household appliances. When a appliance is selected I want a power rating to be assigned to a variable to then calculate the total energy usage. How do I go about it? Should I double click the combobox and write some appropriate code in the method?

我正在制作一个计算能源使用量的计划。我有一个家用电器组合框。选择设备时,我希望为变量分配额定功率,然后计算总能耗。我该怎么办呢?我应该双击组合框并在方法中编写一些适当的代码吗?

This is a windows form application. All I have done is made a combobox from the toolbox using the design view. I then populated the combobox by clicking Properties>Items. The comboxbox contains one drop down that contains hours (00, 01, 02, 03 etc) and another contains minutes (00, 15, 30, 45). I want to get the values from the combobox and do calculations with them.

这是一个Windows窗体应用程序。我所做的就是使用设计视图从工具箱中制作组合框。然后,我通过单击“属性”>“项目”填充组合框。 comboxbox包含一个包含小时(00,01,02,03等)的下拉列表,另一个包含分钟(00,15,30,45)。我想从组合框中获取值并用它们进行计算。

2 个解决方案

#1


So i tried to make what you asked for

所以我试着做出你要求的东西

Here i have my forms load event im just setting the datasource of the hours and minutes comboboxes

这里我有我的表单加载事件我只是设置小时和分钟组合框的数据源

private void Form1_Load(object sender, EventArgs e)
{
        var hours = new int[] {1,2,3,4,5,6,7,8,9,10,11,12};
        var mins = new int[] { 0, 15, 45};
        hoursComboBox.DataSource = hours;
        minsComboBox.DataSource = mins;
}

Create your powerCalculationMethod

创建powerCalculationMethod

private int CalculatePower(int hours,int mins)
    {
        //do calculation
        int calc = 0;
        return calc;
    }

Then maybe after selecting the values in the comboboxes you could have a button click and on its click event Convert the values of the comboboxes to integers because thats the parameters the method expects and do your calculation

然后可能在选择组合框中的值后,您可以单击按钮并单击其单击事件将组合框的值转换为整数,因为这是方法所需的参数并进行计算

private void button1_Click(object sender, EventArgs e)
    {
        //set label's text to the calculated power
        //convert the values of the comboboxes to integers so that you could do calculations with them
        //and because the calculatepower method is expecting ints
        //tostring at end to set labels text to a string
        label1.Text = CalculatePower(Convert.ToInt32(this.hoursComboBox.SelectedValue), Convert.ToInt32(this.minsComboBox.SelectedValue)).ToString();
    }

Maybe this gives you an idea of how you can accomplish what you are trying to do

也许这可以让您了解如何完成您想要做的事情

#2


I think the hardest part is to convert the number of minutes from a 60-based number into a 10-based number here is a global solution:

我认为最困难的部分是将基于60的数字转换为基于10的数字,这是一个全球解决方案:

Dictionary<string, double> dictionaryValues = new Dictionary<string, double>();

private void Form1_Load(object sender, EventArgs e)
{
  comboBoxApplicances.Items.Add("Air conditioning");
  comboBoxApplicances.Items.Add("Attic fan");
  comboBoxApplicances.Items.Add("Ceiling fan");
  comboBoxApplicances.Items.Add("Dishwasher");
  comboBoxApplicances.SelectedIndex = 0;

  dictionaryValues.Add("Air conditioning", 0);
  dictionaryValues.Add("Attic fan", 0);
  dictionaryValues.Add("Ceiling fan", 0);
  dictionaryValues.Add("Dishwasher", 0);

  for (int i = 0; i < 24; i++)
  {
    comboBoxHours.Items.Add(i < 10 ? "0" + i : i.ToString()); // add 0 for i < 10
  }

  comboBoxHours.SelectedIndex = 0;
  comboBoxMinutes.Items.Add("00");
  comboBoxMinutes.Items.Add("15");
  comboBoxMinutes.Items.Add("30");
  comboBoxMinutes.Items.Add("45");
  comboBoxMinutes.SelectedIndex = 0;
}

private void buttonCompute_Click(object sender, EventArgs e)
{
  // adding value to the selected appliance
  dictionaryValues[comboBoxApplicances.SelectedItem.ToString()] +=
    GetHoursMinutes(comboBoxHours.SelectedItem.ToString(),
    comboBoxMinutes.SelectedItem.ToString());
  labelCurrentApplicance.Text = dictionaryValues[comboBoxApplicances.SelectedItem.ToString()].ToString();
}

private double GetHoursMinutes(string hours, string minutes)
{
  double result = 0;
  double minutesB60 = double.Parse(minutes);
  double minutesB10 = (minutesB60 / 60);
  result = double.Parse(hours) + minutesB10;
  return result;
}

#1


So i tried to make what you asked for

所以我试着做出你要求的东西

Here i have my forms load event im just setting the datasource of the hours and minutes comboboxes

这里我有我的表单加载事件我只是设置小时和分钟组合框的数据源

private void Form1_Load(object sender, EventArgs e)
{
        var hours = new int[] {1,2,3,4,5,6,7,8,9,10,11,12};
        var mins = new int[] { 0, 15, 45};
        hoursComboBox.DataSource = hours;
        minsComboBox.DataSource = mins;
}

Create your powerCalculationMethod

创建powerCalculationMethod

private int CalculatePower(int hours,int mins)
    {
        //do calculation
        int calc = 0;
        return calc;
    }

Then maybe after selecting the values in the comboboxes you could have a button click and on its click event Convert the values of the comboboxes to integers because thats the parameters the method expects and do your calculation

然后可能在选择组合框中的值后,您可以单击按钮并单击其单击事件将组合框的值转换为整数,因为这是方法所需的参数并进行计算

private void button1_Click(object sender, EventArgs e)
    {
        //set label's text to the calculated power
        //convert the values of the comboboxes to integers so that you could do calculations with them
        //and because the calculatepower method is expecting ints
        //tostring at end to set labels text to a string
        label1.Text = CalculatePower(Convert.ToInt32(this.hoursComboBox.SelectedValue), Convert.ToInt32(this.minsComboBox.SelectedValue)).ToString();
    }

Maybe this gives you an idea of how you can accomplish what you are trying to do

也许这可以让您了解如何完成您想要做的事情

#2


I think the hardest part is to convert the number of minutes from a 60-based number into a 10-based number here is a global solution:

我认为最困难的部分是将基于60的数字转换为基于10的数字,这是一个全球解决方案:

Dictionary<string, double> dictionaryValues = new Dictionary<string, double>();

private void Form1_Load(object sender, EventArgs e)
{
  comboBoxApplicances.Items.Add("Air conditioning");
  comboBoxApplicances.Items.Add("Attic fan");
  comboBoxApplicances.Items.Add("Ceiling fan");
  comboBoxApplicances.Items.Add("Dishwasher");
  comboBoxApplicances.SelectedIndex = 0;

  dictionaryValues.Add("Air conditioning", 0);
  dictionaryValues.Add("Attic fan", 0);
  dictionaryValues.Add("Ceiling fan", 0);
  dictionaryValues.Add("Dishwasher", 0);

  for (int i = 0; i < 24; i++)
  {
    comboBoxHours.Items.Add(i < 10 ? "0" + i : i.ToString()); // add 0 for i < 10
  }

  comboBoxHours.SelectedIndex = 0;
  comboBoxMinutes.Items.Add("00");
  comboBoxMinutes.Items.Add("15");
  comboBoxMinutes.Items.Add("30");
  comboBoxMinutes.Items.Add("45");
  comboBoxMinutes.SelectedIndex = 0;
}

private void buttonCompute_Click(object sender, EventArgs e)
{
  // adding value to the selected appliance
  dictionaryValues[comboBoxApplicances.SelectedItem.ToString()] +=
    GetHoursMinutes(comboBoxHours.SelectedItem.ToString(),
    comboBoxMinutes.SelectedItem.ToString());
  labelCurrentApplicance.Text = dictionaryValues[comboBoxApplicances.SelectedItem.ToString()].ToString();
}

private double GetHoursMinutes(string hours, string minutes)
{
  double result = 0;
  double minutesB60 = double.Parse(minutes);
  double minutesB10 = (minutesB60 / 60);
  result = double.Parse(hours) + minutesB10;
  return result;
}