C# 在 Windows 窗体应用程序中使用 switch 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1005454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using the switch statement in a Windows Forms application
提问by tintincutes
And I'm doing some exercises about switch. I just did it from console application and I would like to do it in window forms applications. I'm looking for syntax on how to do switch in window forms. In console it's usually like this:
我正在做一些关于 switch 的练习。我只是从控制台应用程序中完成的,我想在窗口窗体应用程序中完成它。我正在寻找有关如何在窗口窗体中进行切换的语法。在控制台它通常是这样的:
switch (wordValue)
{
case 1:
Console.WriteLine("You have entered numbered two");
break;
default:
break;
how can I do this in my window forms, if I would like to display this cases in listbox1?
如果我想在 listbox1 中显示这些案例,我该如何在我的窗口表单中执行此操作?
Thanks
谢谢
=======
========
Thank you. I tried this one but I'm getting an error. This is what I've tried:
谢谢你。我试过这个,但出现错误。这是我尝试过的:
public static void WriteNumber(int wordValue)
{
switch (wordValue)
{
case 1:
listbox.Items.Add("You have entered number one");
break;
}
}
========
========
This is the code I'm trying to do:
这是我想要做的代码:
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;
}
}
====
====
This is the new code:
这是新代码:
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;
}
采纳答案by Factor Mystic
The switch/case syntax is identical between WinForms and a console app (or any other type of application or class library), the only difference is how you display the data. If you want to add a string to a listbox (which is apparently what you're asking), it's as simple as
WinForms 和控制台应用程序(或任何其他类型的应用程序或类库)之间的 switch/case 语法是相同的,唯一的区别是您如何显示数据。如果你想向列表框添加一个字符串(这显然是你要问的),就像
listBox1.Items.Add("Here is the text of the list box item");
回答by Razzie
This works fine:
这工作正常:
switch (wordValue)
{
case 1:
this.listBox1.Items.Add("You have entered numbered two");
break;
default:
break;
}
回答by BengtBe
This should work:
这应该有效:
public void WriteNumber(int wordValue)
{
switch (wordValue)
{
case 1:
listbox.Items.Add("You have entered number one"); break;
}
}
You need to remove the statickeyword to get access to the listbox, which is an instance variable.
您需要删除static关键字才能访问列表框,这是一个实例变量。