Example : How to Add Static Data in the Combo Box.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
   ComboBox1.Text = "Select One"
   ComboBox1.Items.Add("Codershelpline")
   ComboBox1.Items.Add("VB.Net 2012")
   ComboBox1.Items.Add("VB.Net 2013")
   ComboBox1.Items.Add("VB.Net 2015")   

   Form1.ComboBox1.Items.Add("VB.Net 2017")   
End Sub

Example : How to selected combo box item set at top/index 0.

ComboBox1.SelectedItem = "Male"

Example : How to remove combo box item.

ComboBox1.Items.Remove("Female")
ComboBox1.Items.RemoveAt(1)

Example : How to display selected value of a combo box by a message.

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 
   ComboBox1.SelectedIndexChanged
        Dim var1 As String
        var1 = ComboBox1.Text
        MessageBox.Show(var1)
End Sub

Example : How to Add Single Field Data from Database into Combo Box.

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ComboBox1.Items.Clear()
        Try
            cn.Open()
            cm.CommandText = "SELECT distinct prdicode FROM newproduct "
            rdr = cm.ExecuteReader()
            While (rdr.Read())
                ComboBox1.Items.Add(rdr("prdicode").ToString)
            End While
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        Finally
            cn.Close()
        End Try
End Sub
'--------------------  OR  ---------------------
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        cn.Open()
        cm.CommandText = "SELECT distinct centercode FROM NewCenter Where centername = 
           '" + comCNAME.Text + "' and category='" + COMCATE.Text + "'"
        rdr = cm.ExecuteReader()
        While (rdr.Read())
            ComboBox1.Items.Add(rdr("centercode").ToString)
        End While
End Sub
'--------------------  OR  ---------------------
 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        If cn.State = ConnectionState.Open Then
            cn.Close()
        End If

        If cn.State = ConnectionState.Closed Then
            cn.Open()
            cm.CommandText = "select distinct serialno from booklist order by serialno"
            rdr = cm.ExecuteReader
            While (rdr.Read())
                ComboBox1.Items.Add(rdr("serialno").ToString)
            End While
        End If
 End Sub
'--------------------  OR  ---------------------
 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            cm.CommandText = "SELECT distinct FORMAT(bjobno,""000"")AS XYZ FROM prepressjob 
               where bjobmonth='" + cmbbjobmonth.Text + "'"
            rdr = cm.ExecuteReader()
            While (rdr.Read())
                ComboBox1.Items.Add(rdr("XYZ").ToString)
            End While
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        Finally
            cn.Close()
        End Try
  End Sub
'--------------------  OR  ---------------------
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim u As String = "Party / Customer"
        cn.Open()
        cm.CommandText = "select distinct name from ledgcre where under1='" + u + "'"
        rdr = cm.ExecuteReader
        While (rdr.Read())
            ComboBox1.Items.Add(rdr("name").ToString)
        End While
        cn.Close()
End Sub

NB: At first, connect the vb with sql server.

Example : How to Add Multiple Fields Data from Database into Single Combo Box.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        ComboBox1.Items.Clear()

        cn.Open()
        cm.CommandText = "select (adrress +' '+ pincode) as name1 from ledgcre"
        'OR cm.CommandText = "SELECT DISTINCT DJOBMONTH &'/'& FORMAT(DJOBNO,""000"")" AS NEWCOL FROM 
              JOBENTRY "

        rdr = cm.ExecuteReader()
        While (rdr.Read())
            ComboBox1.Items.Add(rdr("name1").ToString)
            ComboBox1.Items.Add(rdr("NEWCOL").ToString)
        End While
        cn.Close()
End Sub

Example : How to open combo box list automatically on getting focus.

Private Sub ComboBox1_GotFocus(sender As Object, e As EventArgs) Handles ComboBox1.GotFocus
        ComboBox1.DroppedDown = True
End Sub

Example : How to make a combo box auto suggest & append when we type some letters.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load        
        With ComboBox1
            .DropDownStyle = ComboBoxStyle.DropDown           
            .AutoCompleteMode = AutoCompleteMode.SuggestAppend
            .AutoCompleteSource = AutoCompleteSource.ListItems
        End With
End Sub

Example : How to create various type of combo box dynamically.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
       
        With ComboBox1
            .DropDownStyle = ComboBoxStyle.Simple               'Combo Box as normal Text Box
            .DropDownStyle = ComboBoxStyle.DropDown             'Code for Combo Box 
            .DropDownStyle = ComboBoxStyle.DropDownList         'Code for DropDownList         
        End With

        '  OR

        ComboBox1.DropDownStyle = ComboBoxStyle.Simple
        ComboBox1.DropDownStyle = ComboBoxStyle.DropDown
        ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList        
End Sub

 385 total views,  1 views today

Categories: VB .Net

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.