各种成功...... ListBoxes再次...... [英] Success Of Sorts... ListBoxes Again...

查看:114
本文介绍了各种成功...... ListBoxes再次......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



这与我之前提出的问题有关,并且得到了强大的Griff的回答!



我现在可以从列表框中找到想要的项目从可怕的列表中排序,我需要现在自动选择它以便我可以正确解析它,我尝试过:

Hi All,

This related to a question from earlier I asked and was answered by the mighty Griff!

I can now get the item wanted in the list box sorted out from the horrible list, I need to auto select it now so I can parse it properly, I have tried:

 lstMeters.SelectedItem = 0;
SelectedMeter = lstMeters.SelectedItem.ToString();

但是这不起作用(?)是不是因为列表框中只有一个项目?如果有多个项目,上面会选择项目。

格伦

However this doesn't work (?) is it due to the fact that there is only one item in the list box? the above would select the item if there was more than one item.
Glenn

推荐答案

如果你想设置初始选择,你应该尝试使用 SelectedIndex SelectedItem 是一个对象类型属性,它将返回绑定到控件的实际对象。



假设它是一个Windows窗体,以下工作正常:



If you are looking to set the initial selection, you should try using SelectedIndex. SelectedItem is an object type property it would return the actual object that is bound to the control.

Assuming it is a windows form, following works fine for me:

listBox1.Items.AddRange(new string[] { "1", "2", "3" });
            //listBox1.SelectedItem = "1";
            listBox1.SelectedIndex = 0;

            string str = listBox1.SelectedItem.ToString();





您可以取消注释SelectedItem行并注释SelectedIndex行,这也可以正常工作。



You can uncomment SelectedItem line and comment SelectedIndex line and that would work fine too.


这篇关于各种成功...... ListBoxes再次......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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