最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】C#中更改ComboBox的子项的值

C# crifan 6197浏览 0评论

【问题】

C#中的ComboBox中,已经手动填入了几个值,供显示了:

combobox edit items

has entered some value for show

也已经初始化时,选择第一个了:

cmbDeliverTo.SelectedIndex = 0;

现在的问题是,

后来的某个时候,想要更改其中的子项中的某个值。

把:

[email protected]

改为:

[email protected]

【解决过程】

1.参考:

C# – Change index of ComboBox item?

去试试:

cmbDeliverTo.Items.Insert(0, "[email protected]");

结果只是在index为0的位置,另外,再插入了一个:

can insert to index 0

不是我所要的,更改原有的index为0的值。

2.所以,再去改为:

            cmbDeliverTo.Items.RemoveAt(0);
            cmbDeliverTo.Items.Insert(0, "[email protected]");

结果就可以了。

但是却默认没有选择了:

default no select when insert ok

所以再添加当前的选择,改为:

            cmbDeliverTo.Items.RemoveAt(0);
            cmbDeliverTo.Items.Insert(0, "[email protected]");
            cmbDeliverTo.SelectedIndex = 0;

然后终于可以了:

insert select index 0 ok

 

【总结】

ComboBox中,没有直接更改某个子项的值。

暂时是用:

comboBox1.Items.RemoveAt(0);
comboBox1.Items.Insert(0, "[email protected]");
comboBox1.SelectedIndex = 0;

实现:

先删除旧的,再插入新的,再确保更新选择的项,而实现,更新index为0的位置的值。

转载请注明:在路上 » 【已解决】C#中更改ComboBox的子项的值

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.168 seconds, using 22.19MB memory