C# 如何在组合框中将大小写用作字符串来执行switch语句?

C# 如何在组合框中将大小写用作字符串来执行switch语句?,c#,combobox,switch-statement,C#,Combobox,Switch Statement,这是我的组合框中的内容 Runescape Maplestory League of Legends 下面是我尝试使用switch语句的代码 private void button1_Click(object sender, EventArgs e) { switch (comboBox1.SelectedIndex) { case "Runescape": MessageBox.Show("You are playing RS");

这是我的组合框中的内容

Runescape
Maplestory
League of Legends
下面是我尝试使用switch语句的代码

private void button1_Click(object sender, EventArgs e)
{
    switch (comboBox1.SelectedIndex)
    {
        case "Runescape":
            MessageBox.Show("You are playing RS");
            break;

        case "Maplestory":
            MessageBox.Show("You are playing MS");
            break;

        default:
            MessageBox.Show("You're playing League");
            break;
    }
}
它给了我一个错误,它不允许我隐式地将字符串转换为int

我想将案例用作字符串而不是数字。我该怎么做


另外,只是出于好奇,如果它能工作,我们可以得到一个字符串的一部分作为一个案例。假装组合框上写的是“Runescape 3”,而不是“Runescape”。我不确定C#是否能识别字符串的一部分

case "Runescape":
        MessageBox.Show("You are playing RS");
        break;

您可以使用
string
int
进行比较

1。如果要将
字符串
进行比较,请使用
组合框
控件的
SelectedItem
属性

试试这个:

switch (comboBox1.SelectedItem.ToString().Trim())
    {
        case "Runescape":
            MessageBox.Show("You are playing RS");
            break;

        case "Maplestory":
            MessageBox.Show("You are playing MS");
            break;

        default:
            MessageBox.Show("You're playing League");
            break;
    }
        switch (comboBox1.SelectedIndex)
        {
            case 0:
                MessageBox.Show("You are playing RS");
                break;

            case 1:
                MessageBox.Show("You are playing MS");
                break;

            default:
                MessageBox.Show("You're playing League");
                break;
        }
           switch (comboBox1.SelectedItem.ToString().Split(' ')[0])
            {
                case "Runescape":
                    MessageBox.Show("You are playing RS");
                    break;

                case "Maplestory":
                    MessageBox.Show("You are playing MS");
                    break;

                default:
                    MessageBox.Show("You're playing League");
                    break;
            }
2.如果要将
索引进行
比较
,请使用
组合框
控件的
SelectedIndex
属性

试试这个:

switch (comboBox1.SelectedItem.ToString().Trim())
    {
        case "Runescape":
            MessageBox.Show("You are playing RS");
            break;

        case "Maplestory":
            MessageBox.Show("You are playing MS");
            break;

        default:
            MessageBox.Show("You're playing League");
            break;
    }
        switch (comboBox1.SelectedIndex)
        {
            case 0:
                MessageBox.Show("You are playing RS");
                break;

            case 1:
                MessageBox.Show("You are playing MS");
                break;

            default:
                MessageBox.Show("You're playing League");
                break;
        }
           switch (comboBox1.SelectedItem.ToString().Split(' ')[0])
            {
                case "Runescape":
                    MessageBox.Show("You are playing RS");
                    break;

                case "Maplestory":
                    MessageBox.Show("You are playing MS");
                    break;

                default:
                    MessageBox.Show("You're playing League");
                    break;
            }
3.如果只想从
组合框中获取
的第一部分,请使用
Split()
函数

试试这个:

switch (comboBox1.SelectedItem.ToString().Trim())
    {
        case "Runescape":
            MessageBox.Show("You are playing RS");
            break;

        case "Maplestory":
            MessageBox.Show("You are playing MS");
            break;

        default:
            MessageBox.Show("You're playing League");
            break;
    }
        switch (comboBox1.SelectedIndex)
        {
            case 0:
                MessageBox.Show("You are playing RS");
                break;

            case 1:
                MessageBox.Show("You are playing MS");
                break;

            default:
                MessageBox.Show("You're playing League");
                break;
        }
           switch (comboBox1.SelectedItem.ToString().Split(' ')[0])
            {
                case "Runescape":
                    MessageBox.Show("You are playing RS");
                    break;

                case "Maplestory":
                    MessageBox.Show("You are playing MS");
                    break;

                default:
                    MessageBox.Show("You're playing League");
                    break;
            }

您可以使用
string
int
进行比较

1。如果要将
字符串
进行比较,请使用
组合框
控件的
SelectedItem
属性

试试这个:

switch (comboBox1.SelectedItem.ToString().Trim())
    {
        case "Runescape":
            MessageBox.Show("You are playing RS");
            break;

        case "Maplestory":
            MessageBox.Show("You are playing MS");
            break;

        default:
            MessageBox.Show("You're playing League");
            break;
    }
        switch (comboBox1.SelectedIndex)
        {
            case 0:
                MessageBox.Show("You are playing RS");
                break;

            case 1:
                MessageBox.Show("You are playing MS");
                break;

            default:
                MessageBox.Show("You're playing League");
                break;
        }
           switch (comboBox1.SelectedItem.ToString().Split(' ')[0])
            {
                case "Runescape":
                    MessageBox.Show("You are playing RS");
                    break;

                case "Maplestory":
                    MessageBox.Show("You are playing MS");
                    break;

                default:
                    MessageBox.Show("You're playing League");
                    break;
            }
2.如果要将
索引进行
比较
,请使用
组合框
控件的
SelectedIndex
属性

