2015-05-06 109 views
1

我想在c#中使用switch語句在一個文本框中顯示一些文本,並在另一個文本框中根據我從組合框中選擇的選項顯示一個數字顯示。我已經創建了自己的類「Devices」,並在課堂中創建了多個對象。我還給每個對象一些屬性(例如DeviceName,DeviceRating)。但是,當我開始表單並從我的組合框中選擇一個選項時,第一個選項按計劃運行(文本顯示在相關文本框中),但所有選項都顯示空白文本框。任何想法爲什麼這可能會發生?使用組合框與switch語句c#

這裏是我的屬性代碼:

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; 

    } 

這裏是我創建的類代碼:

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

這裏就是我在類中創建新的對象代碼:

// 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(); 

這裏是switch語句控制組合框中每個case的情況(注意com bobox列表與列出的對象的順序相同):

// 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; 
     } 


    } 
+0

看起來像它應該工作好了,根據我們可以看到。當您嘗試將它們分配給文本框時,放置一些斷點並查看「Hairdryer.DeviceName」等的值。 –

+0

你可以添加項目到組合框,使用顯示成員來顯示設備名稱和設置文本框爲'((設備)comboBox1.SelectedItem)。設備名稱等 –

+2

爲什麼名爲'comboBox3_SelectedIndexChanged'和'switch'使用'comboBox1.SelectedIndex'。 'comboBox1&comboBox3? –

回答

1

這是一種不匹配。你是否給組合框命名爲comboBox1並使用名爲comboBox3_SelectedIndexChanged()的事件?

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

它不應該是

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

請原諒,如果連名字是有意保持原樣..另外,你真的在​​你的代碼這些額外的分號?

Dishwasher.DeviceRating = 1; 
     ; 

OvenHob.DeviceRating = 1; 
     ; 
+0

感謝您的幫助,我知道它很可能是一件小事。 分號確實是一個錯字,除了當他們需要時,我不會使用它們! – TheFish