如何了解ListBox中的选定项 [英] How to understand the seleted item in ListBox

查看:66
本文介绍了如何了解ListBox中的选定项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我有3个ListBox,我想如果用户在其他ListBoxs第三行中选择第二个ListBox的第三行,则自行选择.

我应该怎么做?

我的意思是:1.如何理解列表框中的选定项并获取它,以及2.如何选择列表框中的项


谢谢

改进:(最佳答案)

首先,我应该感谢s.b对我的帮助,

我从解决方案中了解到这一点:我们有2种(最好,最简单的)方法.1.我们可以转到listBox1_SelectedIndexChanged并在其中写入int A = listBox1.SelectedIndex; A是选择的listBox1的行,并将其设置在listBox2 2中.您可以在每个行的第一行写每个列表框的行,然后再获取选择的行号并在另一个列表框中找到它. (我自己用这个)

祝您好运

解决方案

  int  sel = myListBox2.SelectedIndex; // 获取当前所选项目的索引
如果(sel <  myListBox1.Items.Count)// 谨慎程序员"检查
  myListBox1.SelectedIndex = sel; // 设置所选项目的索引(即,强制选择为索引"sel")
如果(sel <  myListBox3.Items.Count)
  myListBox3.SelectedIndex = sel; 


SelectedItem [ ^ ]属性告诉您用户已选择了哪个项目.您可以使用相同的属性自行将选择设置为特定项目. ListBox的事件.然后,您可以检查SelectedItem,SelectedIndex或SelectValue.
正如Eddy指出的那样,您可以设置第二个ListBox的SelectedItem,SelectedIndex或SelectValue.

关于MSDN的示例 [ 解决方案

int sel = myListBox2.SelectedIndex; // get the index of the current selected item
if ( sel < myListBox1.Items.Count) // 'cautious programmer' check
  myListBox1.SelectedIndex = sel; // set the index of the selected item (i.e. force selection to index 'sel')
if ( sel < myListBox3.Items.Count) 
  myListBox3.SelectedIndex = sel;  


The SelectedItem[^] property tells you what item has been selected by the user. You can use the same property to set the selection to a specific item yourself.


Just adding to Eddy Vluggens answer, to check when a user clicks a certain item you can use the SelectedIndexChanged or SelectedValueChanged Event of the ListBox. You could then check the SelectedItem, SelectedIndex or SelectValue.
As Eddy already pointed out you can then set the SelectedItem, SelectedIndex or SelectValue of your second ListBox.

An example on MSDN[^]. Good luck!


这篇关于如何了解ListBox中的选定项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