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 12th, 2006, 11:52 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default Set cbo.selectedvalue using Value problem persists

I posted this question once already, but somebody responded with a solution that did not work and I suspect nobody else is bothering to look at the question because they think the problem was solved by the response. I could really use a response that actually works:

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.

Finally, the solution suggested to my last posting of this question does not work because the method suggested doesn't even exist:

combobox.Value = 40

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?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Persists.MailSender dammy Classic ASP Components 1 September 6th, 2007 01:02 PM
Need Help with DropDownList SelectedValue putputty ASP.NET 2.0 Basics 4 June 27th, 2007 11:44 AM
Unable to set SelectedValue on DropDownList jlrolin ASP.NET 2.0 Professional 0 April 13th, 2007 09:30 AM
Can't EASILY set cbo.selecteditem using VALUE Ron Howerton Visual Basic 2005 Basics 5 December 14th, 2006 04:52 PM
comboBox.SelectedValue melvik C# 1 August 19th, 2003 09:21 AM





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