以编程方式更改Combox的文本(Programmatically Change the text of a Combox)

编程入门 行业动态 更新时间:2024-10-13 12:19:59
以编程方式更改Combox的文本(Programmatically Change the text of a Combox)

我有一个带有组合框的用户表单,有5个未绑定的数据项。 每个项目的值具有以下格式:“##说明”,一个2位数字代码和代码说明。 用户选择一个项目后,我想只显示2位数字代码。 我尝试了以下内容

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged ComboBox1.Text = Mid(ComboBox1.Text, 1, 2) End Sub

但是,选择一个项目后,该分配似乎不能正常工作,因为ComboBox1.Text保持不变。 有任何想法吗? 提前致谢

I have a user form with a combobox, with 5 unbound data items. The value of each item is of the following format: "## Explanation", a 2-digit numeric code and an explanation of the code. After the user selects an item, I would like to have the 2-digit numeric code displayed only. I have tried the following

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged ComboBox1.Text = Mid(ComboBox1.Text, 1, 2) End Sub

However after selecting an item, the assignment doesn't seem to be working properly because ComboBox1.Text remains unchanged. Any ideas? Thanks in advance

最满意答案

对我来说,听起来你并没有真正使用组合框来充分发挥潜力。 看起来你想拥有包含多条信息的项目,而这些信息正在尝试合并。 但在这里你可以做什么

Private Class ComboItem Public Property Code As Integer Public Property Description As String Public ReadOnly Property Display As String Get Return Code & " " & Description End Get End Property End Class Dim lst As New List(Of ComboItem)() lst.Add(New ComboItem()....) ' add your items cboList.DataSource = lst cboList.DisplayMember = "Display" cboList.ValueMember = "Code"

在这里最好的部分开始 - 一旦用户选择一个项目,通过键入或点击,你可以做到这一点

Dim item As ComboItem = DirectCast(cboList.SelectedItem, ComboItem) txtCode.Text = item.Code txtDescription.Text = item.Description

我觉得,这是你真正需要的。

To me, it sounds that you are not really using combo box to full potential. Looks like you want to have items with multiple pieces of info, which you're trying to combine. But here what you can do instead

Private Class ComboItem Public Property Code As Integer Public Property Description As String Public ReadOnly Property Display As String Get Return Code & " " & Description End Get End Property End Class Dim lst As New List(Of ComboItem)() lst.Add(New ComboItem()....) ' add your items cboList.DataSource = lst cboList.DisplayMember = "Display" cboList.ValueMember = "Code"

Here where the best part starts - once user selects an item, by typing or clicking, you can do this

Dim item As ComboItem = DirectCast(cboList.SelectedItem, ComboItem) txtCode.Text = item.Code txtDescription.Text = item.Description

I feel, this is what you really need.

更多推荐

本文发布于:2023-07-30 15:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338863.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   方式   Combox   text   Programmatically

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!