tin*_*tes 2 c# selectedindex selecteditem
我是C#的新手,我开始学习如何编程我正在学习编程到我使用WindowsApplication而不是Console的Visual Studio Microsoft Edition.在尝试创建此代码时,我遇到了这个命令:Selected Index和Selected Item,我想知道两者之间的区别.我现在对我的代码很困惑.我要做的代码是在列表框中添加和删除文本.
谢谢你的帮助.
其他问题:在我的代码中我有这一行:
int listBoxSelectedItem = listBox1.SelectedIndex;
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
Run Code Online (Sandbox Code Playgroud)
我想理解这一部分:第一行,有一个名为"listBoxSelectedItem"的变量,类型为"int".您选择的项目的位置将存储到名为"listBoxSelectedItem"的变量中.那是对的吗?
第二行是,"listBox1.SelectedIndex"是传递给方法的信息,"RemoveAt"我的理解是正确的吗?
谢谢
Mar*_*ris 13
所选项目将返回所选对象.Selected index将列表中的位置作为int返回.
例如,您可能有一个字符串列表:
Cat
Dog
Hamster
Horse
Run Code Online (Sandbox Code Playgroud)
如果从此列表中选择"Dog",则SelectedItem属性为字符串"Dog",而SelectedIndex为1(索引为零,因此第一项为0,第二项为1等)