Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Visual Basic 2005 Basics
|
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 7th, 2006, 09:37 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default Can't EASILY set cbo.selecteditem using VALUE

I have a combo box on a Windows form populated from a table looking something like this:

DISPLAYMEMBER VALUEMEMBER INDEX

Select Name 0 0
Name1 193 1
Name2 40 2

The user selects an Item from the cbo and the VALUE is saved to another table. When the user later edits the saved data, I want to automatically select the VALUE from the combobox to reflect the previous selection. The only way I have been able to select the item I want by VALUE is by looping and checking every VALUE to find the one I want:

For intPos As Integer = 0 To .Count - 1
  If comboBox.Item(intPos)("Value") = v_intValue Then
    comboBox.SelectedIndex = intPos

    Exit Sub
  End If
Next

It has to do this for over 40 comboboxes on the form, and as you can imagine, that's not exactly efficient. It's all the more frustrating that the ASP version of the combobox supports the Items.FindByValue method to determine the combobox index of the value in question (exactly what I want) and the windows control does not (Hello, Microsoft?!?!?!)

I've found sites claiming this will work, but it doesn't:

combobox.SelectedValue = 40

I know for a fact that VALUE 40 exists in the combobox, but if I query SelectedValue after attempting to set it, it returns NOTHING and SelectedIndex is still 0. I've also seen suggestions that I try this:

combobox.SelectedIndex = combobox.FindString("40")

But FindString appears to only search the DISPLAYMEMBER, and not VALUEMEMBER, and the code above returns -1. Another dead end.

This seems like a pretty common need (and it's been implemented properly in ASP). Isn't there a more efficient way of selecting a combobox item using using VALUEMEMBER in a Windows form?
 
Old July 7th, 2006, 12:40 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Have you tried
Code:
    combobox.Value = 40
?
 
Old July 7th, 2006, 12:50 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

combobox.Value doesn't appear to exist.
 
Old December 5th, 2006, 09:33 PM
Registered User
 
Join Date: Dec 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Amen brother!
I was hoping to find a solution here, but I'm just glad to see that someone else is having the same problem, What a load of you-know-what. When you find something out PLEASE let me know!

 
Old December 7th, 2006, 07:38 AM
Authorized User
 
Join Date: Aug 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anubisascends
Default

Where did you set the value member, is it getting the info from the database.

Have you checked to see what your selected values are? I know this works, as I use this heavily in my programs to run querys to my databases. You need to make sure that they value member is pointed to the correct database item.

The program won't assign a value member to the control, if you don't create one.

Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.
Albert Einstein
US (German-born) physicist (1879 - 1955)
 
Old December 14th, 2006, 04:52 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

I do not typically use bound controls because they rarely function as desired. I prefer to populate them myself so I can do things like adding "Select a value" as the default, etc. And the value most certainly does exist - I can find it by looping through all the VALUES in the combobox using the code described in the original post, which is clearly pointing to the combobox value property.

However, as anubisascends insists this code will work (not sure exactly which code he was refering to), I tried them all again. I can still find no "cbo.value" property whatsoever. So I assume that's not the code he's referring to. Nor does "combobox.SelectedIndex = combobox.FindString("40")" work any better than it did last time, despite the fact that there is such a VALUE associated with an ITEM in the combobox.

I have noticed that if the DISPLAY column happens to begin with the very same text as the VALUE Column it will find the value (findstring does a substring search of the DISPLAY Column only), but not if there's a difference, which may be why his (?) code works where mine does not:

DISPLAY Value
Employee E
Executive X
Supervisor S

cbo.findstring("S") returns 2, but cbo.findstring("X") returns 0, incorrectly selecting Employee rather than Executive. According to Microsoft at:

http://msdn2.microsoft.com/en-us/library/wxyt1t12.aspx

I should be getting a -1 since none of the DISPLAY coumn values begin with "X", so returning a "0" when neither the DISPLAY or VALUE column for index 0 starts with an "X" appears to be a VS/FW error to me.

In any case, perhaps anubisascends' DISPLAY columns start with the same character(s) as his VALUE columns and that's why it works for him, because in my observation it works only for the condition described above, and is not only non-functional when the VALUE bears no resemblance to the DISPLAY value, but appears to be returning an erroneous result. Or perhaps the difference is that I am trying to select a value in a populated combobox, not retrieve data from a table as he appears to be doing? In any case, these solutions do not work in my instance.

I have yet to find something that will work other than the code shown earlier, but frankly have moved on to other projects at this late date anyway. Sorry to those hoping to find a solution here, but AFAIK, there is none and the solutions suggested in this thread do not appear to work.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Listview SelectedItem Apocolypse2005 Visual Basic 2005 Basics 1 October 28th, 2007 01:08 PM
Set cbo.selectedvalue using Value problem persists Ron Howerton Visual Basic 2005 Basics 0 July 12th, 2006 11:52 AM
Listbox selecteditem to string Alcapone C# 2 October 28th, 2004 02:18 AM
SelectedItem in ComboBox miguel.ossa C# 1 September 28th, 2004 06:50 PM
SelectedItem.Value vs. SelectedItem.Text bekim BOOK: Beginning ASP.NET 1.0 2 June 17th, 2004 07:23 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.