vb.net 以编程方式设置组合框的值成员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14638227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:38:48  来源:igfitidea点击:

Set combobox's value member programmatically

vb.netcomboboxvisual-studio-2012selectedvalue

提问by jamesdlivesinatree

I've been looking around for this answer. Checked here: How to set a combobox valuebut I'm not sure if it applies to me (could be wrong, please correct me if I am). I'm using VB.Net, VS2012 and I need to programmatically set the value member of a combobox that is databound.

我一直在寻找这个答案。在这里检查:如何设置组合框值,但我不确定它是否适用于我(可能是错误的,如果我是,请纠正我)。我正在使用 VB.Net、VS2012,我需要以编程方式设置数据绑定的组合框的值成员。

My code now is as follows (this is from within a loop assigning a bunch of controls values):

我的代码现在如下(这是从分配一堆控件值的循环中获得的):

    cboCountry.SelectedValue = row.Item("CCCOUNTRY").ToString

This does not assign any selected value. I have also tried:

这不会分配任何选定的值。我也试过:

    cboCountry.SelectedItem = cboCountry.FindString(row.Item("CCCOUNTRY").ToString)

But this does not work either. For this instance:

但这也不起作用。对于这个例子:

  1. I have one combobox
  2. It has two values databound in it's valuemember properties, "US", and "CA"
  3. The row item that I'm assigning it is one of those values.
  1. 我有一个组合框
  2. 它的值成员属性中有两个数据绑定值,“US”和“CA”
  3. 我分配给它的行项目是这些值之一。

Again, all I need to do is set the selectedvalue programmatically. Any help greatly appreciated!

同样,我需要做的就是以编程方式设置 selectedvalue。非常感谢任何帮助!

回答by xpda

You're close on your second try -- replace SelectedItem with SelectedIndex:

第二次尝试即将结束——用 SelectedIndex 替换 SelectedItem:

cboCountry.SelectedIndex = cboCountry.FindString(row.Item("CCCOUNTRY").ToString)

回答by SUHAIL AG

You should use:

你应该使用:

cboCountry.Items.FindByText(row.Item("CCCOUNTRY").ToString()).Selected = True

回答by Carlos

This worked for me today in vb.net vs2010

这今天在 vb.net vs2010 中对我有用

cboCountry.SelectedIndex = cboCountry.FindString("CCCOUNTRY").ToString

cboCountry.SelectedIndex = cboCountry.FindString("CCCOUNTRY").ToString

The names of the controls and values for me were different, but I continued the previous users posts...

我的控件和值的名称不同,但我继续以前的用户帖子......

回答by CA Martin

This worked for me in a project with VB.NET VS2010

这在 VB.NET VS2010 的项目中对我有用

cboCountry.SelectedIndex = cboCountry.FindString("CCCOUNTRY").ToString

of course the project has different names and values

当然这个项目有不同的名称和价值

回答by Radagast

I have been experiencing the same problem and have not found an answer on the net. Apparently, Microsoft has not fixed this problem yet. I am using VB for a VS2010 WinForms app (in Windows 7). I finally decided to code a work around, Wherever I was trying to preset the selected value of a ComboBox I made the following change:

我也遇到了同样的问题,网上也没有找到答案。显然,微软还没有解决这个问题。我将 VB 用于 VS2010 WinForms 应用程序(在 Windows 7 中)。我最终决定编写一个变通方法,无论我在哪里尝试预设 ComboBox 的选定值,我都进行了以下更改:

' my_cbx.SelectedValue = data_row.value  ' doesn't work!
PresetSelectedValue(my_cbx, data_row.value)

Then, I added the following subroutine to be called by the above changes:

然后,我添加了以下子例程以供上述更改调用:

Public Sub PresetSelectedValue(ByRef ComboBox As ComboBox, ByVal value As Object)
    Dim item_ndx As Integer

    If ComboBox Is Nothing Then
        '   throw exception?
        Exit Sub
    End If
    With ComboBox
        .Tag = "PresetSelectedValue"
        For item_ndx = 0 To .Items.Count - 1
            .SelectedIndex = item_ndx
            If .SelectedValue = value Then
                Exit For
            End If
        Next
        If item_ndx >= .Items.Count Then
            .SelectedIndex = -1
        End If
        .Tag = ""
    End With
End Sub

Finally, I added the following code to my ComboBox.SelectedValueChanged event (this prevents the normal event logic when user changes the value from being executed on every iteration of the For loop in my subroutine above):

最后,我将以下代码添加到我的 ComboBox.SelectedValueChanged 事件(这可以防止在用户更改值时在我上面的子例程中 For 循环的每次迭代中执行的正常事件逻辑):

If my_cbx.Tag = "PresetSelectedValue" Then
    Exit Sub
End If