none
Populating a ListBox with Recipe Names. RRS feed

  • Question

  • Hi ther. I am starting to get the basics of the DataBinding down now but I still need a simple way of having alist of Recipe names displayed in the ListBox.  

    Please Help with some sampel code.

    Tuesday, February 4, 2020 1:48 PM

Answers

  • Hi VB Novice Hendri,

    I find the thread is mostly the same as the other thread, so hope the link can help other members to find the solution quickly if they face a similar issue. 

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 13, 2020 9:08 AM
    Moderator

All replies

  • You need a ListBox for recipe names, last time I looked you were populating several TextBox controls using a data reader, here is what you need instead.

    Place a ListBox on the form, name it RecipeNamesListBox

    Create the following class

    Public Class Recipe
        Public Property Id() As Integer
        Public Property Name() As String
        Public Property Con1() As String
        Public Property Con2() As String
        Public Property Con3() As String
        Public Property Con4() As String
        Public Property Con5() As String
        Public Property Con6() As String
        Public Property Con7() As String
        Public Property Con8() As String
        Public Overrides Function ToString() As String
            Return Name
        End Function
    End Class

    Then adjust the form code e.g.

    Private Sub TitleListBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TitleListBox.SelectedIndexChanged
    
        Try
    
            Dim tableName As String = TitleListBox.SelectedItem.ToString()
    
            Using cn As OleDbConnection = New OleDbConnection(connString)
    
                cn.Open()
    
                Dim cmdText As String =
                        "SELECT ID, [Recipe Name],Caption,Delivery, [Con 1] as Con1, [Con 2] as con2, " &
                        "[Con 3] as con3, [Con 4] as con3, [Con 5] as con5, [Con 6] as con6, " &
                        "[Con 7] as con7, [Con 8] as con8 FROM [" & tableName & "]"
    
                Using cmd As OleDbCommand = New OleDbCommand(cmdText, cn)
    
                    Dim reader = cmd.ExecuteReader()
    
                    If reader.HasRows Then
                        Dim RecipeList As New List(Of Recipe)
                        While reader.Read()
                            RecipeList.Add(New Recipe() With {
                                              .Id = reader.GetInt32(0),
                                              .Name = reader.GetString(1),
                                              .Con1 = reader.GetString(2),
                                              .Con2 = reader.GetString(3),
                                               .Con3 = reader.GetString(4),
                                               .Con4 = reader.GetString(5),
                                               .Con5 = reader.GetString(6),
                                               .Con6 = reader.GetString(7),
                                               .Con7 = reader.GetString(8),
                                               .Con8 = reader.GetString(9)
                                              })
                        End While
    
                        RecipeNamesListBox.DataSource = RecipeList
    
                    End If
                End Using
            End Using
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    
    End Sub
    
    Get that to work and we can move to showing ingredients. 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, February 4, 2020 2:53 PM
    Moderator
  • I have done a bit of work on a way I understand without using Classes, it confuse the hack out of me.  

    Please show me a simple way to get the recipe names into a ListBox, I feel I am allmost ther, I wil show you what I've done as soon as it works, for now it looks a bit messy.  

    Tuesday, February 4, 2020 6:22 PM
  • I have done a bit of work on a way I understand without using Classes, it confuse the hack out of me.  

    Please show me a simple way to get the recipe names into a ListBox, I feel I am allmost ther, I wil show you what I've done as soon as it works, for now it looks a bit messy.  

    If I saw a simple method then you will never learn about classes and that is something you need to learn as using a class especially in this case keeps things simple while what you would call simple makes things complicated later on.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, February 4, 2020 6:48 PM
    Moderator
  • What is this code sepose to do, I dont get it.

    Imports System.Data.OleDb
    Imports System.IO
    
    Public Class ToolsForm
    
        Dim MyParent As Form1
        'Dim connString As String
    
        Dim RecipeControlHeadingFont As New System.Drawing.Font("Arial Rounded MT Bold", 14)
        Dim RecipeControlFont As New System.Drawing.Font("Arial Rounded MT Bold", 11)
    
        Private Sub ToolsForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            MyParent = DirectCast(Me.Owner, Form1)
            'MyParent.ChildStatus = 1
    
            Me.Location = New Point(1050, 150)
            Me.Size = New Size(347, 500)
    
            Me.Input_Label.Text = "Wat doen ons vandag?"
            '& vbCrLf & "" & vbCrLf & "Plaas ons 'n Nuwe Resep?" & vbCrLf & "of" & vbCrLf & "Soek  ons na 'n Bestaande Resep?"
            Me.Input_Label.Font = RecipeControlHeadingFont
    
            Me.New_Recipe_Button.Location = New Point(Me.New_Recipe_Button.Left, Me.Input_Label.Bottom + 5)
            Me.Save_View_Button.Location = New Point(Me.Save_View_Button.Left, Me.Input_Label.Bottom + 5)
    
            Me.New_Recipe_Button.Text = "Plaas 'n Nuwe Resep"
            Me.New_Recipe_Button.Size = New Size(158, 25)
            Me.New_Recipe_Button.Font = RecipeControlFont
    
            Me.Save_View_Button.Text = "Soek  na 'n Resep"
            Me.Save_View_Button.Size = New Size(158, 25)
            Me.Save_View_Button.Font = RecipeControlFont
    
    
           
    
        End Sub
    
        'Private ConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Hoofgeregte.accdb"
        Private ConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=M:\My Documents\My Resepte\Hoofgeregte.accdb"
        'Private ActiveTabel As String
        'Private BindingSource As New BindingSource
    
        Private Sub CategoryComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CategoryComboBox.SelectedIndexChanged
    
        End Sub
    
        Private Sub TitleListBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TitleListBox.SelectedIndexChanged
            Try
    
                Dim tableName As String = TitleListBox.SelectedItem.ToString()
    
                Using cn As OleDbConnection = New OleDbConnection(ConnectionString)
    
                    cn.Open()
    
                    Dim cmdText As String =
                            "SELECT ID, RecipeName, Comment, Delivery, Con1, con2, " &
                            "con3, con4, con5, con6, " &
                            "con7, con8 FROM [" & tableName & "]"
    
                    Using cmd As OleDbCommand = New OleDbCommand(cmdText, cn)
    
                        Dim reader = cmd.ExecuteReader()
    
                        If reader.HasRows Then
                            Dim RecipeList As New List(Of Recipe)
                            While reader.Read()
                                RecipeList.Add(New Recipe() With {
                                                  .Id = reader.GetInt32(0),
                                                  .Name = reader.GetString(1),
                                                  .Con1 = reader.GetString(2),
                                                  .Con2 = reader.GetString(3),
                                                   .Con3 = reader.GetString(4),
                                                   .Con4 = reader.GetString(5),
                                                   .Con5 = reader.GetString(6),
                                                   .Con6 = reader.GetString(7),
                                                   .Con7 = reader.GetString(8),
                                                   .Con8 = reader.GetString(9)
                                                  })
                            End While
    
                            RecipeNamesListBox.DataSource = RecipeList
    
                        End If
                    End Using
                End Using
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    
        End Sub

    Tuesday, February 4, 2020 8:03 PM
  • What is this code sepose to do, I dont get it.

    Imports System.Data.OleDb
    Imports System.IO
    
    Public Class ToolsForm
    
        Dim MyParent As Form1
        'Dim connString As String
    
        Dim RecipeControlHeadingFont As New System.Drawing.Font("Arial Rounded MT Bold", 14)
        Dim RecipeControlFont As New System.Drawing.Font("Arial Rounded MT Bold", 11)
    
        Private Sub ToolsForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            MyParent = DirectCast(Me.Owner, Form1)
            'MyParent.ChildStatus = 1
    
            Me.Location = New Point(1050, 150)
            Me.Size = New Size(347, 500)
    
            Me.Input_Label.Text = "Wat doen ons vandag?"
            '& vbCrLf & "" & vbCrLf & "Plaas ons 'n Nuwe Resep?" & vbCrLf & "of" & vbCrLf & "Soek  ons na 'n Bestaande Resep?"
            Me.Input_Label.Font = RecipeControlHeadingFont
    
            Me.New_Recipe_Button.Location = New Point(Me.New_Recipe_Button.Left, Me.Input_Label.Bottom + 5)
            Me.Save_View_Button.Location = New Point(Me.Save_View_Button.Left, Me.Input_Label.Bottom + 5)
    
            Me.New_Recipe_Button.Text = "Plaas 'n Nuwe Resep"
            Me.New_Recipe_Button.Size = New Size(158, 25)
            Me.New_Recipe_Button.Font = RecipeControlFont
    
            Me.Save_View_Button.Text = "Soek  na 'n Resep"
            Me.Save_View_Button.Size = New Size(158, 25)
            Me.Save_View_Button.Font = RecipeControlFont
    
    
           
    
        End Sub
    
        'Private ConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Hoofgeregte.accdb"
        Private ConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=M:\My Documents\My Resepte\Hoofgeregte.accdb"
        'Private ActiveTabel As String
        'Private BindingSource As New BindingSource
    
        Private Sub CategoryComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CategoryComboBox.SelectedIndexChanged
    
        End Sub
    
        Private Sub TitleListBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TitleListBox.SelectedIndexChanged
            Try
    
                Dim tableName As String = TitleListBox.SelectedItem.ToString()
    
                Using cn As OleDbConnection = New OleDbConnection(ConnectionString)
    
                    cn.Open()
    
                    Dim cmdText As String =
                            "SELECT ID, RecipeName, Comment, Delivery, Con1, con2, " &
                            "con3, con4, con5, con6, " &
                            "con7, con8 FROM [" & tableName & "]"
    
                    Using cmd As OleDbCommand = New OleDbCommand(cmdText, cn)
    
                        Dim reader = cmd.ExecuteReader()
    
                        If reader.HasRows Then
                            Dim RecipeList As New List(Of Recipe)
                            While reader.Read()
                                RecipeList.Add(New Recipe() With {
                                                  .Id = reader.GetInt32(0),
                                                  .Name = reader.GetString(1),
                                                  .Con1 = reader.GetString(2),
                                                  .Con2 = reader.GetString(3),
                                                   .Con3 = reader.GetString(4),
                                                   .Con4 = reader.GetString(5),
                                                   .Con5 = reader.GetString(6),
                                                   .Con6 = reader.GetString(7),
                                                   .Con7 = reader.GetString(8),
                                                   .Con8 = reader.GetString(9)
                                                  })
                            End While
    
                            RecipeNamesListBox.DataSource = RecipeList
    
                        End If
                    End Using
                End Using
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    
        End Sub

    I was about to response with a description but after going through your code again it dawn on me that this is not going to work as you expected since a) classes confuse you b) the state of the data is not what I call conducive to simple read operations as a quick fix I needed to use a language extension I wrote to assist as the fields are causing issues when reading them back into the application.

    In short I could rewrite the entire project and anything less than that is just asking for trouble. 

    My guess is this is not going to be clear to you and I don't have time to walk you through this.

    If reader.HasRows Then
    Dim RecipeList As New List(Of Recipe)
    While reader.Read()
        RecipeList.Add(New Recipe() With {
                          .Id = reader.GetInt32(0),
                          .Name = reader.GetString(1),
                          .Caption = reader.GetStringSafe("Caption"),
                          .Delivery = reader.GetStringSafe("Delivery"),
                          .Con1 = reader.GetString("Con1")
                          })
    End While
    
    RecipeNamesListBox.DataSource = RecipeList
    

    Imports System.Runtime.CompilerServices
    Public Module DataExensions
        <Extension>
        Public Function GetStringSafe(pReader As IDataReader, pField As String) As String
            Return If(TypeOf pReader(pField) Is DBNull, Nothing, pReader(pField).ToString())
        End Function
    End Module
    


     

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, February 4, 2020 10:21 PM
    Moderator
  • Hi VB Novice Hendri,

    Thanks for your feedback.

    You can use Treeview to see the result of query.

    I create a student class and make a test. Here's the code you can refer to.

        Class Student
            Public Property ID As Integer
            Public Property Name As String
            Public Property Age As Integer
        End Class
        Private Sub TitleListBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TitleListBox.SelectedIndexChanged
            Try
                Dim tableName As String = TitleListBox.SelectedItem.ToString()
    
                Using cn As OleDbConnection = New OleDbConnection(ConnectionString)
                    cn.Open()
                    Dim cmdText As String = "SELECT ID, Name, Age FROM [" & tableName & "]"
    
                    Using cmd As OleDbCommand = New OleDbCommand(cmdText, cn)
                        Dim reader = cmd.ExecuteReader()
    
                        If reader.HasRows Then
                            Dim count As Integer = 0
    
                            While reader.Read()
                                TreeView1.Nodes.Add($"Student{count + 1}")
                                TreeView1.Nodes(count).Nodes.Add($"ID: {reader(0).ToString()}")
                                TreeView1.Nodes(count).Nodes.Add($"Name: {reader(1).ToString()}")
                                TreeView1.Nodes(count).Nodes.Add($"Age: {reader(2).ToString()}")
                                count += 1
                            End While
                        End If
                    End Using
                End Using
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub

    Data in my database:

    Result:

    Hope it could be helpful.

    Besides, if you still have any questions about this issue, please let me know.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 5, 2020 3:25 AM
    Moderator
  • This is part of what I have sofar.  This is part of ab Select Case statment under CategoryComboBox.SelectedIndexChanged, the list in the ComboBox is not DataBound. 

    Making a selection in it will place an list of items in the TitleListBox as seen below.  

     Case Is = "Hoofgeregte"
                    ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=M:\My Documents\My Resepte\Hoofgeregte.accdb"
                    TitleListBox.Items.Add("Eiergeregte")
                    TitleListBox.Items.Add("Heerlike Sop Resepte")
                    TitleListBox.Items.Add("Pasta, Bobotie, Breyani, Tamatie en Kerie")
                    TitleListBox.Items.Add("Viegitariese Eetes & Groente Geregte")
                    TitleListBox.Items.Add("Vleis Geregte")
                    TitleListBox.Items.Add("Pluimvee Geregte - Hoender, Eend ens.")
                    TitleListBox.Items.Add("Vis & ander Seekos Geregte")

    Making a selection in the TitleListBox, this Select Case in TitleListBox.SelectedIndexChanged shown below will Load the apropreate Tabel Name into a Var called ActiveTabel.  

    Now the TitleListBox must show a list of all recipes in the Tabel.  This is what I need from you, just a simple way of showing a list of recipes in a ListBox. 

    Case Is = "Eiergeregte"
                        ActiveTabel = "Eiergeregte"
                    Case Is = "Heerlike Sop Resepte"
                        ActiveTabel = "SopResepte"
                    Case Is = "Pasta, Bobotie, Breyani, Tamatie en Kerie"
                        ActiveTabel = "PastaBobotieBreyaniTamatieKerie"
                    Case Is = "Viegitariese Eetes & Groente Geregte"
                        ActiveTabel = "ViegitarieseEetesGroenteGeregte"
                    Case Is = "Vleis Geregte"
                        ActiveTabel = "VleisGeregte"
                    Case Is = "Pluimvee Geregte - Hoender, Eend ens."
                        ActiveTabel = "PluimveeGeregte"
                    Case Is = "Vis & ander Seekos Geregte"
                        ActiveTabel = "VisSeekosGeregte"

    Wednesday, February 5, 2020 11:51 AM
  • Hi VB Novice Hendri,

    I find the thread is mostly the same as the other thread, so hope the link can help other members to find the solution quickly if they face a similar issue. 

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 13, 2020 9:08 AM
    Moderator