如何在WinForms的click事件方法中使用组合框中的值?

nnsrf1az  于 2022-11-16  发布在  其他
关注(0)|答案(2)|浏览(147)

所以首先,我是非常新的编程,这是我第一次张贴所以抱歉提前!
因此,我在下面发布我的更新代码,并删除了以前发布的信息,以避免混淆我在问什么,因为我现在意识到,我原来问了错误的问题。
我需要ComboBox选项可以被PostBtn_Click访问。一旦我单击按钮,我需要获取所选的值,并在Forecast类的CalculateWindChill()方法中使用它。我的大多数尝试都导致错误,告诉我该名称在当前上下文中不存在。

private void PostBtn_Click(object sender, EventArgs e)
    {

        Forecast currentForecast = new Forecast();
        {
            predicted_LoTemp = 
            Convert.ToInt32(PredictedLoTemp.SelectedItem);
            Wind_Speed = Convert.ToInt32(WindSpeed.SelectedItem);

            HiTempLbl.Text = 
            Convert.ToString(PredictedHiTemp.SelectedItem);
            LoTempLbl.Text = 
            Convert.ToString(PredictedLoTemp.SelectedItem);
            WindSpeedLbl.Text = 
            Convert.ToString(WindSpeed.SelectedItem);

            WindChillLbl.Text = 
            Convert.ToString(currentForecast.CalculateWindChill());
        }
    }       
    public class Forecast
    {
        public int Predicted_LoTemp { get; set; }
        public int Wind_Speed { get; set; }

        //Formula for calculating wind chill
        //Currently "Predicted_LoTemp" and "Wind_Speed" do not pull 
        //a value from the combobox as needed

        public double CalculateWindChill()
        {
            double WindChill = 35.74 + 0.6215 * 
            Convert.ToDouble(Predicted_LoTemp) - 35.75 *
            Math.Pow(Convert.ToDouble(Wind_Speed), 0.16) + 
            0.4275 * Convert.ToDouble(Predicted_LoTemp) *
            Math.Pow(Convert.ToDouble(Wind_Speed), 0.16);

            return WindChill;
        }
    }
snz8szmq

snz8szmq1#

好了,我终于让它与以下内容一起工作了:

private void PostBtn_Click(object sender, EventArgs e)
    {
       int LoTempSelection = Convert.ToInt32(PredictedLoTemp.SelectedItem);
       int WindSpeedSelection = Convert.ToInt32(WindSpeed.SelectedItem);

       Forecast currentForecast = new Forecast( LoTempSelection,  
       WindSpeedSelection);
        {
        HiTempLbl.Text = Convert.ToString(PredictedHiTemp.SelectedItem);
        LoTempLbl.Text = Convert.ToString(PredictedLoTemp.SelectedItem);
        WindSpeedLbl.Text = Convert.ToString(WindSpeed.SelectedItem);
        
        WindChillLbl.Text = 
        Convert.ToString(currentForecast.CalculateWindChill());
        }
    }

    public class Forecast
    {
        public int Predicted_LoTemp { get; set; }
        public int Wind_Speed { get; set; }

        public Forecast(int LoTempSelection, int WindSpeedSelection)
        {
            Predicted_LoTemp = LoTempSelection;
            Wind_Speed = WindSpeedSelection;
        }
wlwcrazw

wlwcrazw2#

您只需将这些Forecast属性设置为常规自动属性:

public class Forecast
{
    public int PredictedLoTemp { get; set; }
    public int WindSpeed { get; set; }

    public void DoSomething()
    {
        var product = PredictedLoTemp * WindSpeed;

        // ...
    }

    // ...
}

然后,在创建Forecast对象时,可以基于ComboBoxes设置它们:

var currentForecast = new Forecast
                      {
                          PredictedLoTemp = (int)PredictedLoTemp.SelectedItem,
                          WindSpeed = (int)WindSpeed.SelectedItem
                      };

currentForecast.DoSomething();

然后在Forecast类中使用这些相同的属性。
编辑:
解决您更新后的问题。问题是您实际上没有在任何地方设置属性。我在上面提供了一个示例,但您没有按照它操作。首先,这很奇怪:

Forecast currentForecast = new Forecast();
{
    predicted_LoTemp = 
    Convert.ToInt32(PredictedLoTemp.SelectedItem);
    Wind_Speed = Convert.ToInt32(WindSpeed.SelectedItem);

    HiTempLbl.Text = 
    Convert.ToString(PredictedHiTemp.SelectedItem);
    LoTempLbl.Text = 
    Convert.ToString(PredictedLoTemp.SelectedItem);
    WindSpeedLbl.Text = 
    Convert.ToString(WindSpeed.SelectedItem);

    WindChillLbl.Text = 
    Convert.ToString(currentForecast.CalculateWindChill());
}

我不知道你为什么要用那些大括号,但我想你可能对对象初始化器是如何工作的感到困惑。那些大括号是完全没有意义的,代码应该这样写:

Forecast currentForecast = new Forecast();

predicted_LoTemp = Convert.ToInt32(PredictedLoTemp.SelectedItem);
Wind_Speed = Convert.ToInt32(WindSpeed.SelectedItem);

HiTempLbl.Text = Convert.ToString(PredictedHiTemp.SelectedItem);
LoTempLbl.Text = Convert.ToString(PredictedLoTemp.SelectedItem);
WindSpeedLbl.Text = Convert.ToString(WindSpeed.SelectedItem);

WindChillLbl.Text = Convert.ToString(currentForecast.CalculateWindChill());

正如您所看到的,您没有在任何地方设置Forecast对象的属性,因此数据不会进入该对象以供使用。如果您按照我提供的示例进行操作,那么您将这样做:

var currentForecast = new Forecast
                      {
                          Predicted_LoTemp = (int)PredictedLoTemp.SelectedItem,
                          Wind_Speed = (int)WindSpeed.SelectedItem
                      };

predicted_LoTemp = Convert.ToInt32(PredictedLoTemp.SelectedItem);
Wind_Speed = Convert.ToInt32(WindSpeed.SelectedItem);

HiTempLbl.Text = Convert.ToString(PredictedHiTemp.SelectedItem);
LoTempLbl.Text = Convert.ToString(PredictedLoTemp.SelectedItem);
WindSpeedLbl.Text = Convert.ToString(WindSpeed.SelectedItem);

WindChillLbl.Text = Convert.ToString(currentForecast.CalculateWindChill());

请注意,在本例中,Forecast构造函数后面没有分号,所以括号实际上是对象初始化的一部分。该语法字面上称为对象初始化程序。该代码等效于:

var currentForecast = new Forecast();

currentForecast.Predicted_LoTemp = (int)PredictedLoTemp.SelectedItem;
currentForecast.Wind_Speed = (int)WindSpeed.SelectedItem;

predicted_LoTemp = Convert.ToInt32(PredictedLoTemp.SelectedItem);
Wind_Speed = Convert.ToInt32(WindSpeed.SelectedItem);

HiTempLbl.Text = Convert.ToString(PredictedHiTemp.SelectedItem);
LoTempLbl.Text = Convert.ToString(PredictedLoTemp.SelectedItem);
WindSpeedLbl.Text = Convert.ToString(WindSpeed.SelectedItem);

WindChillLbl.Text = Convert.ToString(currentForecast.CalculateWindChill());

现在,您实际上是在使用属性之前对其进行设置。
另一个变化是,您似乎希望将属性视为double值,因此您可能应该首先将它们声明为double。我使用int是因为您已经使用了int,但如果它们将被视为double,则将它们声明为该类型。

相关问题