|
-
Feb 14th, 2006, 01:18 PM
#1
Thread Starter
Hyperactive Member
Combobox SelectedIndexChanged
I have a combobox that I am filling with data at form load. I also have code that adds data to another field when the SelectedIndex gets changed. Right now I have a SelectedIndex = -1 after the combobox is filled so that there is no value listed in the combobox when the form is finished loading.
The problem is that that SelectedIndex = -1 is tripping my SelectedIndexChange code block. This seems like I am attempting such a common task that there must be an easy workaround or I am really missing something obvious.
Any ideas?
-
Feb 14th, 2006, 01:22 PM
#2
Re: Combobox SelectedIndexChanged
You should do a check to see if SelectedIndex=-1 in the top of the SelectedIndexChanged method end exit if it is.
I wish I could think of something witty to put in my sig...
...Currently using VS2013...
-
Feb 14th, 2006, 02:09 PM
#3
Thread Starter
Hyperactive Member
Re: Combobox SelectedIndexChanged
Thanks Pax. I tried that but since the records in the combobox get loaded and ordered alphabeticaly the IndexNumber starts out at 23 (The 23rd Record is (Abe) and then gets set to -1 as the form finishes loading, which is when the IndexXhanged gets fired.
Doing it this way the 23 is what gets tested not the -1. And I can't test for a 23 all the time either because it is possible that a record will get added that will be Aarron and will move ahead of record 23.
Anything else you can think of that I can try?
-
Feb 14th, 2006, 02:30 PM
#4
Re: Combobox SelectedIndexChanged
I use this same basic type of code every where I use a combobox:
VB Code:
If Me.cboLevel.SelectedIndex < 0 Then
MessageBox.Show("You must select a User Level for this new user." & System.Environment.NewLine & _
"Please select a User Level from the provided list.", "No User Level Selected", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Me.cboLevel.Focus()
Exit Sub
Else
intLevel = Me.intUserLevels(Me.cboLevel.SelectedIndex)
End If
if the selectedindex is < 0 then the procedure exits. I also fill the combo box alphabetically, But if nothing is selected in the box the selectedindex is equal to -1
if the first item is selected then the selectedindex = 0 and so on...
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Feb 14th, 2006, 03:05 PM
#5
Thread Starter
Hyperactive Member
Re: Combobox SelectedIndexChanged
Here is my entire codeblock
VB Code:
Private Sub cboMarketPiece_SelectedindexChanged( _
ByVal sender As Object, ByVal e As System.EventArgs) _
Handles cboMarketPiece.SelectedValueChanged
If cboMarketPiece.SelectedIndex < 0 Then
cboMarketPiece.Focus()
Exit Sub
Else
Dim cnn As New OleDbConnection
Dim cmd As New OleDbCommand
Dim dr As OleDbDataReader
cnn.ConnectionString = conBCA
cmd = cnn.CreateCommand
cmd.CommandText = "SELECT Number From " & _
"queMarketingPiece Where MarketingPiece = '" & _
cboMarketPiece.Text & "'"
Try
cnn.Open()
dr = cmd.ExecuteReader
While dr.Read()
txtMpCode.Text = dr("Number").ToString
End While
dr.Close()
Catch ex As Exception
Exit Sub
Finally
cnn.Close()
End Try
End If
End Sub
The txtMpCode.Text = dr("Number").ToString is filling that box when it should not be because I set the Index to -1 at the form load. I did come up with a quick and dirty work around and that is to add this to the form load.
VB Code:
Private Sub frmCreateBarcode_Load( _
ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Load
FillMarketingPiece()
FillCouponOffer()
txtMpCode.Clear()
txtCoCode.Clear()
End Sub
' The last line in FillCouponOffer and FillMarketingPieceOffer is cboXXX.SelectedIndex = -1
That seems to be doing the trick.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|