简体   繁体   English

C#如何在Design中设置字符串集合的默认值

[英]C# How to set default value of a string collection in Design

In WinForm, I have a combobox with DropDownStyle set to DropDownList (so can't enter a Text). 在WinForm中,我有一个组合框,DropDownStyle设置为DropDownList(因此无法输入文本)。 In the properties window, there is the Items property which is a string collection. 在属性窗口中,有Items属性,它是一个字符串集合。 I enter all my values. 我输入了所有的价值观。

But now, I would like to set one of these value by default (instead to have the empty entry at run-time). 但是现在,我想默认设置其中一个值(而不是在运行时使用空条目)。 I know how to do this via coding, but I am pretty sure (damn memory) that it was possible to set one of the value in the string collection as default by adding a special symbole in front of the line. 我知道如何通过编码来做到这一点,但我很确定(该死的记忆)可以通过在行前面添加一个特殊的符号来设置字符串集合中的一个值作为默认值。

Anybody know that symbole? 有人知道那个符号吗? Or my memory is playing me trick and it is not possible to do it via the designer? 或者我的记忆在玩耍技巧,而且不可能通过设计师来做到这一点?

Doesn't look like it can be done when using a DropDownList. 使用DropDownList时看起来不像。 From here it is suggested that you can set the text property to the default value you want, but this will only works in a DropDown rather than DropDownList style. 这里建议你可以将text属性设置为你想要的默认值,但这只适用于DropDown而不是DropDownList样式。

I'm sorry but that is not possible within the Designer only, since the Text property is used for this feature and that property is ignored/cleared when the using a DropDownList. 很抱歉,但仅在Designer中无法实现,因为Text属性用于此功能,并且在使用DropDownList时会忽略/清除该属性。

If you don't mind having your data values outside of the Designer, you could probably use DataBinding to accomplish this since the DisplayMember and ValueMember properties of ComboBox can be used in the Designer and would set the display value. 如果您不介意在Designer之外使用数据值,则可以使用DataBinding来完成此操作,因为ComboBoxDisplayMemberValueMember属性可以在Designer中使用设置显示值。 I don't normally use DataBinding so unfortunately I cannot provide code examples - perhaps another user can chime in? 我通常不使用DataBinding所以很遗憾我无法提供代码示例 - 也许其他用户可以插入?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM