VB.Net Tutorial/GUI/ListBox

Материал из VB Эксперт
Перейти к: навигация, поиск

Add, remove and clear list box items

<source lang="vbnet">Imports System.Windows.Forms public class AddClearListBoxDelete

  public Shared Sub Main
       Application.Run(New FrmListBox)
  End Sub

End class

Public Class FrmListBox

  Inherits Form
  1. Region " Windows Form Designer generated code "
  Public Sub New()
     MyBase.New()
     "This call is required by the Windows Form Designer.
     InitializeComponent()
     "Add any initialization after the InitializeComponent() call
  End Sub
  "Form overrides dispose to clean up the component list.
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
     If disposing Then
        If Not (components Is Nothing) Then
           components.Dispose()
        End If
     End If
     MyBase.Dispose(disposing)
  End Sub
  " contains user-input list of elements
  Friend WithEvents lstDisplay As ListBox
  " user input textbox
  Friend WithEvents txtInput As TextBox
  " add, remove, clear and exit command buttons
  Friend WithEvents cmdAdd As Button
  Friend WithEvents cmdRemove As Button
  Friend WithEvents cmdClear As Button
  Friend WithEvents cmdExit As Button
  "Required by the Windows Form Designer
  Private components As System.ruponentModel.Container
  "NOTE: The following procedure is required by the Windows Form Designer
  "It can be modified using the Windows Form Designer.  
  "Do not modify it using the code editor.
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
     Me.cmdClear = New System.Windows.Forms.Button()
     Me.cmdExit = New System.Windows.Forms.Button()
     Me.cmdAdd = New System.Windows.Forms.Button()
     Me.txtInput = New System.Windows.Forms.TextBox()
     Me.cmdRemove = New System.Windows.Forms.Button()
     Me.lstDisplay = New System.Windows.Forms.ListBox()
     Me.SuspendLayout()
     "
     "cmdClear
     "
     Me.cmdClear.Location = New System.Drawing.Point(160, 144)
     Me.cmdClear.Name = "cmdClear"
     Me.cmdClear.Size = New System.Drawing.Size(104, 40)
     Me.cmdClear.TabIndex = 4
     Me.cmdClear.Text = "Clear"
     "
     "cmdExit
     "
     Me.cmdExit.Location = New System.Drawing.Point(160, 192)
     Me.cmdExit.Name = "cmdExit"
     Me.cmdExit.Size = New System.Drawing.Size(104, 40)
     Me.cmdExit.TabIndex = 5
     Me.cmdExit.Text = "Exit"
     "
     "cmdAdd
     "
     Me.cmdAdd.Location = New System.Drawing.Point(160, 48)
     Me.cmdAdd.Name = "cmdAdd"
     Me.cmdAdd.Size = New System.Drawing.Size(104, 40)
     Me.cmdAdd.TabIndex = 2
     Me.cmdAdd.Text = "Add"
     "
     "txtInput
     "
     Me.txtInput.Location = New System.Drawing.Point(160, 8)
     Me.txtInput.Name = "txtInput"
     Me.txtInput.Size = New System.Drawing.Size(104, 20)
     Me.txtInput.TabIndex = 1
     Me.txtInput.Text = ""
     "
     "cmdRemove
     "
     Me.cmdRemove.Location = New System.Drawing.Point(160, 96)
     Me.cmdRemove.Name = "cmdRemove"
     Me.cmdRemove.Size = New System.Drawing.Size(104, 40)
     Me.cmdRemove.TabIndex = 3
     Me.cmdRemove.Text = "Remove"
     "
     "lstDisplay
     "
     Me.lstDisplay.Location = New System.Drawing.Point(16, 8)
     Me.lstDisplay.Name = "lstDisplay"
     Me.lstDisplay.Size = New System.Drawing.Size(120, 238)
     Me.lstDisplay.TabIndex = 0
     "
     "FrmListBox
     "
     Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
     Me.ClientSize = New System.Drawing.Size(292, 273)
     Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cmdExit, Me.cmdClear, Me.cmdRemove, Me.cmdAdd, Me.txtInput, Me.lstDisplay})
     Me.Name = "FrmListBox"
     Me.Text = "ListBoxTest"
     Me.ResumeLayout(False)
  End Sub
  1. End Region
  Private Sub cmdAdd_Click(ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles cmdAdd.Click
     lstDisplay.Items.Add(txtInput.Text)
     txtInput.Text = ""
  End Sub 
  Private Sub cmdRemove_Click(ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles cmdRemove.Click
     If lstDisplay.SelectedIndex <> -1 Then
        lstDisplay.Items.RemoveAt(lstDisplay.SelectedIndex)
     End If
  End Sub 
  Private Sub cmdClear_Click(ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles cmdClear.Click
     lstDisplay.Items.Clear()
  End Sub 
  Private Sub cmdExit_Click(ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles cmdExit.Click
     Application.Exit()
  End Sub

End Class</source>

Add value to ListBox

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class ListBoxSelectionEventAddValue

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class Public Class Form1

   Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
   Public Sub New()
       MyBase.New()
       "This call is required by the Windows Form Designer.
       InitializeComponent()
       "Add any initialization after the InitializeComponent() call
   End Sub
   "Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing Then
           If Not (components Is Nothing) Then
               components.Dispose()
           End If
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
   Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.ListBox1 = New System.Windows.Forms.ListBox
       Me.TextBox1 = New System.Windows.Forms.TextBox
       Me.SuspendLayout()
       "
       Me.ListBox1.Location = New System.Drawing.Point(88, 64)
       Me.ListBox1.Name = "ListBox1"
       Me.ListBox1.Size = New System.Drawing.Size(120, 121)
       Me.ListBox1.TabIndex = 0
       "
       Me.TextBox1.Location = New System.Drawing.Point(16, 216)
       Me.TextBox1.Name = "TextBox1"
       Me.TextBox1.Size = New System.Drawing.Size(264, 20)
       Me.TextBox1.TabIndex = 1
       Me.TextBox1.Text = ""
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(292, 273)
       Me.Controls.Add(Me.TextBox1)
       Me.Controls.Add(Me.ListBox1)
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       For i As Integer = 0 To 9
           ListBox1.Items.Add("Item " & i.ToString())
       Next i
   End Sub
   Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
       TextBox1.Text = "You selected item " & ListBox1.SelectedIndex
   End Sub

End Class</source>

Get mouse over index by mouse location

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class ListBoxMouseOverLocation

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class Public Class Form1

   Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
   Public Sub New()
       MyBase.New()
       "This call is required by the Windows Form Designer.
       InitializeComponent()
       "Add any initialization after the InitializeComponent() call
   End Sub
   "Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing Then
           If Not (components Is Nothing) Then
               components.Dispose()
           End If
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
   Friend WithEvents Button1 As System.Windows.Forms.Button
   Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
   Friend WithEvents Label1 As System.Windows.Forms.Label
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.ListBox1 = New System.Windows.Forms.ListBox()
       Me.Button1 = New System.Windows.Forms.Button()
       Me.TextBox1 = New System.Windows.Forms.TextBox()
       Me.Label1 = New System.Windows.Forms.Label()
       Me.SuspendLayout()
       "
       "ListBox1
       "
       Me.ListBox1.Items.AddRange(New Object() {"Item0", "Item1", "Item2"})
       Me.ListBox1.Location = New System.Drawing.Point(8, 88)
       Me.ListBox1.Name = "ListBox1"
       Me.ListBox1.Size = New System.Drawing.Size(336, 134)
       Me.ListBox1.TabIndex = 0
       "
       "Button1
       "
       Me.Button1.Location = New System.Drawing.Point(8, 232)
       Me.Button1.Name = "Button1"
       Me.Button1.TabIndex = 1
       Me.Button1.Text = "Populate"
       "
       "TextBox1
       "
       Me.TextBox1.Location = New System.Drawing.Point(88, 232)
       Me.TextBox1.Name = "TextBox1"
       Me.TextBox1.ReadOnly = True
       Me.TextBox1.Size = New System.Drawing.Size(256, 20)
       Me.TextBox1.TabIndex = 2
       Me.TextBox1.Text = ""
       "
       "Label1
       "
       Me.Label1.Location = New System.Drawing.Point(8, 264)
       Me.Label1.Name = "Label1"
       Me.Label1.Size = New System.Drawing.Size(336, 72)
       Me.Label1.TabIndex = 3
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(352, 341)
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label1, Me.TextBox1, Me.Button1, Me.ListBox1})
       Me.Name = "Form1"
       Me.Text = "Lab 5.3 HitTest"
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Sub ListBox1_MouseMove(ByVal sender As Object, _
   ByVal e As System.Windows.Forms.MouseEventArgs) _
   Handles ListBox1.MouseMove
       Dim G As System.Drawing.Graphics = Graphics.FromHwnd(Me.Handle)
       Dim item As String
       item = ListBox1.IndexFromPoint(New Point(e.X, e.Y))
       If item >= 0 Then "mouse is over an item
           TextBox1.Text = "Hittest Index:" & item & " is a " & ListBox1.Items(item).GetType.ToString()
       End If
   End Sub

End Class</source>

Get selected indices in a ListBox

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class ListBoxMultiSelectionIndex

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class Public Class Form1

   Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
   Public Sub New()
       MyBase.New()
       "This call is required by the Windows Form Designer.
       InitializeComponent()
       "Add any initialization after the InitializeComponent() call
   End Sub
   "Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing Then
           If Not (components Is Nothing) Then
               components.Dispose()
           End If
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
   Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.TextBox1 = New System.Windows.Forms.TextBox
       Me.TextBox2 = New System.Windows.Forms.TextBox
       Me.SuspendLayout()
       "
       "TextBox1
       "
       Me.TextBox1.Location = New System.Drawing.Point(8, 184)
       Me.TextBox1.Name = "TextBox1"
       Me.TextBox1.Size = New System.Drawing.Size(272, 20)
       Me.TextBox1.TabIndex = 0
       Me.TextBox1.Text = ""
       "
       "TextBox2
       "
       Me.TextBox2.Location = New System.Drawing.Point(8, 240)
       Me.TextBox2.Name = "TextBox2"
       Me.TextBox2.Size = New System.Drawing.Size(272, 20)
       Me.TextBox2.TabIndex = 1
       Me.TextBox2.Text = ""
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(292, 273)
       Me.Controls.Add(Me.TextBox2)
       Me.Controls.Add(Me.TextBox1)
       Me.ResumeLayout(False)
       ListBox1 = New ListBox
       ListBox1.Size = New Size(270, 100)
       ListBox1.Location = New Point(10, 60)
       AddHandler ListBox1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged
       Me.Controls.Add(ListBox1)
       ListBox1.MultiColumn = True
       ListBox1.SelectionMode = SelectionMode.MultiExtended
       For i As Integer = 0 To 19
           ListBox1.Items.Add("Item " & i.ToString())
       Next i
       ListBox1.SetSelected(2, True)
       ListBox1.SetSelected(4, True)
       ListBox1.SetSelected(8, True)
       ListBox1.SetSelected(10, True)
   End Sub
  1. End Region
   Dim ListBox1 As ListBox
   Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
       Dim Item As String
       Dim Index As Integer
       TextBox2.Text = "Here are the selected indices: "
       For Each Index In ListBox1.SelectedIndices
           TextBox2.Text &= Index.ToString() & " "
       Next
   End Sub

End Class</source>

Get selected item in a ListBox selection event

<source lang="vbnet">Imports System.Windows.Forms public class GetSelectedItemListBox

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class Public Class Form1

   Inherits System.Windows.Forms.Form
   Public Sub New()
       MyBase.New()
       InitializeComponent()
   End Sub
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing Then
           If Not (components Is Nothing) Then
               components.Dispose()
           End If
       End If
       MyBase.Dispose(disposing)
   End Sub
   Private components As System.ruponentModel.IContainer
   Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
   Friend WithEvents Label1 As System.Windows.Forms.Label
   Friend WithEvents Label2 As System.Windows.Forms.Label
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.ListBox1 = New System.Windows.Forms.ListBox
       Me.Label1 = New System.Windows.Forms.Label
       Me.Label2 = New System.Windows.Forms.Label
       Me.SuspendLayout()
       "
       "ListBox1
       "
       Me.ListBox1.Items.AddRange(New Object() {"A", "B", "C", "D", "E", "F", "G"})
       Me.ListBox1.Location = New System.Drawing.Point(72, 48)
       Me.ListBox1.Name = "ListBox1"
       Me.ListBox1.Size = New System.Drawing.Size(88, 108)
       Me.ListBox1.TabIndex = 0
       "
       "Label1
       "
       Me.Label1.Location = New System.Drawing.Point(8, 16)
       Me.Label1.Name = "Label1"
       Me.Label1.Size = New System.Drawing.Size(136, 24)
       Me.Label1.TabIndex = 1
       Me.Label1.Text = "Translate:"
       "
       "Label2
       "
       Me.Label2.Location = New System.Drawing.Point(56, 176)
       Me.Label2.Name = "Label2"
       Me.Label2.Size = New System.Drawing.Size(128, 40)
       Me.Label2.TabIndex = 2
       Me.Label2.Text = "Chn:"
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(224, 246)
       Me.Controls.Add(Me.Label2)
       Me.Controls.Add(Me.Label1)
       Me.Controls.Add(Me.ListBox1)
       Me.Name = "Form1"
       Me.Text = "Form1"
       Me.ResumeLayout(False)
   End Sub
   Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
   End Sub
   Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
       Select Case ListBox1.SelectedIndex
           Case 0
               Label2.Text = "AA"
           Case 1
               Label2.Text = "BB"
           Case 2
               Label2.Text = "CC"
           Case 3
               Label2.Text = "DD"
           Case 4
               Label2.Text = "EE"
           Case 5
               Label2.Text = "FF"
           Case 6
               Label2.Text = "GG"
       End Select
   End Sub

End Class</source>

Get selected Items in a ListBox

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class ListBoxMultiSelection

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class Public Class Form1

   Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
   Public Sub New()
       MyBase.New()
       "This call is required by the Windows Form Designer.
       InitializeComponent()
       "Add any initialization after the InitializeComponent() call
   End Sub
   "Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing Then
           If Not (components Is Nothing) Then
               components.Dispose()
           End If
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
   Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.TextBox1 = New System.Windows.Forms.TextBox
       Me.TextBox2 = New System.Windows.Forms.TextBox
       Me.SuspendLayout()
       "
       "TextBox1
       "
       Me.TextBox1.Location = New System.Drawing.Point(8, 184)
       Me.TextBox1.Name = "TextBox1"
       Me.TextBox1.Size = New System.Drawing.Size(272, 20)
       Me.TextBox1.TabIndex = 0
       Me.TextBox1.Text = ""
       "
       "TextBox2
       "
       Me.TextBox2.Location = New System.Drawing.Point(8, 240)
       Me.TextBox2.Name = "TextBox2"
       Me.TextBox2.Size = New System.Drawing.Size(272, 20)
       Me.TextBox2.TabIndex = 1
       Me.TextBox2.Text = ""
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(292, 273)
       Me.Controls.Add(Me.TextBox2)
       Me.Controls.Add(Me.TextBox1)
       Me.ResumeLayout(False)
       ListBox1 = New ListBox
       ListBox1.Size = New Size(270, 100)
       ListBox1.Location = New Point(10, 60)
       AddHandler ListBox1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged
       Me.Controls.Add(ListBox1)
       ListBox1.MultiColumn = True
       ListBox1.SelectionMode = SelectionMode.MultiExtended
       For i As Integer = 0 To 19
           ListBox1.Items.Add("Item " & i.ToString())
       Next i
       ListBox1.SetSelected(2, True)
       ListBox1.SetSelected(4, True)
       ListBox1.SetSelected(8, True)
       ListBox1.SetSelected(10, True)
   End Sub
  1. End Region
   Dim ListBox1 As ListBox
   Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
       Dim Item As String
       Dim Index As Integer
       TextBox1.Text = "Here are the selected items: "
       For Each Item In ListBox1.SelectedItems
           TextBox1.Text &= Item.ToString() & " "
       Next
   End Sub

End Class</source>

ListBox double click event

<source lang="vbnet">Imports System.Windows.Forms public class ListBoxDoubleClick

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class Public Class Form1

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   End Sub
   Private Sub ListBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
       Select Case ListBox1.SelectedIndex
           Case 0
               TextBox1.Text = "index 0"
           Case 1
               TextBox1.Text = "index 1"
           Case 2
               TextBox1.Text = "index 2"
       End Select
   End Sub

End Class

<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Class Form1

   Inherits System.Windows.Forms.Form
   "Form overrides dispose to clean up the component list.
   <System.Diagnostics.DebuggerNonUserCode()> _
   Protected Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing AndAlso components IsNot Nothing Then
           components.Dispose()
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   <System.Diagnostics.DebuggerStepThrough()> _
   Private Sub InitializeComponent()
       Me.ListBox1 = New System.Windows.Forms.ListBox
       Me.TextBox1 = New System.Windows.Forms.TextBox
       Me.SuspendLayout()
       "
       "ListBox1
       "
       Me.ListBox1.FormattingEnabled = True
       Me.ListBox1.ItemHeight = 12
       Me.ListBox1.Items.AddRange(New Object() {"Document1", "Document2", "Document3"})
       Me.ListBox1.Location = New System.Drawing.Point(277, 12)
       Me.ListBox1.Name = "ListBox1"
       Me.ListBox1.Size = New System.Drawing.Size(112, 184)
       Me.ListBox1.TabIndex = 0
       "
       "TextBox1
       "
       Me.TextBox1.Location = New System.Drawing.Point(12, 12)
       Me.TextBox1.Multiline = True
       Me.TextBox1.Name = "TextBox1"
       Me.TextBox1.Size = New System.Drawing.Size(248, 178)
       Me.TextBox1.TabIndex = 1
       "
       "Form1
       "
       Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
       Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
       Me.ClientSize = New System.Drawing.Size(401, 202)
       Me.Controls.Add(Me.TextBox1)
       Me.Controls.Add(Me.ListBox1)
       Me.Name = "Form1"
       Me.Text = "Form1"
       Me.ResumeLayout(False)
       Me.PerformLayout()
   End Sub
   Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
   Friend WithEvents TextBox1 As System.Windows.Forms.TextBox

End Class</source>

ListBox selection event

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class ListBoxSelectionEventAddValue

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class Public Class Form1

   Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
   Public Sub New()
       MyBase.New()
       "This call is required by the Windows Form Designer.
       InitializeComponent()
       "Add any initialization after the InitializeComponent() call
   End Sub
   "Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing Then
           If Not (components Is Nothing) Then
               components.Dispose()
           End If
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
   Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.ListBox1 = New System.Windows.Forms.ListBox
       Me.TextBox1 = New System.Windows.Forms.TextBox
       Me.SuspendLayout()
       "
       Me.ListBox1.Location = New System.Drawing.Point(88, 64)
       Me.ListBox1.Name = "ListBox1"
       Me.ListBox1.Size = New System.Drawing.Size(120, 121)
       Me.ListBox1.TabIndex = 0
       "
       Me.TextBox1.Location = New System.Drawing.Point(16, 216)
       Me.TextBox1.Name = "TextBox1"
       Me.TextBox1.Size = New System.Drawing.Size(264, 20)
       Me.TextBox1.TabIndex = 1
       Me.TextBox1.Text = ""
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(292, 273)
       Me.Controls.Add(Me.TextBox1)
       Me.Controls.Add(Me.ListBox1)
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       For i As Integer = 0 To 9
           ListBox1.Items.Add("Item " & i.ToString())
       Next i
   End Sub
   Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
       TextBox1.Text = "You selected item " & ListBox1.SelectedIndex
   End Sub

End Class</source>

MultiColumn ListBox

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class ListBoxMultiSelection

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class Public Class Form1

   Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
   Public Sub New()
       MyBase.New()
       "This call is required by the Windows Form Designer.
       InitializeComponent()
       "Add any initialization after the InitializeComponent() call
   End Sub
   "Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing Then
           If Not (components Is Nothing) Then
               components.Dispose()
           End If
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
   Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.TextBox1 = New System.Windows.Forms.TextBox
       Me.TextBox2 = New System.Windows.Forms.TextBox
       Me.SuspendLayout()
       "
       "TextBox1
       "
       Me.TextBox1.Location = New System.Drawing.Point(8, 184)
       Me.TextBox1.Name = "TextBox1"
       Me.TextBox1.Size = New System.Drawing.Size(272, 20)
       Me.TextBox1.TabIndex = 0
       Me.TextBox1.Text = ""
       "
       "TextBox2
       "
       Me.TextBox2.Location = New System.Drawing.Point(8, 240)
       Me.TextBox2.Name = "TextBox2"
       Me.TextBox2.Size = New System.Drawing.Size(272, 20)
       Me.TextBox2.TabIndex = 1
       Me.TextBox2.Text = ""
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(292, 273)
       Me.Controls.Add(Me.TextBox2)
       Me.Controls.Add(Me.TextBox1)
       Me.ResumeLayout(False)
       ListBox1 = New ListBox
       ListBox1.Size = New Size(270, 100)
       ListBox1.Location = New Point(10, 60)
       AddHandler ListBox1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged
       Me.Controls.Add(ListBox1)
       ListBox1.MultiColumn = True
       ListBox1.SelectionMode = SelectionMode.MultiExtended
       For i As Integer = 0 To 19
           ListBox1.Items.Add("Item " & i.ToString())
       Next i
       ListBox1.SetSelected(2, True)
       ListBox1.SetSelected(4, True)
       ListBox1.SetSelected(8, True)
       ListBox1.SetSelected(10, True)
   End Sub
  1. End Region
   Dim ListBox1 As ListBox
   Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
       Dim Item As String
       Dim Index As Integer
       TextBox1.Text = "Here are the selected items: "
       For Each Item In ListBox1.SelectedItems
           TextBox1.Text &= Item.ToString() & " "
       Next
   End Sub

End Class</source>

Multiple Selections

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class ListBoxMultiSelection

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class Public Class Form1

   Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
   Public Sub New()
       MyBase.New()
       "This call is required by the Windows Form Designer.
       InitializeComponent()
       "Add any initialization after the InitializeComponent() call
   End Sub
   "Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing Then
           If Not (components Is Nothing) Then
               components.Dispose()
           End If
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
   Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.TextBox1 = New System.Windows.Forms.TextBox
       Me.TextBox2 = New System.Windows.Forms.TextBox
       Me.SuspendLayout()
       "
       "TextBox1
       "
       Me.TextBox1.Location = New System.Drawing.Point(8, 184)
       Me.TextBox1.Name = "TextBox1"
       Me.TextBox1.Size = New System.Drawing.Size(272, 20)
       Me.TextBox1.TabIndex = 0
       Me.TextBox1.Text = ""
       "
       "TextBox2
       "
       Me.TextBox2.Location = New System.Drawing.Point(8, 240)
       Me.TextBox2.Name = "TextBox2"
       Me.TextBox2.Size = New System.Drawing.Size(272, 20)
       Me.TextBox2.TabIndex = 1
       Me.TextBox2.Text = ""
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(292, 273)
       Me.Controls.Add(Me.TextBox2)
       Me.Controls.Add(Me.TextBox1)
       Me.ResumeLayout(False)
       ListBox1 = New ListBox
       ListBox1.Size = New Size(270, 100)
       ListBox1.Location = New Point(10, 60)
       AddHandler ListBox1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged
       Me.Controls.Add(ListBox1)
       ListBox1.MultiColumn = True
       ListBox1.SelectionMode = SelectionMode.MultiExtended
       For i As Integer = 0 To 19
           ListBox1.Items.Add("Item " & i.ToString())
       Next i
       ListBox1.SetSelected(2, True)
       ListBox1.SetSelected(4, True)
       ListBox1.SetSelected(8, True)
       ListBox1.SetSelected(10, True)
   End Sub
  1. End Region
   Dim ListBox1 As ListBox
   Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
       Dim Item As String
       Dim Index As Integer
       TextBox1.Text = "Here are the selected items: "
       For Each Item In ListBox1.SelectedItems
           TextBox1.Text &= Item.ToString() & " "
       Next
   End Sub

End Class</source>

Owner draw ListBox

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class OwnerDrawnVariableListBoxDemo

  public Shared Sub Main
       Application.Run(New OwnerDrawnVariableListBox)
  End Sub

End class

Public Class OwnerDrawnVariableListBox

   Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
   Public Sub New()
       MyBase.New()
       "This call is required by the Windows Form Designer.
       InitializeComponent()
       "Add any initialization after the InitializeComponent() call
   End Sub
   "Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing Then
           If Not (components Is Nothing) Then
               components.Dispose()
           End If
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   Friend WithEvents listBox2 As System.Windows.Forms.ListBox
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.listBox2 = New System.Windows.Forms.ListBox()
       Me.SuspendLayout()
       "
       "listBox2
       "
       Me.listBox2.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable
       Me.listBox2.Items.AddRange(New Object() {"AAAAAAAAAAAAAAA", "BBBBBBBBBBBBBBBB", "CCCCCC"})
       Me.listBox2.Location = New System.Drawing.Point(6, 37)
       Me.listBox2.Name = "listBox2"
       Me.listBox2.Size = New System.Drawing.Size(280, 192)
       Me.listBox2.TabIndex = 5
       "
       "OwnerDrawnVariableListBox
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(292, 266)
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.listBox2})
       Me.Name = "OwnerDrawnVariableListBox"
       Me.Text = "OwnerDrawnVariableListBox"
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Sub listBox2_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles listBox2.DrawItem
       e.DrawBackground()
       Dim textFont As Font
       If e.Index Mod 2 = 0 Then
           textFont = New Font(e.Font.FontFamily, e.Font.Size * 2)
       Else
           textFont = e.Font
       End If
       e.Graphics.DrawString(listBox2.Items(e.Index).ToString(), textFont, New SolidBrush(e.ForeColor), RectangleF.op_Implicit(e.Bounds))
       e.DrawFocusRectangle()
   End Sub
   Private Sub listBox2_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles listBox2.MeasureItem
       If e.Index Mod 2 = 0 Then
           e.ItemHeight = e.ItemHeight * 2
       End If
   End Sub

