2014-10-01 38 views
0

我試圖打印出星期幾,當一個數字輸入到文本框中,但它似乎並沒有在標籤上打印一旦我點擊按鈕,當我運行代碼似乎工作正常,直到當我輸入一個值,然後單擊按鈕,沒有任何反應。標籤priting via按鈕

private void button1_Click(object sender, EventArgs e) 
     { 
      int dayOfWeek = Convert.ToInt32(textBox1); 
      String dayName = null; 
      switch (dayOfWeek) 
      { 
       case 1: 
        dayName = "Sunday"; 
        break; 
       case 2: 
        dayName = "Monday"; 
        break; 
       case 3: 
        dayName = "Tuesday"; 
        break; 
       case 4: 
        dayName = "Wednesday"; 
        break; 
       case 5: 
        dayName = "Thursday"; 
        break; 
       case 6: 
        dayName = "Friday"; 
        break; 
       case 7: 
        dayName = "Saturday"; 
        break; 



        String stndrd = null; 
        if (dayOfWeek == 1) 
        { 
         stndrd = dayName; 
         label1.Text = (stndrd); 
        } 
        else if (dayOfWeek == 2) 
        { 
         stndrd = dayName; 
         label1.Text = (stndrd); 
        } 
        else if (dayOfWeek == 3) 
        { 
         stndrd = dayName; 
         label1.Text = (stndrd); 
        } 
        else if (dayOfWeek == 4) 
        { 
         stndrd = dayName; 
         label1.Text = (stndrd); 
        } 
        else if (dayOfWeek == 5) 
        { 
         stndrd = dayName; 
         label1.Text = (stndrd); 
        } 
        else if (dayOfWeek == 6) 
        { 
         stndrd = dayName; 
         label1.Text = (stndrd); 
        } 
        else if (dayOfWeek == 7) 
        { 
         stndrd = dayName; 
         label1.Text = (stndrd); 
        } 
        else if (dayOfWeek == null) 
        { 
         stndrd = "Mad Day!!!"; 
         label1.Text = (stndrd); 
+1

沒有它編譯一個更簡單的方法(在C.Evenhuis的建議延長)? – 2014-10-01 09:48:51

回答

0

除@ C.Evenhuis的回答外,設置標籤的代碼段位於'case 7'開關的case語句的內部,但在break之後。這段代碼對於當前的代碼將無法訪問,因此標籤本身不會被設置爲任何內容,因爲代碼從不執行。

您需要完成switch語句,然後執行標籤的設置。 但是所有的代碼也是多餘的,因爲你可以做到這一切在

int dayOfWeek; 
if (!int.TryParse(textBox1.Text, out dayOfWeek)) 
{ 
    // you can remove the MessageBox if you're not interested in feedback 
    MessageBox.Show("Value entered is not a valid day number!"); 
    return; 
} 

String dayName = null; 
switch (dayOfWeek) 
{ 
    case 1: 
    dayName = "Sunday"; 
    break; 
    case 2: 
    dayName = "Monday"; 
    break; 
    case 3: 
    dayName = "Tuesday"; 
    break; 
    case 4: 
    dayName = "Wednesday"; 
    break; 
    case 5: 
    dayName = "Thursday"; 
    break; 
    case 6: 
    dayName = "Friday"; 
    break; 
    case 7: 
    dayName = "Saturday"; 
    break; 
    default: 
    dayName = "Mad Day!!!"; 
    break; 
    } 
    // Set the label's text to what was defined above 
    label1.Text = dayName; 
} 
+0

嗨,我剛剛嘗試過這一點,當我點擊按鈕時,我仍然沒有改變標籤上的數據 – Reynolds 2014-10-01 12:15:38

+0

這很奇怪,因爲我在'private void button1_Click(object sender,EventArgs e )'事件方法(只有那個代碼,沒有別的)。你確定該按鈕的OnClick事件是否已設置,並且TextBox和Labels是否可見並被正確引用? – 2014-10-01 12:23:18

+0

啊,我剛剛意識到TextBox沒有被正確引用,謝謝你的幫助。 – Reynolds 2014-10-01 12:40:10

2

這條線:

int dayOfWeek = Convert.ToInt32(textBox1); 

試圖將文本框轉換爲整數,而不是在其內部(textBox1.Text)鍵入的值。你應該將其更改爲類似:

int dayOfWeek; 
if (!int.TryParse(textBox1.Text, out dayOfWeek)) 
{ 
    // you can remove the MessageBox if you're not interested in feedback 
    MessageBox.Show("Value entered is not a valid day number!"); 
    return; 
} 

String dayName = null; 
switch (dayOfWeek) 
{ 
    //... 

使用int.TryParse()可以防止異常,並能夠處理「非法值」。