Using combobox with switch statement c#

I am trying to use the switch statement in c# to make some text display in a textbox and a number display in another textbox based on the option I select from a combobox. I have made my own class named 'Devices' and have created several objects within the class. I have also given each object several properties (e.g. DeviceName, DeviceRating). However, when I start my form and select an option from my combobox, the first option works as planned (text shows in the relevant text boxes) but all the options display blank textboxes. Any thoughts on why this might be happening?

Here is my code for the properties:

 private void Form1_Load(object sender, EventArgs e)
    {

        // Gives properties of each object in the 'Devices' class.

        WashingMachine.DeviceName = "Washing Machine";
        WashingMachine.DeviceRating = 1200;


        Dishwasher.DeviceName = "Dishwasher";
        Dishwasher.DeviceRating = 1;
        ;

        OvenHob.DeviceName = "Oven/Hob";
        OvenHob.DeviceRating = 1;
        ;

        TowelRail.DeviceName = "Towel Rail";
        TowelRail.DeviceRating = 1;


        Hairdryer.DeviceName = "Hairdryer";
        Hairdryer.DeviceRating = 1;


        Shower.DeviceName = "Shower";
        Shower.DeviceRating = 1;

    }

Here is the code for the class I created:

class Devices
    {
        public string DeviceName;
        public int DeviceRating;
        public int UsedMins;
    }

Here is code where I create the new objects in the class:

// Creating new objects under the 'Devices' class.

    Devices WashingMachine = new Devices();
    Devices Dishwasher = new Devices();
    Devices OvenHob = new Devices();
    Devices TowelRail = new Devices();
    Devices Hairdryer = new Devices();
    Devices Shower = new Devices();
    Devices PhoneCharger = new Devices();
    Devices TabletCharger = new Devices();
    Devices ElectricBlanket = new Devices();

And here is the switch statement controlling what happens for each case in the combobox (note that combobox list is in the same order as the listed objects):

 // Puts a Name and Power Rating into the textboxes based on the chosen device
    private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch (comboBox1.SelectedIndex)
        {
            case 0:
                DeviceName.Text = WashingMachine.DeviceName;
                PowerRating.Text = WashingMachine.DeviceRating.ToString();
                break;
            case 1:
                DeviceName.Text = Dishwasher.DeviceName;
                PowerRating.Text = Dishwasher.DeviceName.ToString();
                break;
            case 2:
                DeviceName.Text = OvenHob.DeviceName;
                PowerRating.Text = OvenHob.DeviceRating.ToString();
                break;
            case 3:
                DeviceName.Text = TowelRail.DeviceName;
                PowerRating.Text = TowelRail.DeviceRating.ToString();
                break;
            case 4:
                DeviceName.Text = Hairdryer.DeviceName;
                PowerRating.Text = Hairdryer.DeviceRating.ToString();
                break;
            case 5:
                DeviceName.Text = Shower.DeviceName;
                PowerRating.Text = Shower.DeviceRating.ToString();
                break;
        }


    }

Answers


This is kind of not matching. Did you give the ComboBox a name of comboBox1 and using an event named comboBox3_SelectedIndexChanged()?

private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
    switch (comboBox1.SelectedIndex)
    {

Shouldn't it be

private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
    switch (comboBox3.SelectedIndex)
    {

Please excuse, if the even name is intentionally kept as it is.. Also, do you really have these extra semi-colons in your code?

Dishwasher.DeviceRating = 1;
        ;

and

OvenHob.DeviceRating = 1;
        ;

Need Your Help

ios - How to declare setToken method?

iphone ios objective-c uiwebviewdelegate

I'm trying to get access token from Google,Yahoo.But I'm getting an error like WACloudAccessControlClient may not respond to setToken.How to declare setToken method here.

Cross compile when using apt-get

linux command-line cross-compiling apt-get scratchbox

I am currently using Scratchbox2 for cross compiling. I can do ./configure, make install from my toolchain prompt:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.