在windows窗体中有类似listitem类的东西吗? [英] Is there something like the listitem class in windows forms?

查看:81
本文介绍了在windows窗体中有类似listitem类的东西吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我在网页表单中使用下拉列表进行了此操作。



以下是代码:

 ListItem l1 = new ListItem( -  Select  - ,0); 
ddlUName.Items.Add(l1);
ddlUName.SelectedValue =0;



现在我正在尝试使用组合框在Windows窗体中执行相同的操作,但这里的ListItem类是不在那里。那么有人可以帮助我吗?如何在Windows窗体中使用上面的代码?



谢谢。

解决方案

类似列表type是 System.Windows.Forms.ListBox 。一个更高级的控件是 System.Windows.Forms.ListView

http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.aspx [ ^ ],
http://msdn.microsoft.com/en-us /library/system.windows.forms.listview.aspx [ ^ ]。



首先,课程不需要列表项目。相反,您可以使用任何类型的对象。这非常重要,因为您可以在项目中存储任何数据。许多初学者都不理解它,并倾向于存储代表数据的字符串而不是数据本身,然后提取数据。你真的应该将实际数据存储在 Items 中。唯一的问题是:UI中显示了什么字符串?答案很简单:无论虚拟方法 ToString 返回。因此,您可以通过覆盖项类型的 System.Object.ToString()来控制此文本,该类型可以是任何结构或类。当然,在一些简单的情况下,你可以只存储字符串。



第二类确实有项目类型, ListViewItem ,但也是子项的概念, ListViewItem.SubItems 。如果需要,请参阅上面引用页面上的代码示例。



-SA


使用 ListViewItem [ ^ ]。使用 SelectedIndices [ ^ ]属性以迭代所选项目

Actually, I did this in web forms with dropdownlist.

Here is the code:

ListItem l1 = new ListItem("-Select-","0");
            ddlUName.Items.Add(l1);
            ddlUName.SelectedValue = "0";


Now I''m trying to do the same thing in Windows forms using combobox, but here the ListItem class is not there. So can anybody help me? How can I use the above code in windows forms?

Thank you.

解决方案

The analogous list type is System.Windows.Forms.ListBox. A much more advanced control is System.Windows.Forms.ListView:
http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.aspx[^].

First, class does not need list items. Instead, you can use object of any type. This is very important, because you can store any data in the item. Many beginners don''t understand it and tend to store strings representing data instead the data itself, and then suffer extracting data. You really should store actual data in Items. The only question is: what string is shown in the UI? The answer is simple: whatever the virtual method ToString returns. So, you can control this text by overriding System.Object.ToString() of your item type, which can be any structure or class. Of course, you can store just strings, in some simple cases.

The second class does have the item type, ListViewItem, but also a concept of sub-item, ListViewItem.SubItems. If you need it, see the code sample on the page referenced above.

—SA


use ListViewItem [^] for WindowsForms. use SelectedIndices[^] property to iterate through selected items


这篇关于在windows窗体中有类似listitem类的东西吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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