End Class</source>

Owner draw ListBox fixed

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class OwnerDrawnVariableListBoxDemo

  public Shared Sub Main
       Application.Run(New OwnerDrawnFixedListBox)
  End Sub

End class Public Class OwnerDrawnFixedListBox

   Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
   Public Sub New()
       MyBase.New()
       "This call is required by the Windows Form Designer.
       InitializeComponent()
       "Add any initialization after the InitializeComponent() call
   End Sub
   "Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing Then
           If Not (components Is Nothing) Then
               components.Dispose()
           End If
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   Friend WithEvents listBox1 As System.Windows.Forms.ListBox
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.listBox1 = New System.Windows.Forms.ListBox()
       Me.SuspendLayout()
       "
       "listBox1
       "
       Me.listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed
       Me.listBox1.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
       Me.listBox1.ItemHeight = 18
       Me.listBox1.Items.AddRange(New Object() {"Foo", "Bar", "Quux"})
       Me.listBox1.Location = New System.Drawing.Point(22, 50)
       Me.listBox1.Name = "listBox1"
       Me.listBox1.Size = New System.Drawing.Size(248, 166)
       Me.listBox1.TabIndex = 3
       "
       "OwnerDrawnFixedListBox
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(292, 266)
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.listBox1})
       Me.Name = "OwnerDrawnFixedListBox"
       Me.Text = "OwnerDrawnFixedListBox"
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Sub listBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles listBox1.DrawItem
       e.DrawBackground()
       Dim drawFont As Font = Nothing
       If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
           drawFont = New Font("Times New Roman", 14, FontStyle.Bold Or FontStyle.Italic)
       Else
           drawFont = e.Font
       End If
       e.Graphics.DrawString(listBox1.Items(e.Index).ToString(), drawFont, New SolidBrush(e.ForeColor), RectangleF.op_Implicit(e.Bounds))
       e.DrawFocusRectangle()
   End Sub

End Class</source>

Set selected item in a ListBox

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class ListBoxMultiSelection

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class Public Class Form1

   Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
   Public Sub New()
       MyBase.New()
       "This call is required by the Windows Form Designer.
       InitializeComponent()
       "Add any initialization after the InitializeComponent() call
   End Sub
   "Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing Then
           If Not (components Is Nothing) Then
               components.Dispose()
           End If
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
   Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.TextBox1 = New System.Windows.Forms.TextBox
       Me.TextBox2 = New System.Windows.Forms.TextBox
       Me.SuspendLayout()
       "
       "TextBox1
       "
       Me.TextBox1.Location = New System.Drawing.Point(8, 184)
       Me.TextBox1.Name = "TextBox1"
       Me.TextBox1.Size = New System.Drawing.Size(272, 20)
       Me.TextBox1.TabIndex = 0
       Me.TextBox1.Text = ""
       "
       "TextBox2
       "
       Me.TextBox2.Location = New System.Drawing.Point(8, 240)
       Me.TextBox2.Name = "TextBox2"
       Me.TextBox2.Size = New System.Drawing.Size(272, 20)
       Me.TextBox2.TabIndex = 1
       Me.TextBox2.Text = ""
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(292, 273)
       Me.Controls.Add(Me.TextBox2)
       Me.Controls.Add(Me.TextBox1)
       Me.ResumeLayout(False)
       ListBox1 = New ListBox
       ListBox1.Size = New Size(270, 100)
       ListBox1.Location = New Point(10, 60)
       AddHandler ListBox1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged
       Me.Controls.Add(ListBox1)
       ListBox1.MultiColumn = True
       ListBox1.SelectionMode = SelectionMode.MultiExtended
       For i As Integer = 0 To 19
           ListBox1.Items.Add("Item " & i.ToString())
       Next i
       ListBox1.SetSelected(2, True)
       ListBox1.SetSelected(4, True)
       ListBox1.SetSelected(8, True)
       ListBox1.SetSelected(10, True)
   End Sub
  1. End Region
   Dim ListBox1 As ListBox
   Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
       Dim Item As String
       Dim Index As Integer
       TextBox1.Text = "Here are the selected items: "
       For Each Item In ListBox1.SelectedItems
           TextBox1.Text &= Item.ToString() & " "
       Next
   End Sub

End Class</source>

Use popup menu to add and delete ListBox item

<source lang="vbnet">Imports System.Windows.Forms public class MenuItemAddRemoveItemListBox

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class Public Class Form1

   Inherits System.Windows.Forms.Form
   Public Sub New()
       MyBase.New()
       InitializeComponent()
   End Sub
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing Then
           If Not (components Is Nothing) Then
               components.Dispose()
           End If
       End If
       MyBase.Dispose(disposing)
   End Sub
   Private components As System.ruponentModel.IContainer
   Friend WithEvents ContextMenu1 As System.Windows.Forms.ContextMenu
   Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
   Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
   Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.ContextMenu1 = New System.Windows.Forms.ContextMenu
       Me.MenuItem1 = New System.Windows.Forms.MenuItem
       Me.MenuItem2 = New System.Windows.Forms.MenuItem
       Me.ListBox1 = New System.Windows.Forms.ListBox
       Me.SuspendLayout()
       "
       "ContextMenu1
       "
       Me.ContextMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem2})
       "
       "MenuItem1
       "
       Me.MenuItem1.Index = 0
       Me.MenuItem1.Text = "Add"
       "
       "MenuItem2
       "
       Me.MenuItem2.Index = 1
       Me.MenuItem2.Text = "Delete"
       "
       "ListBox1
       "
       Me.ListBox1.Items.AddRange(New Object() {"A", "B", "C", "D", "E"})
       Me.ListBox1.Location = New System.Drawing.Point(80, 64)
       Me.ListBox1.Name = "ListBox1"
       Me.ListBox1.Size = New System.Drawing.Size(104, 121)
       Me.ListBox1.TabIndex = 0
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(292, 266)
       Me.ContextMenu = Me.ContextMenu1
       Me.Controls.Add(Me.ListBox1)
       Me.ResumeLayout(False)
   End Sub
   Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
       ListBox1.Items.Add("www.vbex.ru")
   End Sub
   Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
       ListBox1.Items.Remove(ListBox1.SelectedItem)
   End Sub

End Class</source>