Visual Basic .NET Forums  
Click here to advertise with us

Go Back   Visual Basic .NET Forums > VB.NET > Windows Forms

Windows Forms Discussion related to Winforms application development

VB.NET Forums Newsletter Signup:
Email address:


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-08-2009, 2:08 PM
VB.NET Forum Enthusiast
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Feb 2008
Posts: 33
Reputation: 27
tcl4p is on a distinguished programming path ahead
Default How to return ValueMember from combobox

I'm loading a combobox from a stored procedure, which appears to be working fine(see code below). So now I want to get the ValueMember from the combobox when the user selects a value and here's where my problem starts.

I figured I could get the ValueMember from the SelectedIndexChanged event by using the following:
comboboxValue = Me.cboCategory.SelectedValue
When I use this code I get an error (Argument 'Prompt' cannot be converted to type 'String'.) during what appears to be the loading of the combobox.

I can however remove that code, add a button to the form and after a value is selected from the combobox, click the button and the same code returns the correct value.

I next tried to drop a message box to see the SelectedIndex value in the SelectedIndexChanged event and to my amazement it appeared that during the loading of the combo box the message box show three values, being 0,0 and -1 respectively.

I next tried the Click event of the combobox adding a messagebox returning the msgbox(SelectedIndex), but as soon as I click on the drop down arrow on the combo box the message box comes up with a -1 value and closes the combo box.

So the questions are:
1. Am I loading the combobox correctly from the stored procedure?
2. Why is the combobox returning index values (0,0,-1) during the load?
3. Why is the Click event return -1 when I click the drop down on the combobox?

If I'm doing something wrong could someone give me some code examples on how to return the ValueMember from the combobox and which event should I use.

Thanks,
Tom



Public Function LoadCategories(ByVal cboBox As ComboBox) As Boolean
Dim cmdsub As New SqlCommand
Dim da As New SqlDataAdapter(cmdsub)
Dim ds As New DataSet()

If objConn.State <> ConnectionState.Open Then
objConn = objConnClass.GetConnection
End If
cmdsub.Connection = objConn
cmdsub.CommandText = "GetVendorCategories"
cmdsub.CommandType = CommandType.StoredProcedure
cmdsub.Parameters.Add("@rc", SqlDbType.Int).Direction = ParameterDirection.ReturnValue

Try
da.Fill(ds)
da.Fill(ds, "Disk")

With cboBox
.DataSource = ds.Tables("Disk")
.DisplayMember = "VendorCategory"
.ValueMember = "vcID"
.SelectedIndex = -1
End With
return true
Catch ex As SqlException
MsgBox("Error Encountered In Loading Vendor Categories." & ex.Message, MsgBoxStyle.OkOnly, "Error")
Return false
cmdsub.Dispose()
objConn.Dispose()
End Try
End Function
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-08-2009, 8:19 PM
jmcilhinney's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Aug 2004
Location: Sydney, Australia
Age: 40
Posts: 6,100
Reputation: 541
jmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalist
Default

The SelectedIndexChanged event is raised every time the SelectedIndex property value changes. If your ComboBox is initially empty then its SelectedIndex is -1. When you bind data to it the first item is selected by default and the SelectedIndex changes to 0. You are then explicitly setting the SelectedIndex to -1 again.

If you're only interested in knowing when the user makes a selection then handle the SelectionChangeCommitted event instead.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-09-2009, 9:27 PM
VB.NET Forum Enthusiast
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Feb 2008
Posts: 33
Reputation: 27
tcl4p is on a distinguished programming path ahead
Default thanks

You suggestion worked fine and I thank you for taking the time to help.

Regards,
Tom
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





All times are GMT -4. The time now is 3:01 PM.

Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2


For advertising opportunities click here.