在 winforms 中设置组合框的选定文本的正确方法?
Right way to set selected text of combobox in winforms?
我有一个表单,它在其构造函数中接受一个对象,并根据该对象的属性在表单上填充控件。我遇到一个问题,我无法设置 ComboBox 的 SelectedText
属性,或者至少它没有按照我的预期工作。
public Form(ValueHoldingObject obj)
{
// yeah I know this is not a very clean way to populate the combobox, the issue
// isn't limited to the combobox so I don't think this is relevant
List<int> items = Repo.GetAllItems().Reverse();
foreach (int id in checkInPrizeIds.Take(100))
// Insert at beginning to put more recently used items at the top
combobox.Items.Insert(0, id);
combobox.DropDownHeight = 200;
combobox.SelectedText = obj.StringProperty;
}
当我测试此表单时,未填充组合框的文本。如果我在分配文本的行上添加一个断点,它确实会被分配,所以某些事件正在触发(可能是多个焦点更改事件)并使其按我想要的方式工作。显然我不能在生产代码中使用断点作为修复。我是否错误地分配了这个值?我应该使用不同的方法来填充值吗?
进一步的测试表明它不仅仅是组合框,只有当我有断点时,我的所有控件才会正确填充。
在构造函数中,需要设置selected项,例如:
foreach ( var item in combobox.Items )
if ( (string)item == obj.StringProperty )
combobox.SelectedItem = item;
或:
foreach ( var item in combobox.Items )
if ( (int)item == Convert.ToInt32(obj.StringProperty) )
combobox.SelectedItem = item;
令人困惑,但尽管它的名字如此,属性 SelectedText
并不是真正的 selected 项目...因为组合框项目是对象而不是字符串:显示的文本是使用 ToString()
.
的项目对象表示
因此设置 selected 文本并不能保证 select 一个项目,我们更喜欢设置 SelectedItem
.
除了这些注意事项之外,您在填充组合框后在构造函数中设置 selected 文本 属性,这可能会导致问题,因为它是在绘制窗体和控件之前或类似的东西......也就是说,也许在项目上调用 ToString()
方法以准备视觉缓存之前,因此设置 selected 文本无法与列表匹配。
设置 selected 文本 select 为现有项目(如果在表单加载或显示事件中完成)。
private void Form_Load(object sender, EventArgs e)
{
combobox.SelectedText = obj.StringProperty;
}
ComboBox.SelectedText doesn't give me the SelectedText
我有一个表单,它在其构造函数中接受一个对象,并根据该对象的属性在表单上填充控件。我遇到一个问题,我无法设置 ComboBox 的 SelectedText
属性,或者至少它没有按照我的预期工作。
public Form(ValueHoldingObject obj)
{
// yeah I know this is not a very clean way to populate the combobox, the issue
// isn't limited to the combobox so I don't think this is relevant
List<int> items = Repo.GetAllItems().Reverse();
foreach (int id in checkInPrizeIds.Take(100))
// Insert at beginning to put more recently used items at the top
combobox.Items.Insert(0, id);
combobox.DropDownHeight = 200;
combobox.SelectedText = obj.StringProperty;
}
当我测试此表单时,未填充组合框的文本。如果我在分配文本的行上添加一个断点,它确实会被分配,所以某些事件正在触发(可能是多个焦点更改事件)并使其按我想要的方式工作。显然我不能在生产代码中使用断点作为修复。我是否错误地分配了这个值?我应该使用不同的方法来填充值吗?
进一步的测试表明它不仅仅是组合框,只有当我有断点时,我的所有控件才会正确填充。
在构造函数中,需要设置selected项,例如:
foreach ( var item in combobox.Items )
if ( (string)item == obj.StringProperty )
combobox.SelectedItem = item;
或:
foreach ( var item in combobox.Items )
if ( (int)item == Convert.ToInt32(obj.StringProperty) )
combobox.SelectedItem = item;
令人困惑,但尽管它的名字如此,属性 SelectedText
并不是真正的 selected 项目...因为组合框项目是对象而不是字符串:显示的文本是使用 ToString()
.
因此设置 selected 文本并不能保证 select 一个项目,我们更喜欢设置 SelectedItem
.
除了这些注意事项之外,您在填充组合框后在构造函数中设置 selected 文本 属性,这可能会导致问题,因为它是在绘制窗体和控件之前或类似的东西......也就是说,也许在项目上调用 ToString()
方法以准备视觉缓存之前,因此设置 selected 文本无法与列表匹配。
设置 selected 文本 select 为现有项目(如果在表单加载或显示事件中完成)。
private void Form_Load(object sender, EventArgs e)
{
combobox.SelectedText = obj.StringProperty;
}
ComboBox.SelectedText doesn't give me the SelectedText