C# - Cannot do Key-Value in a ListBox in WinForms(C# - 无法在 WinForms 的列表框中执行键值操作)
问题描述
我正在使用 WinForms 中的 ListBox 编写 C# 应用程序.
I'm writing a C# app using a ListBox in WinForms.
我从一个 XML 文件中获取我的数据(ID 和全名).我想在列表框中显示全名,当我选择其中一个时,我想获取相关的 id.
I get my data (id and full name) from an XML file. I want to show full names in the listbox and when I select one of them, I want to get the relevant id.
我尝试使用 SelectedValue 属性,但没有成功.
I tried using SelectedValue property with no luck.
我也试过 KeyValuePair,它在列表框中显示[full name, id]",这不是我想要的:
I also tried KeyValuePair and it shows "[full name, id]" in the listbox, which is not what I wanted:
LB_UserList.Items.Add(new KeyValuePair<string, string>(full_name, node["user_id"].InnerText));
简而言之,如何在 C# 中模拟 HTML 选择?我想在列表框中显示全名并在后端获取相关 id.
How can I simulate a HTML select in C# in short? I want to show full names in the listbox and to get relevant id in the backend.
推荐答案
使用c# 字典为此,
Dictionary<string, string> list = new Dictionary<string, string>();
list.Add("item 1", "Item 1");
list.Add("item 2", "Item 2");
list.Add("item 3", "Item 3");
list.Add("item 4", "Item 4");
dropdown.DataSource = list;
dropdown.DataTextField = "Value";
dropdown.DataValueField = "Key";
dropdown.DataBind();
<小时>
listBox.DataSource = new BindingSource(list, null); 
listBox.ValueMember = "Key";
listBox.DisplayMember = "Value"; 
                        这篇关于C# - 无法在 WinForms 的列表框中执行键值操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C# - 无法在 WinForms 的列表框中执行键值操作
				
        
 
            
        - WebMatrix WebSecurity PasswordSalt 2022-01-01
 - 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
 - Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
 - 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
 - C# 中多线程网络服务器的模式 2022-01-01
 - 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
 - 输入按键事件处理程序 2022-01-01
 - 如何用自己压缩一个 IEnumerable 2022-01-01
 - MoreLinq maxBy vs LINQ max + where 2022-01-01
 - C#MongoDB使用Builders查找派生对象 2022-09-04
 
						
						
						
						
						
				
				
				
				