简体   繁体   English

如何在C#WinForm中复制下拉标签和值

[英]How to replicate drop-down label and value in C# WinForm

I am new to C# and I am trying to create a drop-down box with a label and value property. 我是C#的新手,我正在尝试创建一个带有label和value属性的下拉框。 For example in HTML I can have <option value"user_name"> Username </option> , how do I get this done in WinForms using C#. 例如,在HTML中,我可以具有<option value"user_name"> Username </option> ,如何使用C#在WinForms中完成此操作。

I understand how to create a combo box and add string collections to it using the WinForm designer, but how do I assign value to this strings? 我了解如何使用WinForm设计器创建组合框并向其添加字符串集合,但是如何为该字符串分配值? The value is quite important, as this is what is needed to interact with a different server. 该值非常重要,因为这是与其他服务器进行交互所需要的。

Thanks. 谢谢。

Break up your problem 分手你的问题

1) You said - "I understand how to create a combo box and add string collections to it using the WinForm designer" 1)您说-“我了解如何使用WinForm设计器创建组合框并向其添加字符串集合”

2) You said - "but how do I assign value to this strings?" 2)您说-“但是如何为该字符串赋值?”

Option 1, is not at all related to Option 2. 选项1与选项2根本无关。

Option 1 - You are adding data to the combobox in design mode. 选项1-您正在设计模式下将数据添加到组合框。

Option 2 - Which string you are talking about? 选项2-您在说哪个字符串? If you have already assigned the data(collection of strings) to combobox then combobox will display collection of strings only. 如果您已经将数据(字符串集合)分配给combobox,则combobox将仅显示字符串集合。 M i right? 我可以吗?

You don't need Option 1, if you are implementing Option 2. 如果要实现选项2,则不需要选项1。

You need data to be displayed in your combobox which your user will select. 您需要在用户将选择的组合框中显示数据。

So, now where the data coming up? 那么,现在数据在哪里呢? It is from Sql Server/XML/Text File/...? 它来自Sql Server / XML /文本文件/ ...? Or you are creating a List/Dictionary in your code and assigning to the combobox? 还是要在代码中创建列表/字典并将其分配给组合框?

So finally, you can use any one approach...either Option 1 - "add data to your combobox in design mode" Or Option 2 - "add data to your combobox by retrieving from a datasource(Sql Server/XML/Text File/List/Dictionary)". 因此,最后,您可以使用任何一种方法...选项1-“在设计模式下将数据添加到组合框”或选项2-“通过从数据源检索来将数据添加到组合框(Sql Server / XML / Text File /列表/字典)”。

How to do that, refer the links mentioned in your comments. 如何做到这一点,请参阅您的评论中提到的链接。

Both the case/s, data will be available inside combobox and user can interact with the data inside combobox. 两种情况下,数据都将在组合框内可用,并且用户可以与组合框内的数据进行交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Selenium C#拍摄下拉菜单的屏幕截图? - How to take screenshot of drop-down menu with Selenium C#? 如何将值设置为下拉列表 - How To Set Value To Drop-down list 我无法使用Selenium C#选择下拉选项 - I cannot select an drop-down option with selenium C# 通过 C# 下拉选择 HTML - Selecting HTML drop-down through C# 如何使用另一个下拉值填充下拉列表 - how to populate drop-down based on another drop-down value using 使组合框像下拉列表(C#) - Make combobox act like drop-down list (C#) 如何在下拉列表中显示同一个表中的 2 个字段 MVC C# - How to display 2 fields from the same table in the drop-down list MVC C# 如何使用 C# 从字符串数组制作类似枚举的 Unity 检查器下拉菜单? - How to make an enum-like Unity inspector drop-down menu from a string array with C#? 如何使用Regex表达式匹配宽度和高度大小,以及如何在SQL或C#中使用“排序依据”来构建下拉列表? - How to match a width and height size with a Regex expression and use Sort By in SQL or C# to build a drop-down? 如何使用 C# 通过 Selenium WebDriver 获取下拉列表中的所有选项? - How to get all options in a drop-down list by Selenium WebDriver using C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM