Tag Archives: ComboBox

How to get the list of installed printers in vb.net

To get the list of installed printers on your computer use the following code which will populate a Combo box with the list of installed printers.

 

Private Sub PopulateInstalledPrintersCombo()
        ' Add list of installed printers found to the combo box. 
        ' The pkInstalledPrinters string will be used to provide the display string. 
        Dim i As Integer
        Dim pkInstalledPrinters As String

        For i = 0 To Printing.PrinterSettings.InstalledPrinters.Count - 1
            pkInstalledPrinters = Printing.PrinterSettings.InstalledPrinters.Item(i)
            comboInstalledPrinters.Items.Add(pkInstalledPrinters)

        Next
    End Sub

Learn how to printer address labels on Dymo Label Printer in VB.NET

How to fill a ComboBox using a DataTable in VB.NET

A Combo box has two properties

 

1.One which displays the items in the combo box called the DISPLAY MEMBER

2. Another one which stores the underlying ID for the Item which is being Displayed.

 

eg your table may have EmployeeId and EmployeeName.

You would like to display the EmployeeName in the ComboBox and when a user selects an employee name you would like to save the EmployeeID.

Function Fillcombo(ByVal SQL As String, ByVal CBO As ComboBox, ByVal DisplayMember As String, ByVal ValueMember As String)
Dim dt As New DataTable                  'Create a New DataTable
Dim dc As New SqlCommand              'Create a New SQLCommand
Dim da As New SqlDataAdapter         ' Create a New SQL Data Adapter
dc.CommandTimeout = 0
dc.CommandText = sql
da.SelectCommand = dc
dc.Connection = cn

da.Fill(dt)    ‘ Use the DataAdapter to fill the DataTable with the records
Try
CBO.DataSource = dt
CBO.DisplayMember = DisplayMember   ‘ This is the item which will be displayed on the combo box  eg Employee Name
CBO.ValueMember = ValueMember       ‘ This is the value which is stored eg. employeeID

Catch
MsgBox(“Unable to Fill the Combobox:” & CBO.Name & ” with values” & “:” & Err.Description)
End Try
End Function

 

 How to use the above function

Fillcombo(“Select EmployeeID,EmployeeName from Employees”, Me.EmployeeCombo, “EmployeeName”, “EmployeeID”)

How to restrict free Data Entry in ComboBox in Visual Studio

When you create a Combo Box in Visual Studio  , the default behaviour is that it will show the list and it will also allow the data entry. So a user can type something in instead of selecting the options listed in the Combo Box. This can cause issues as you might want the user to only select the valid options which are listed in your combo box.

 

Here is a simple way to achieve this behavior. Highlight the Combo Box on your form. The properties window will be show.

Change the Drop Down Style to  DropDownList as shown below.

Your combo box will not allow the users to type in any free text. they will have to select the options listed in your combo box.

 

comboBox1