[英]How to set ComboBox to not show a part of string in it's items?
Want to keep track of some data for each insertion into ComboBox
which data type is string. 想要跟踪每个插入
ComboBox
数据,其中数据类型是字符串。 So I thought it's good idea to merge item text with my data. 所以我认为将项目文本与我的数据合并是个好主意。
However I need a way to hide the data from the items list. 但是我需要一种方法来隐藏项目列表中的数据。
Here is some a simple example of what I want: 以下是我想要的一个简单示例:
"First item text Data:This was a dog"
"Second item text Data:This was a girl"
"...item text... Data:...."
I need to find a solution to hide data from user. 我需要找到一个隐藏用户数据的解决方案。
Already tried to set MaxLength
of ComboBox to 20 with no success. 已经尝试将ComboBox的
MaxLength
设置为20但没有成功。
Note : I am not allowed to use any other class to achieve this so Stack, Queue won't help 注意:我不允许使用任何其他类来实现这一点,因此Stack,Queue将无济于事
There are two attributes of each item in c# combo box are text and value. c#组合框中每个项目有两个属性是text和value。 set item value to Data you want and text to string you want to display for your user.
将项目值设置为您想要的数据,并将文本设置为要为用户显示的字符串。
mycomboBox.Items.Add(new ListItem("Value To Display", "actual value"));
mycomboBox.Items.Add(new ListItem("First item text", "This was a dog"));
Ok. 好。 Lets consider this simple approach:
让我们考虑这个简单的方法:
For i = 0 to n
set combobox[i] to substring(0, m)
So that every item is just a substring containing the appreciated text to appear. 这样每个项目只是一个子字符串,包含要显示的文本。
If the item was "First item text Data:This was a dog"
, for example, then this code shows only the part "First item text"
: (in C#) 例如,如果项目是
"First item text Data:This was a dog"
,则此代码仅显示"First item text"
:( 在C#中)
string str = "First item text Data:This was a dog";
string shown = str.Substring(0, str.IndexOf("text") + 4);
string hidden = str.Substring(shown.Length);
And simply handle the SelectedIndexChanged
event to update the UI with the data of the selected item. 并简单地处理
SelectedIndexChanged
事件以使用所选项的数据更新UI。
private readonly string[] data = new[] { "This was a dog", "This was a girl" };
and then show data[comboBox.SelectedIndex] 然后显示数据[comboBox.SelectedIndex]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.