试试这个:

switch (comboBox1.SelectedItem.ToString().Trim())
    {
        case "Runescape":
            MessageBox.Show("You are playing RS");
            break;

        case "Maplestory":
            MessageBox.Show("You are playing MS");
            break;

        default:
            MessageBox.Show("You're playing League");
            break;
    }
        switch (comboBox1.SelectedIndex)
        {
            case 0:
                MessageBox.Show("You are playing RS");
                break;

            case 1:
                MessageBox.Show("You are playing MS");
                break;

            default:
                MessageBox.Show("You're playing League");
                break;
        }
           switch (comboBox1.SelectedItem.ToString().Split(' ')[0])
            {
                case "Runescape":
                    MessageBox.Show("You are playing RS");
                    break;

                case "Maplestory":
                    MessageBox.Show("You are playing MS");
                    break;

                default:
                    MessageBox.Show("You're playing League");
                    break;
            }
3.如果只想从
组合框中获取
的第一部分,请使用
Split()
函数

试试这个:

switch (comboBox1.SelectedItem.ToString().Trim())
    {
        case "Runescape":
            MessageBox.Show("You are playing RS");
            break;

        case "Maplestory":
            MessageBox.Show("You are playing MS");
            break;

        default:
            MessageBox.Show("You're playing League");
            break;
    }
        switch (comboBox1.SelectedIndex)
        {
            case 0:
                MessageBox.Show("You are playing RS");
                break;

            case 1:
                MessageBox.Show("You are playing MS");
                break;

            default:
                MessageBox.Show("You're playing League");
                break;
        }
           switch (comboBox1.SelectedItem.ToString().Split(' ')[0])
            {
                case "Runescape":
                    MessageBox.Show("You are playing RS");
                    break;

                case "Maplestory":
                    MessageBox.Show("You are playing MS");
                    break;

                default:
                    MessageBox.Show("You're playing League");
                    break;
            }
试试这个:

switch (Convert.ToString(comboBox1.SelectedItem))
{
  //...
}
使用
Convert.ToString()
以更安全的方式将其转换为字符串。

尝试以下操作:

switch (Convert.ToString(comboBox1.SelectedItem))
{
  //...
}

使用
Convert.ToString()
以更安全的方式将其转换为字符串。

组合框有三种方法可以从中获取所需内容:

  • SelectedItem
    :与索引关联的实际对象
  • SelectedIndex
    :所选内容的整数(从零开始)索引
  • SelectedValue
    :选择项的值(使用
    SelectedValuePath

组合框有三种方式可以从中获取您想要的内容:

  • SelectedItem
    :与索引关联的实际对象
  • SelectedIndex
    :所选内容的整数(从零开始)索引
  • SelectedValue
    :选择项的值(使用
    SelectedValuePath

只是一个建议,但您当前的方法不太可扩展。添加更多游戏需要添加到此开关语句。另外,从外观上看,开始玩所选游戏所需的逻辑将与您的UI代码紧密耦合。最好定义一个接口,比如“IGame”,它可以定义一个Name属性和一个StartPlaying()函数。这三个游戏可以定义为三个独立的类来实现这个接口。然后可以将组合框绑定到IGame对象的集合。单击按钮时,我们只需调用combobox1.SelectedItem.StartPlaying(),无论用户选择了哪个游戏

我已经离开WinForms世界有一段时间了,所以如果不首先进行类型检查并将combobox1.SelectedItem转换为IGame,我不能100%确定这是否有效。但不管怎样,我想你会发现这种方法在未来会减少挫折感


我知道这不是对你问题的直接回答,但是多态性对于这样的东西来说是很棒的。

只是一个建议,但是你目前的方法不是很好扩展。添加更多游戏需要添加到此开关语句。另外,从外观上看,开始玩所选游戏所需的逻辑将与您的UI代码紧密耦合。最好定义一个接口,比如“IGame”,它可以定义一个Name属性和一个StartPlaying()函数。这三个游戏可以定义为三个独立的类来实现这个接口。然后可以将组合框绑定到IGame对象的集合。单击按钮时,我们只需调用combobox1.SelectedItem.StartPlaying(),无论用户选择了哪个游戏

我已经离开WinForms世界有一段时间了,所以如果不首先进行类型检查并将combobox1.SelectedItem转换为IGame,我不能100%确定这是否有效。但不管怎样,我想你会发现这种方法在未来会减少挫折感


我知道这不是对你问题的直接回答,但多态性对于这样的东西来说是非常棒的。

呃Trim()做什么?还有,我只是想知道我编辑的代码是否可以工作。我在原来的代码下面添加了一些代码。如果没有,那么我将恢复编辑。@puretppc:那么您还想在
SelectedValue
中添加一个索引,对吗?@puretppc:如果您只想从ComboBox中获取SelectedItem的第一部分,请使用Split()函数,查看我用第三个选项编辑的答案。@Downvoter:您能解释一下代码中的错误在哪里吗?呃Trim()是什么还有,我只是想知道我编辑的代码是否有效。我在原来的代码下面添加了一些代码。如果没有,我将恢复编辑。@puretppc:那么您还想向
SelectedValue
添加一个索引,对吗?@puretppc:如果您只想从组合框中获取SelectedItem的第一部分,请使用Split()函数,使用第三个选项查看我编辑的答案。@Downvoter:你能解释一下代码中的错误在哪里吗?你能解释一下
Convert.ToString()
object.ToString()
“更安全”吗?@Lightning\A它只涉及空值检查,使用
Convert.ToString()
将返回空值