如何在ListBox C#中检索每个项目

尤里

我是C#的新手,尽管去年我确实学习了Java的四分之一。我的作业明天要交,所以这是我的问题。我编写了一个小程序示例,希望可以得到想要的东西。我想知道,我如何看待listBox并说,例如,如果选择了项目Soccer,则做一件事,但是如果选择了其他任何东西,则做另一件事?我将上传一段不执行应做的代码,大家都可以称我为愚蠢的人,然后给我答案。

    private void submitButton_Click(object sender, EventArgs e)
    {
        string best;
        best = namesListBox.SelectedItem.ToString();

        if ((string)namesListBox.SelectedItem == "Soccer") 
        {
            MessageBox.Show("Fried chicken, don't let that bird die in vain.");
        }
        else
        {
            MessageBox.Show("Long long ago, in the land of the Hobbit...");
        }
    }

    private void exitButton_Click(object sender, EventArgs e)
    {
        Close();
    }
}

}

每次运行此代码时,我总是很久以前……。这不是我想要看到的。任何帮助将不胜感激,我将放弃这个程序。这不是实际的程序,它要复杂得多,我只是制作了这个程序来演示我的问题……在此先感谢

泰勒·潘图索(Tyler Pantuso)

尤里,

看起来您正在将所选项目附加到字符串中。我觉得这可能是问题所在。

您究竟将字符串“ best”用作什么?您定义了它,但是在您的示例中未使用它。

这是你想要的?尝试使用ToString()方法,并对其进行修剪,以使空格不会抛出您的代码:

string selectedItem = namesListBox.SelectedItem.ToString().Trim();

if (selectedItem == "Soccer")
    MessageBox.Show("Soccer is selected.");
else
    MessageBox.Show("NOT SELECTED");

也许是套管错误?如果您不想担心大写或小写,请尝试在字符串中附加.ToLower():

string selectedItem = namesListBox.SelectedItem.ToString().Trim().ToLower();

if (selectedItem == "Soccer".ToLower())
    // Handle code accordingly.
else
    // Handle accordingly.

您还可以在ListBox中搜索字符串,然后找出其位置。然后确定用户是否选择了该特定项目:

int selIndex = namesListBox.FindString("Soccer");

这将返回其中包含单词“ Soccer”的第一项的位置的从零开始的索引。

现在处理选定的索引:

if (namesListBox.SelectedIndex != -1 &&
    namesListBox.SelectedIndex == selIndex)
    MessageBox.Show("First item containing \"Soccer\" is selected.");
else
    MessageBox.Show("First item containing \"Soccer\" is not selected.");

您很有可能使用的是ListView对象,而不是实际的ListBox。在这种情况下,您将不得不采用其他方法。此示例仍然使用您的ListBox名称:

// This assumes that you cannot select multiple items, and that you
// only have one column in your ListView.
string selectedItem = namesListBox.SelectedItems[0].SubItems[0].Text;

if (selectedItem.Trim() == "Soccer") // Continue as before...

这对您的项目有用吗?

编辑:等等。您是否要在评估之前将所选项目更改为“最佳”?

如果是这样,请执行以下操作:

int selIndex = namesListBox.SelectedIndex;

namesListBox.Items.RemoveAt(selIndex);
namesListBox.Items.Insert(selIndex, "best");

编辑:

确保包括不处理任何选择的代码:

// Will not execute block of code if nothing is selected.
if (namesListBox.SelectedIndex == -1)
{
    MessageBox.Show("No item is selected.");
    return;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在列表中编写 ListBox 项目?

如何在C#中对listBox进行排序?

如何从列表C#中检索常见项目

如何在 tkinter.Listbox 中“修复”选定的项目?

如何在 C# 项目中检索 EmbeddedResource 的 DependentUpon 属性

如何在Winforms中使用ListBox的多色项目?

如何遍历json对象中的项目并检索C#中的特定值?

为每个项目XAML C#创建Listbox.ItemTemplate

如何在 WPF ListBox 中添加標題

如何在文件隐藏代码中绑定 ListBox 的属性?

如何在 tkinter.Listbox 中重置选择顺序?

如何在“窗口资源”中设置ListBox项的高度?

如何在代码中设置ListBox控件的Style属性

如何在 Python Tkinter 中绑定 ComboBox 的 ListBox

如何在ListBox中水平显示绑定数据

如何在 ListBox 中绑定和显示 ListBoxItem 索引?

如何在 C# 中将 ListBox 中的名称显示到 TextBox 上?

如何检索CheckedListBox中的项目是否被选中?Winforms C#

如何使用C#检索/比较HTML列表框中的项目

从 listBox c# 中获取随机项目

如何在C#中使用RowFilter筛选对象中的每个项目的数据表

如何使用PowerShell和C#检索每个.bak文件?

如何在C#中从TFS检索工作项列表?

如何在C#中检索HttpPost参数

如何在C#中检索JWT令牌?

如何在C#中检索数据列表项?

如何在 C# 中返回和检索对象值?

如何在c#代码中从powershell检索pwd的值

如何在C#中的每个对象中实现GetType?