在VB中将组合框的值更改为另一个值。

huangapple go评论49阅读模式
英文:

Changing Value of Combo Box to another Value in VB

问题

我试图将组合框的值从"Black Shredded - 7.90"更改为在选中时只显示"Black Shredded"

Dim intIndex As Integer
Dim strString1 As String
Dim strString2 As String

strString1 = cboProduct.SelectedItem
intIndex = strString1.IndexOf(" ")
strString2 = strString1.Remove(intIndex + 9)

If cboProduct.SelectedIndex = 0 Then
    cboProduct.Text = strString2
End If

我已经检查了这些值,它们显示正常,但是它没有更改组合框的值,我可能做错了什么?

英文:

I am trying to change the value of a combo box value "Black Shredded - 7.90" to just show "Black Shredded" when it is selected

    Dim intIndex As Integer
    Dim strString1 As String
    Dim strString2 As String

    strString1 = cboProduct.SelectedItem
    intIndex = strString1.IndexOf(" ")
    strString2 = strString1.Remove(intIndex + 9)

    If cboProduct.SelectedIndex = 0 Then
        cboProduct.Text = strString2
    End If

I went through the values and they show as they should but it isn't changing the combobox value what could I be doing wrong?

答案1

得分: 0

以下是翻译好的部分:

如果您刚刚在首次将 Strings 添加到 ComboBox 中,那么您需要将现有项替换为新值。这样做:

cboProduct.Text = strString2

应该改成这样:

cboProduct.Items(cboProduct.SelectedIndex) = strString2

在这一点上,您已经确认了这就是该位置的索引,所以可以直接使用 0 代替 cboProduct.SelectedIndex

设置 Text 属性不会对项产生任何影响。如果 DropDownStyle 设置为 DropDown,则将显示指定的文本,但不会选择任何项。如果 DropDownStyle 设置为 DropDownList,则将选择具有该文本的项(如果存在)。无论哪种方式,都不会添加或更改任何项。

英文:

If you have just added Strings to the ComboBox in the first place then you need to replace the existing item with the new value. This:

cboProduct.Text = strString2

should be this:

cboProduct.Items(cboProduct.SelectedIndex) = strString2

You can just use 0 rather than cboProduct.SelectedIndex, given that you have already confirmed that that is the index at that point.

Setting the Text property doesn't affect the items at all. If DropDownStyle is set to DropDown then the specified text will be displayed but no item will be selected. If DropDownStyle is set to DropDownList then the item with that text will be selected, if one exists. Either way, no item is added or changed.

huangapple
  • 本文由 发表于 2023年2月14日 08:02:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定