selecteditems 和 selectedindex c#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/851781/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:10:56  来源:igfitidea点击:

selecteditems and selectedindex c#

c#selecteditemselectedindex

提问by tintincutes

I'm new to C# and I'm starting to learn how to program I'm learning to program into the Visual Studio Microsoft Edition where I use the WindowsApplication instead of the Console. While trying to make this code, I encountered this command: Selected Index and Selected Item and I would like to know the difference between the two. I'm quite confused now with my code. The code I'm trying to do is adding and deleting text in the listbox.

我是 C# 新手,我开始学习如何编程我正在学习如何编程到 Visual Studio Microsoft 版,在那里我使用 WindowsApplication 而不是控制台。在尝试制作此代码时,我遇到了这个命令:Selected Index 和 Selected Item,我想知道两者之间的区别。我现在对我的代码很困惑。我正在尝试执行的代码是在列表框中添加和删除文本。

Thanks for your help.

谢谢你的帮助。

Additional question: in my code I have this line:

附加问题:在我的代码中,我有这一行:

int listBoxSelectedItem = listBox1.SelectedIndex;
listBox1.Items.RemoveAt(listBox1.SelectedIndex);

I would like to understand this part: The first line, has a variable called " listBoxSelectedItem" with a type "int". The position of the item you selected will be store to the variable called "listBoxSelectedItem". Is that correct?

我想了解这部分:第一行,有一个名为“listBoxSelectedItem”的变量,类型为“int”。您选择的项目的位置将存储到名为“listBoxSelectedItem”的变量中。那是对的吗?

The second line is, the "listBox1.SelectedIndex" is the information that is being pass through to the method, "RemoveAt" Is my understanding here correct?

第二行是,“listBox1.SelectedIndex”是传递给方法的信息,“RemoveAt”我的理解是否正确?

Thanks

谢谢

采纳答案by Martin Harris

Selected item will return the object that is selected. Selected index returns the location in the list as an int.

Selected item 将返回被选中的对象。选定的索引以整数形式返回列表中的位置。

For example you may have a list of strings:

例如,您可能有一个字符串列表:

Cat
Dog
Hamster
Horse

If you select "Dog" from this list them the SelectedItem property is the string "Dog" while the SelectedIndex is 1 (indexes are zero based, so the first item is 0, second 1 etc.)

如果您从此列表中选择“Dog”,则 SelectedItem 属性为字符串“Dog”,而 SelectedIndex 为 1(索引从零开始,因此第一项为 0,第二项为 1 等)