简体   繁体   English

验证列表框中的项目的文本与列表中的相同 <string> C#

[英]Verify Text of item in Listbox is the same one in List<string> C#

I'm trying to get it to verify that it has the same item in the List as the one that's currently selected in the listbox 我正在尝试获取它以验证它在列表中是否与列表框中当前选择的项目相同

Why does this code not work, It should work unconditionally because the text generated from the listbox is taken from the List choicetitle 为什么此代码不起作用,它应该无条件起作用,因为从列表框生成的文本取自“列表”选择标题

if (RemovePackages_Listbox.Text == choicetitle[RemovePackages_Listbox.SelectedIndex])
            {
                MessageBox.Show("The above code worked!");
            }

Try this 尝试这个

if (RemovePackages_Listbox.SelectedItem.ToString() == choicetitle[RemovePackages_Listbox.SelectedIndex])
            {
                MessageBox.Show("The above code worked!");
            }

else
{
    MessageBox.Show("RemovePackages_Listbox.SelectedItem.ToString() is "+RemovePackages_Listbox.SelectedItem.ToString()+" and choicetitle[RemovePackages_Listbox.SelectedIndex] is "+choicetitle[RemovePackages_Listbox.SelectedIndex]);
}

And tell us what you see in the popup messagebox? 并告诉我们您在弹出消息框中看到的内容吗?

RemovePackages_Listbox.SelectedIndex

will return a zero-based index of the selected item in the ListBox. 将在列表框中返回所选项目的从零开始的索引。

So you're asking: 所以你问:

If the text displayed in my Listbox is the same as the string in my ChoiceTitle List at position SELECTEDINDEX - 如果我的列表 框中 显示文本我的“选择标题列表”中位于SELECTEDINDEX位置字符串相同-

Do this. 做这个。

Triple check that. 三重检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM