我正在做一些關於switch的練習。我只是從控制檯應用程序做到這一點,我想在窗體應用程序中做到這一點。我正在尋找如何在窗體窗體中切換的語法。 在控制檯它通常是這樣的:在Windows Forms應用程序中使用switch語句
switch (wordValue)
{
case 1:
Console.WriteLine("You have entered numbered two");
break;
default:
break;
我怎麼能做到這一點在我的窗口形式,如果我想顯示此情況ListBox1的?
感謝
=======
謝謝。我試過這個,但是我得到一個錯誤。這是我已經試過:
public static void WriteNumber(int wordValue)
{
switch (wordValue)
{
case 1:
listbox.Items.Add("You have entered number one");
break;
}
}
========
這是我想要做的代碼:
private void btnOk_Click(object sender, EventArgs e)
{
string strUserInputNumber;
strUserInputNumber = textBox1.Text.Trim();
Int32 intNumber;
if (Int32.TryParse(textBox1.Text, out intNumber))
{
listBox1.Items.Add(intNumber.ToString());
}
}
public static void WriteNumber(int wordValue)
{
switch (wordValue)
{
case 1:
this.listBox1.Items.Add("You have entered numbered one");
break;
}
}
====
這是新的代碼:
private void btnOk_Click(object sender, EventArgs e)
{
string strUserInputNumber;
strUserInputNumber = textBox1.Text.Trim();
Int32 intNumber;
if (Int32.TryParse(textBox1.Text, out intNumber))
{
listBox1.Items.Add(intNumber.ToString());
WriteNumber(intNumber);
}
else
{
MessageBox.Show("Please enter an integer not a character");
}
}
public void WriteNumber(int wordValue)
{
switch (wordValue)
{
case 1:
listBox2.Items.Add("You have entered numbered one");
break;
case 2:
listBox2.Items.Add("You have entered numbered two");
break;
case 3:
listBox2.Items.Add("You have entered numbered three");
break;
default:
listBox2.Items.Add("You have exceeded the range of 1-3. Please enter the number between 1-3");
break;
}
它給你什麼錯誤? – Pondidum 2009-06-17 07:06:53
「意外令牌」,並且下劃線爲紅色 – tintincutes 2009-06-17 07:15:52
下劃線爲紅色? – Pondidum 2009-06-17 07:25:12