List Box Codes

       

Example : How to add static/constant values in List box using property window in vb .net .
- Draw a 'list box' controls from 'toolbox' to 'form' - Select the 'list box' on the form - 
  click on the 'triangle symbol' on the top-right corner of the list box - click 'edit items' 
  - enter the values one by one pressing enter key - ok - Now run the form to see the result.


 
- Write source code at load event as
   
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load      
        Dim x As String
        Dim y As Integer

        x = InputBox("Enter your name")
        y = InputBox("Enter your mobile number")

        ListBox1.Items.Add(x)
        ListBox1.Items.Add(y)
        ListBox1.Items.Add("Apple")
        ListBox1.Items.Add("200")
   End Sub   
Example : How to add static/constant values in List box with the help of source code in vb .net .
(Write source code at load event as)

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load        
        ListBox1.Items.Add("Apple")
        ListBox1.Items.Add("200")
End Sub
Example : How to add dynamic/run time values in List box with the help of source code in vb .net .
(Write source code at Button click event as)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim x As String
        Dim y As Integer

        x = InputBox("Enter your name")
        y = InputBox("Enter your mobile number")

        ListBox1.Items.Add(x)
        ListBox1.Items.Add(y)
End Sub
Example : How to remove/delete single list data from a list box dynamically with the help of source code in vb .net .
(Write source code at Button click event as)

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim x1 As String        
        x1 = InputBox("Enter similar data as in the list box to remove it")        

        ListBox1.Items.Remove(x1)        
End Sub
Example : How to clear/blank/cancel all values from a List box simultaneously with the help of source code in vb .net .
(Write source code at Button click event as)

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        ListBox1.Items.Clear()        
End Sub
Example : How to put data/values into respective VB controls such as text/combo/check                     box etc. from database into List Box Cell.
(Write/Put Connectivity Code here first.)

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
   Try
     cn.Open()
         cm.CommandText = "SELECT name FROM Usereg"
         rdr = cm.ExecuteReader()
         While (rdr.Read())
              ListBox1.Items.Add(rdr("name").ToString)             
         End While
    Catch ex As Exception
         MsgBox(ex.Message.ToString)
    Finally
         cn.Close()
    End Try
End Sub

List View Codes

       

Example : How to Add  New Item at specific Index such as index no. 3 in the List View.
Private Sub TextBox1_Click(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown

     ListView1.Items.Add("Codershelpline", 3)

End Sub
Example : How to Remove the First row Item from the List View.
Private Sub TextBox1_Click(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown

   ListView1.Items.RemoveAt(0)

End Sub
Example : How to Remove/Clear/Delete All the items from the List View.
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown

    ListView1.Items.Clear()
End Sub
Example : How to display the contents of a List View with column name i.e. in details view.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    ListView1.View = View.Details
End Sub
Example : How to append/Add a Column with Width 20 and Left Alignment in last of the List                       View.
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown

     ListView1.Columns.Add("Column Name", 20, HorizontalAlignment.Left)
End Sub
Example : How to Add List View Items into corresponding/related vb controls/boxes.
Private Sub ListView1_DoubleClick(sender As Object, e As EventArgs) Handles ListView1.DoubleClick
        Dim SelectedItemInList As New ListViewItem
        SelectedItemInList = ListView1.SelectedItems(0)

        CmbProCode.Text = SelectedItemInList.SubItems(1).Text
        TxtProName.Text = SelectedItemInList.SubItems(2).Text
        TxtQntty.Text = SelectedItemInList.SubItems(3).Text
        TxtUnit.Text = SelectedItemInList.SubItems(4).Text
        TxtRate.Text = SelectedItemInList.SubItems(5).Text
        TxtAmount.Text = SelectedItemInList.SubItems(6).Text
        CmbReturn.Text = SelectedItemInList.SubItems(7).Text
 End Sub
Example : How to add/transfer values into respective VB controls such as text/combo/check                     box etc. on Double Click on List View Cell/Row.
Private Sub ListView1_DoubleClick(sender As Object, e As EventArgs) Handles ListView1.DoubleClick        
   
   Dim x As New ListViewItem
   x = ListView1.SelectedItems(0)

   CmbProCode.Text = x .SubItems(1).Text
   TxtProName.Text = x .SubItems(2).Text
   TxtQntty.Text = x .SubItems(3).Text
   TxtUnit.Text = x .SubItems(4).Text
   TxtRate.Text = x .SubItems(5).Text
   TxtAmount.Text = x .SubItems(6).Text
   CmbReturn.Text = x .SubItems(7).Text

End Sub
Example : How to Check Particular/Individual Value Present in a List View Column/Cell [such as Column no. 4(or Sub items/Index 3)] or Not.
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown

  For i As Integer = 0 To listView1.Items.Count - 1
    If CmbDelCode.Text = listView1.Items(i).SubItems(3).Text Then
         MsgBox("Already Entered, Try To Choose New")
         CmbDelCode.Focus()
         SendKeys.Send("{F4}")
         Exit For
     End If
   Next
End Sub
Example : How to put data into List View cells/rows from database.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

   ListView1.Items.Clear()
   Try
      Dim itemid As Integer = ListView1.Items.Count
      cn.Open()
      cm.CommandText = "select * from Usereg WHERE CATEGORY ='" + COMCATE.Text + "'"
      rdr = cm.ExecuteReader()
      While(rdr.Read())
           ListView1.Items.Add(rdr(0).ToString)
           ListView1.Items(itemid).SubItems.Add(rdr(1).ToString)
           ListView1.Items(itemid).SubItems.Add(rdr(2).ToString)
           itemid += 1
      End While          
   Catch ex As Exception
      MsgBox(ex.Message.ToString)
   Finally
      cn.Close()
   End Try

End Sub

 338 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.