PeterBaileyUk
asked on
vb.net listbox event code not working as desired
I have a form with a listbox and in the listbox are chords based on combo selection. I just cannot get the list box code right, i think its the right event. in the example you can see 7 chords each chord position on the grid is directly related to the index position of the listbox. so 0 is panel 1 bottom left 1 is panel 2 2 is panel3
the grid sequence is
789
456
123
I had hoped that after a selection the grid would update so if i multi select cMin and d# aug that the panel 1 and panel 3 go green
my code does not do that. if i select 3 i want 3 to go green if i deselect 1 of the three i would like to see 2
the grid sequence is
789
456
123
I had hoped that after a selection the grid would update so if i multi select cMin and d# aug that the panel 1 and panel 3 go green
my code does not do that. if i select 3 i want 3 to go green if i deselect 1 of the three i would like to see 2
Imports System
Imports System.Text.RegularExpressions
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call GetCBData("usp_GetNotes")
Call GetCBData("usp_GetScaleTypes")
Call ClearCaption()
Call ClearColor()
End Sub
Private Sub GetCBChords()
Dim StrProcName As String
Dim dtChord As DataTable
Dim Stri As String
Dim Strii As String
Dim Striii As String
Dim Striv As String
Dim Strv As String
Dim Strvi As String
Dim Strvii As String
Dim Strdim7 As String
Dim connectionString As String = "Data Source=MAIN-PC\SQLEXPRESS;Initial Catalog=PushCircle;Integrated Security=True;MultipleActiveResultSets=True"
dtChord = New DataTable
CBChordList.Items.Clear()
ListBoxChords.Items.Clear()
StrProcName = "usp_GetChords"
Using connChord As New SqlConnection(connectionString)
Using cmdChord As New SqlCommand(StrProcName, connChord)
cmdChord.CommandType = CommandType.StoredProcedure
connChord.Open()
cmdChord.Parameters.AddWithValue("@Tonic", CBNote.SelectedItem.ToString())
cmdChord.Parameters.AddWithValue("@ScaleType", CBScaleType.SelectedItem.ToString())
Dim reader As SqlDataReader = cmdChord.ExecuteReader()
dtChord.Load(reader)
For Each drRecord As DataRow In dtChord.Rows
Stri = drRecord("i")
Strii = drRecord("ii")
Striii = drRecord("iii")
Striv = drRecord("iv")
Strv = drRecord("v")
Strvi = drRecord("vi")
Strvii = drRecord("vii")
Strdim7 = drRecord("dim7")
Next
If CheckPopulated(Stri) Then CBChordList.Items.Add(Stri)
If CheckPopulated(Strii) Then CBChordList.Items.Add(Strii)
If CheckPopulated(Striii) Then CBChordList.Items.Add(Striii)
If CheckPopulated(Striv) Then CBChordList.Items.Add(Striv)
If CheckPopulated(Strv) Then CBChordList.Items.Add(Strv)
If CheckPopulated(Strvi) Then CBChordList.Items.Add(Strvi)
If CheckPopulated(Strvii) Then CBChordList.Items.Add(Strvii)
If CheckPopulated(Strdim7) Then CBChordList.Items.Add(Strdim7)
ListBoxChords.SelectionMode = SelectionMode.MultiExtended
' Shutdown the painting of the ListBox as items are added.
ListBoxChords.BeginUpdate()
If CheckPopulated(Stri) Then ListBoxChords.Items.Add(Stri)
If CheckPopulated(Strii) Then ListBoxChords.Items.Add(Strii)
If CheckPopulated(Striii) Then ListBoxChords.Items.Add(Striii)
If CheckPopulated(Striv) Then ListBoxChords.Items.Add(Striv)
If CheckPopulated(Strv) Then ListBoxChords.Items.Add(Strv)
If CheckPopulated(Strvi) Then ListBoxChords.Items.Add(Strvi)
If CheckPopulated(Strvii) Then ListBoxChords.Items.Add(Strvii)
If CheckPopulated(Strdim7) Then ListBoxChords.Items.Add(Strdim7)
ListBoxChords.EndUpdate()
End Using
End Using
End Sub
Function CheckPopulated(ByVal StrIn As String) As Boolean
Dim StrInput As String
StrInput = StrIn
If StrInput = "-" Then
Return False
Else
Return True
End If
End Function
Private Sub GetCBData(ByVal UspValue As String)
Dim StrProcName As String
Try
StrProcName = UspValue
Dim connectionString As String = "Data Source=MAIN-PC\SQLEXPRESS;Initial Catalog=PushCircle;Integrated Security=True;MultipleActiveResultSets=True"
Using conNote As New SqlConnection(connectionString)
Using cmdNote As New SqlCommand(StrProcName, conNote)
If conNote.State = ConnectionState.Closed Then
conNote.Open()
Dim reader As SqlDataReader = cmdNote.ExecuteReader()
' Fill a combo box with the datareader
Do While reader.Read() = True
Select Case UspValue
Case = "usp_GetNotes"
CBNote.Items.Add(reader.GetString(0))
Case = "usp_GetScaleTypes"
CBScaleType.Items.Add(reader.GetString(0))
End Select
Loop
conNote.Close()
End If
End Using
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub CBNote_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CBNote.SelectedIndexChanged
If ((CBNote.SelectedIndex = -1) Or (CBScaleType.SelectedIndex = -1)) Then
Else
Call GetCBChords()
Call ClearCaption()
Call ClearColor()
End If
End Sub
Private Sub CBScaleType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CBScaleType.SelectedIndexChanged
If ((CBNote.SelectedIndex = -1) Or (CBScaleType.SelectedIndex = -1)) Then
Else
Call GetCBChords()
Call ClearCaption()
Call ClearColor()
End If
End Sub
Private Sub CBNote_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'CBNote.Text = "C"
End Sub
Private Sub CBScaleType_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'CBScaleType.Text = "Major"
End Sub
Private Sub ClearCaption()
lb1.Text = ""
lb2.Text = ""
lb3.Text = ""
lb4.Text = ""
lb5.Text = ""
lb6.Text = ""
lb7.Text = ""
lb8.Text = ""
lb9.Text = ""
lb10.Text = ""
lb11.Text = ""
lb12.Text = ""
lb13.Text = ""
lb14.Text = ""
lb15.Text = ""
lb16.Text = ""
lb17.Text = ""
lb18.Text = ""
lb19.Text = ""
lb20.Text = ""
End Sub
Private Sub ClearColor()
Pn1.BackColor = Color.White
Pn2.BackColor = Color.White
pn3.BackColor = Color.White
Pn4.BackColor = Color.White
Pn5.BackColor = Color.White
Pn6.BackColor = Color.White
Pn7.BackColor = Color.White
Pn8.BackColor = Color.White
Pn9.BackColor = Color.White
Pn10.BackColor = Color.White
Pn11.BackColor = Color.White
Pn12.BackColor = Color.White
Pn13.BackColor = Color.White
Pn14.BackColor = Color.White
Pn15.BackColor = Color.White
Pn16.BackColor = Color.White
Pn17.BackColor = Color.White
Pn18.BackColor = Color.White
Pn19.BackColor = Color.White
Pn20.BackColor = Color.White
End Sub
Private Sub CBChordList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CBChordList.SelectedIndexChanged
Dim selectedItem = CBChordList.SelectedItem
Dim selectedIndex As Integer = CBChordList.SelectedIndex
Call ClearCaption()
Call ClearColor()
Select Case selectedIndex
Case 0
lb1.Text = selectedItem
lb7.Text = selectedItem
lb3.Text = selectedItem
Pn1.BackColor = Color.Green
Pn7.BackColor = Color.Green
pn3.BackColor = Color.Green
Case 1
lb2.Text = selectedItem
lb8.Text = selectedItem
lb4.Text = selectedItem
Pn2.BackColor = Color.Green
Pn8.BackColor = Color.Green
Pn4.BackColor = Color.Green
Case 2
lb3.Text = selectedItem
lb9.Text = selectedItem
lb5.Text = selectedItem
pn3.BackColor = Color.Green
Pn9.BackColor = Color.Green
Pn5.BackColor = Color.Green
Case 3
lb6.Text = selectedItem
lb12.Text = selectedItem
lb8.Text = selectedItem
Pn6.BackColor = Color.Green
Pn12.BackColor = Color.Green
Pn8.BackColor = Color.Green
Case 4
lb7.Text = selectedItem
lb13.Text = selectedItem
lb9.Text = selectedItem
Pn7.BackColor = Color.Green
Pn13.BackColor = Color.Green
Pn9.BackColor = Color.Green
Case 5
lb8.Text = selectedItem
lb14.Text = selectedItem
lb10.Text = selectedItem
Pn8.BackColor = Color.Green
Pn14.BackColor = Color.Green
Pn10.BackColor = Color.Green
Case 6
lb11.Text = selectedItem
lb17.Text = selectedItem
lb13.Text = selectedItem
Pn11.BackColor = Color.Green
Pn17.BackColor = Color.Green
Pn13.BackColor = Color.Green
Case 7
lb12.Text = selectedItem
lb18.Text = selectedItem
lb14.Text = selectedItem
Pn12.BackColor = Color.Green
Pn18.BackColor = Color.Green
Pn14.BackColor = Color.Green
Case 8
lb13.Text = selectedItem
lb19.Text = selectedItem
lb15.Text = selectedItem
Pn13.BackColor = Color.Green
Pn19.BackColor = Color.Green
Pn15.BackColor = Color.Green
End Select
End Sub
Private Sub ListBoxChords_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBoxChords.SelectedIndexChanged
For Each Item In ListBoxChords.SelectedItems
If Not String.IsNullOrWhiteSpace(Item) Then
Select Case ListBoxChords.SelectedIndex()
Case 0
lb1.Text = Item.ToString()
Pn1.BackColor = Color.Green
Case 1
lb2.Text = Item.ToString()
Pn2.BackColor = Color.Green
Case 2
lb3.Text = Item.ToString()
pn3.BackColor = Color.Green
Case 3
lb6.Text = Item.ToString()
Pn6.BackColor = Color.Green
Case 4
lb7.Text = Item.ToString()
Pn7.BackColor = Color.Green
Case 5
lb8.Text = Item.ToString()
Pn8.BackColor = Color.Green
Case 6
lb11.Text = Item.ToString()
Pn11.BackColor = Color.Green
Case 7
lb12.Text = Item.ToString()
Pn12.BackColor = Color.Green
Case 8
lb13.Text = Item.ToString()
Pn13.BackColor = Color.Green
End Select
End If
Next
End Sub
End Class
ee.JPG
Hi,
Please share your designer form as well. So it will help us to design the form as well
Please share your designer form as well. So it will help us to design the form as well
ASKER
how do i pull that tab up? cannot find it
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ive actually got the code right now but i will assign the points as I now found the form designer vb code which I didnt know about. That will help me next time.
No issue. You can close this request by not assigning any points to me. :)
You got the solution that's the important over here.
You got the solution that's the important over here.
ASKER
Open in new window