VB.Net by API/System.Windows.Forms/ListBox
Содержание
- 1 ListBox.DataSource
- 2 ListBox.DisplayMember
- 3 ListBox.DoDragDrop
- 4 ListBox.DoubleClick
- 5 ListBox.DragDrop
- 6 ListBox.DragEnter
- 7 ListBox.DragLeave
- 8 ListBox.DragOver
- 9 ListBox.DrawMode
- 10 ListBox.IndexFromPoint
- 11 ListBox.ItemHeight
- 12 ListBox.Items.Add
- 13 ListBox.Items.Clear()
- 14 ListBox.Items.Remove
- 15 ListBox.Items.RemoveAt
- 16 ListBox.MeasureItem
- 17 ListBox.MouseDown
- 18 ListBox.PointToClient
- 19 ListBox.SelectedIndexChanged
- 20 ListBox.SelectedIndices
- 21 ListBox.SelectionMode
- 22 ListBox.SetSelected
- 23 ListBox.TopIndex
ListBox.DataSource
<source lang="vbnet">
Imports System Imports System.Data Imports System.Windows.Forms Imports System.Data.SqlClient public class MainClass
Shared Sub Main() Dim form1 As Form = New Form1 Application.Run(form1) End Sub
End Class
Public Class Form1
Inherits System.Windows.Forms.Form
- 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 Friend WithEvents TextBox2 As System.Windows.Forms.TextBox Friend WithEvents DataSet1 As System.Data.DataSet <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.ListBox1 = New System.Windows.Forms.ListBox Me.TextBox1 = New System.Windows.Forms.TextBox Me.TextBox2 = New System.Windows.Forms.TextBox Me.DataSet1 = New System.Data.DataSet CType(Me.DataSet1, System.ruponentModel.ISupportInitialize).BeginInit() Me.SuspendLayout() " "ListBox1 " Me.ListBox1.Location = New System.Drawing.Point(8, 8) Me.ListBox1.Name = "ListBox1" Me.ListBox1.Size = New System.Drawing.Size(272, 95) Me.ListBox1.TabIndex = 0 " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(8, 112) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(128, 20) Me.TextBox1.TabIndex = 1 Me.TextBox1.Text = "TextBox1" " "TextBox2 " Me.TextBox2.Location = New System.Drawing.Point(152, 112) Me.TextBox2.Name = "TextBox2" Me.TextBox2.Size = New System.Drawing.Size(128, 20) Me.TextBox2.TabIndex = 2 Me.TextBox2.Text = "TextBox2" " "DataSet1 " Me.DataSet1.DataSetName = "NewDataSet" Me.DataSet1.Locale = New System.Globalization.CultureInfo("en-GB") " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 149) Me.Controls.Add(Me.TextBox2) Me.Controls.Add(Me.TextBox1) Me.Controls.Add(Me.ListBox1) Me.Name = "Form1" Me.Text = "Form1" CType(Me.DataSet1, System.ruponentModel.ISupportInitialize).EndInit() Me.ResumeLayout(False) End Sub
- End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load "Create Connection object Dim thisConnection As New SqlConnection _ ("server=(local)\SQLEXPRESS;" & _ "integrated security=sspi;" & _ "database=MyDatabase") " Sql Query Dim sql As String = _ "SELECT FirstName, LastName FROM Employee" " Create Data Adapter Dim da As New SqlDataAdapter(sql, thisConnection) " Fill Dataset and Create DataTable da.Fill(DataSet1, "Employee") Dim dt As DataTable = DataSet1.Tables("Employee") ListBox1.DataSource = dt ListBox1.DisplayMember = "FirstName" " Bind to firstname column of the employees table TextBox1.DataBindings.Add("text", dt, "FirstName") " Bind to lastname column of the employees table TextBox2.DataBindings.Add("text", dt, "LastName") End Sub
End Class
</source>
ListBox.DisplayMember
<source lang="vbnet"> Imports System Imports System.Data Imports System.Windows.Forms Imports System.Data.SqlClient public class MainClass
Shared Sub Main() Dim form1 As Form = New Form1 Application.Run(form1) End Sub
End Class
Public Class Form1
Inherits System.Windows.Forms.Form
- 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 Friend WithEvents TextBox2 As System.Windows.Forms.TextBox Friend WithEvents DataSet1 As System.Data.DataSet <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.ListBox1 = New System.Windows.Forms.ListBox Me.TextBox1 = New System.Windows.Forms.TextBox Me.TextBox2 = New System.Windows.Forms.TextBox Me.DataSet1 = New System.Data.DataSet CType(Me.DataSet1, System.ruponentModel.ISupportInitialize).BeginInit() Me.SuspendLayout() " "ListBox1 " Me.ListBox1.Location = New System.Drawing.Point(8, 8) Me.ListBox1.Name = "ListBox1" Me.ListBox1.Size = New System.Drawing.Size(272, 95) Me.ListBox1.TabIndex = 0 " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(8, 112) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(128, 20) Me.TextBox1.TabIndex = 1 Me.TextBox1.Text = "TextBox1" " "TextBox2 " Me.TextBox2.Location = New System.Drawing.Point(152, 112) Me.TextBox2.Name = "TextBox2" Me.TextBox2.Size = New System.Drawing.Size(128, 20) Me.TextBox2.TabIndex = 2 Me.TextBox2.Text = "TextBox2" " "DataSet1 " Me.DataSet1.DataSetName = "NewDataSet" Me.DataSet1.Locale = New System.Globalization.CultureInfo("en-GB") " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 149) Me.Controls.Add(Me.TextBox2) Me.Controls.Add(Me.TextBox1) Me.Controls.Add(Me.ListBox1) Me.Name = "Form1" Me.Text = "Form1" CType(Me.DataSet1, System.ruponentModel.ISupportInitialize).EndInit() Me.ResumeLayout(False) End Sub
- End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load "Create Connection object Dim thisConnection As New SqlConnection _ ("server=(local)\SQLEXPRESS;" & _ "integrated security=sspi;" & _ "database=MyDatabase") " Sql Query Dim sql As String = _ "SELECT FirstName, LastName FROM Employee" " Create Data Adapter Dim da As New SqlDataAdapter(sql, thisConnection) " Fill Dataset and Create DataTable da.Fill(DataSet1, "Employee") Dim dt As DataTable = DataSet1.Tables("Employee") ListBox1.DataSource = dt ListBox1.DisplayMember = "FirstName" " Bind to firstname column of the employees table TextBox1.DataBindings.Add("text", dt, "FirstName") " Bind to lastname column of the employees table TextBox2.DataBindings.Add("text", dt, "LastName") End Sub
End Class
</source>
ListBox.DoDragDrop
<source lang="vbnet">
Imports System.Drawing.Drawing2D Imports System Imports System.Drawing.Text Imports System.Drawing Imports System.Windows.Forms Imports System.Math Public Class MainClass
Shared Sub Main() Dim form1 As Form = New Form1() Application.Run(form1) End Sub
End Class Public Class Form1
Private m_DragSource As ListBox = Nothing Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load lstUnselected.AllowDrop = True lstSelected.AllowDrop = True " Add event handlers. AddHandler lstUnselected.MouseDown, AddressOf List_MouseDown AddHandler lstUnselected.DragOver, AddressOf List_DragOver AddHandler lstUnselected.DragDrop, AddressOf List_DragDrop AddHandler lstUnselected.DragLeave, AddressOf List_DragLeave AddHandler lstSelected.MouseDown, AddressOf List_MouseDown AddHandler lstSelected.DragOver, AddressOf List_DragOver AddHandler lstSelected.DragDrop, AddressOf List_DragDrop AddHandler lstSelected.DragLeave, AddressOf List_DragLeave End Sub Private Sub List_MouseDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) Dim this_list As ListBox = DirectCast(sender, ListBox) this_list.SelectedIndex = this_list.IndexFromPoint(e.X, e.Y) If this_list.SelectedIndex < 0 Then Exit Sub m_DragSource = this_list this_list.DoDragDrop(this_list.SelectedItem.ToString,DragDropEffects.Move) m_DragSource = Nothing End Sub Private Sub List_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) If m_DragSource Is Nothing Then Exit Sub e.Effect = DragDropEffects.Move Dim this_list As ListBox = DirectCast(sender, ListBox) Dim pt As Point = this_list.PointToClient(New Point(e.X, e.Y)) Dim drop_index As Integer = this_list.IndexFromPoint(pt.X, pt.Y) this_list.SelectedIndex = drop_index End Sub Private Sub List_DragLeave(ByVal sender As Object, ByVal e As System.EventArgs) Dim this_list As ListBox = DirectCast(sender, ListBox) this_list.SelectedIndex = -1 End Sub " Accept the drop. Private Sub List_DragDrop(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) Dim this_list As ListBox = DirectCast(sender, ListBox) MoveItem(e.Data.GetData(DataFormats.Text).ToString, _ m_DragSource, this_list, e.X, e.Y) End Sub " Move the value txt from drag_source to drop_target. Private Sub MoveItem(ByVal txt As String, ByVal drag_source As ListBox, _ ByVal drop_target As ListBox, ByVal X As Integer, ByVal Y As Integer) Dim drop_index As Integer = drop_target.SelectedIndex If drop_index < 0 Then drop_index = drop_target.Items.Add(txt) Else drop_target.Items.Insert(drop_target.SelectedIndex, txt) End If drop_target.SelectedIndex = drop_index If drag_source Is drop_target Then Dim target_index As Integer = drag_source.FindStringExact(txt) If target_index = drop_index Then _ target_index = drag_source.FindStringExact(txt, target_index) drag_source.Items.RemoveAt(target_index) Else drag_source.Items.Remove(txt) End If End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Public Class Form1
Inherits System.Windows.Forms.Form "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overloads 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.Label2 = New System.Windows.Forms.Label Me.Label1 = New System.Windows.Forms.Label Me.lstSelected = New System.Windows.Forms.ListBox Me.lstUnselected = New System.Windows.Forms.ListBox Me.SuspendLayout() " "Label2 " Me.Label2.Location = New System.Drawing.Point(152, 0) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(144, 16) Me.Label2.TabIndex = 11 Me.Label2.Text = "Right" Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "Label1 " Me.Label1.Location = New System.Drawing.Point(0, 0) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(144, 16) Me.Label1.TabIndex = 10 Me.Label1.Text = "Left" Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "lstSelected " Me.lstSelected.FormattingEnabled = True Me.lstSelected.Items.AddRange(New Object() {"1", "2", "3", "4", "5", "6", "7", "8"}) Me.lstSelected.Location = New System.Drawing.Point(152, 16) Me.lstSelected.Name = "lstSelected" Me.lstSelected.Size = New System.Drawing.Size(144, 264) Me.lstSelected.TabIndex = 9 " "lstUnselected " Me.lstUnselected.FormattingEnabled = True Me.lstUnselected.Items.AddRange(New Object() {"A", "B", "C", "D", "E", "F", "G", "H"}) Me.lstUnselected.Location = New System.Drawing.Point(0, 16) Me.lstUnselected.Name = "lstUnselected" Me.lstUnselected.Size = New System.Drawing.Size(144, 264) Me.lstUnselected.TabIndex = 8 " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(297, 281) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.lstSelected) Me.Controls.Add(Me.lstUnselected) Me.Name = "Form1" Me.Text = "DragBetweenListBoxes" Me.ResumeLayout(False) End Sub Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents lstSelected As System.Windows.Forms.ListBox Friend WithEvents lstUnselected As System.Windows.Forms.ListBox
End Class
</source>
ListBox.DoubleClick
<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.DragDrop
<source lang="vbnet">
Imports System.Drawing.Drawing2D Imports System Imports System.Drawing.Text Imports System.Drawing Imports System.Windows.Forms Imports System.Math Public Class MainClass
Shared Sub Main() Dim form1 As Form = New Form1() Application.Run(form1) End Sub
End Class Public Class Form1
Private m_DragSource As ListBox = Nothing Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load lstUnselected.AllowDrop = True lstSelected.AllowDrop = True " Add event handlers. AddHandler lstUnselected.MouseDown, AddressOf List_MouseDown AddHandler lstUnselected.DragOver, AddressOf List_DragOver AddHandler lstUnselected.DragDrop, AddressOf List_DragDrop AddHandler lstUnselected.DragLeave, AddressOf List_DragLeave AddHandler lstSelected.MouseDown, AddressOf List_MouseDown AddHandler lstSelected.DragOver, AddressOf List_DragOver AddHandler lstSelected.DragDrop, AddressOf List_DragDrop AddHandler lstSelected.DragLeave, AddressOf List_DragLeave End Sub Private Sub List_MouseDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) Dim this_list As ListBox = DirectCast(sender, ListBox) this_list.SelectedIndex = this_list.IndexFromPoint(e.X, e.Y) If this_list.SelectedIndex < 0 Then Exit Sub m_DragSource = this_list this_list.DoDragDrop(this_list.SelectedItem.ToString,DragDropEffects.Move) m_DragSource = Nothing End Sub Private Sub List_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) If m_DragSource Is Nothing Then Exit Sub e.Effect = DragDropEffects.Move Dim this_list As ListBox = DirectCast(sender, ListBox) Dim pt As Point = this_list.PointToClient(New Point(e.X, e.Y)) Dim drop_index As Integer = this_list.IndexFromPoint(pt.X, pt.Y) this_list.SelectedIndex = drop_index End Sub Private Sub List_DragLeave(ByVal sender As Object, ByVal e As System.EventArgs) Dim this_list As ListBox = DirectCast(sender, ListBox) this_list.SelectedIndex = -1 End Sub " Accept the drop. Private Sub List_DragDrop(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) Dim this_list As ListBox = DirectCast(sender, ListBox) MoveItem(e.Data.GetData(DataFormats.Text).ToString, _ m_DragSource, this_list, e.X, e.Y) End Sub " Move the value txt from drag_source to drop_target. Private Sub MoveItem(ByVal txt As String, ByVal drag_source As ListBox, _ ByVal drop_target As ListBox, ByVal X As Integer, ByVal Y As Integer) Dim drop_index As Integer = drop_target.SelectedIndex If drop_index < 0 Then drop_index = drop_target.Items.Add(txt) Else drop_target.Items.Insert(drop_target.SelectedIndex, txt) End If drop_target.SelectedIndex = drop_index If drag_source Is drop_target Then Dim target_index As Integer = drag_source.FindStringExact(txt) If target_index = drop_index Then _ target_index = drag_source.FindStringExact(txt, target_index) drag_source.Items.RemoveAt(target_index) Else drag_source.Items.Remove(txt) End If End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Public Class Form1
Inherits System.Windows.Forms.Form "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overloads 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.Label2 = New System.Windows.Forms.Label Me.Label1 = New System.Windows.Forms.Label Me.lstSelected = New System.Windows.Forms.ListBox Me.lstUnselected = New System.Windows.Forms.ListBox Me.SuspendLayout() " "Label2 " Me.Label2.Location = New System.Drawing.Point(152, 0) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(144, 16) Me.Label2.TabIndex = 11 Me.Label2.Text = "Right" Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "Label1 " Me.Label1.Location = New System.Drawing.Point(0, 0) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(144, 16) Me.Label1.TabIndex = 10 Me.Label1.Text = "Left" Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "lstSelected " Me.lstSelected.FormattingEnabled = True Me.lstSelected.Items.AddRange(New Object() {"1", "2", "3", "4", "5", "6", "7", "8"}) Me.lstSelected.Location = New System.Drawing.Point(152, 16) Me.lstSelected.Name = "lstSelected" Me.lstSelected.Size = New System.Drawing.Size(144, 264) Me.lstSelected.TabIndex = 9 " "lstUnselected " Me.lstUnselected.FormattingEnabled = True Me.lstUnselected.Items.AddRange(New Object() {"A", "B", "C", "D", "E", "F", "G", "H"}) Me.lstUnselected.Location = New System.Drawing.Point(0, 16) Me.lstUnselected.Name = "lstUnselected" Me.lstUnselected.Size = New System.Drawing.Size(144, 264) Me.lstUnselected.TabIndex = 8 " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(297, 281) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.lstSelected) Me.Controls.Add(Me.lstUnselected) Me.Name = "Form1" Me.Text = "DragBetweenListBoxes" Me.ResumeLayout(False) End Sub Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents lstSelected As System.Windows.Forms.ListBox Friend WithEvents lstUnselected As System.Windows.Forms.ListBox
End Class
</source>
ListBox.DragEnter
<source lang="vbnet">
Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class FileDragDrop
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Private Sub ListBox1_DragEnter(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) _ Handles ListBox1.DragEnter If (e.Data.GetDataPresent(DataFormats.FileDrop) =True) Then e.Effect = DragDropEffects.Copy End If End Sub Private Sub ListBox1_DragDrop(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) _ Handles ListBox1.DragDrop For Each oneFile As String In _ e.Data.GetData(DataFormats.FileDrop) ListBox1.Items.Add(oneFile) Next oneFile 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.SuspendLayout() " "ListBox1 " Me.ListBox1.AllowDrop = True Me.ListBox1.FormattingEnabled = True Me.ListBox1.Location = New System.Drawing.Point(8, 8) Me.ListBox1.Name = "ListBox1" Me.ListBox1.Size = New System.Drawing.Size(576, 238) Me.ListBox1.TabIndex = 0 " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(594, 256) Me.Controls.Add(Me.ListBox1) Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle Me.MaximizeBox = False Me.Name = "Form1" Me.Text = "Drag and Drop Files" Me.ResumeLayout(False) End Sub Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
End Class
</source>
ListBox.DragLeave
<source lang="vbnet">
Imports System.Drawing.Drawing2D Imports System Imports System.Drawing.Text Imports System.Drawing Imports System.Windows.Forms Imports System.Math Public Class MainClass
Shared Sub Main() Dim form1 As Form = New Form1() Application.Run(form1) End Sub
End Class Public Class Form1
Private m_DragSource As ListBox = Nothing Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load lstUnselected.AllowDrop = True lstSelected.AllowDrop = True " Add event handlers. AddHandler lstUnselected.MouseDown, AddressOf List_MouseDown AddHandler lstUnselected.DragOver, AddressOf List_DragOver AddHandler lstUnselected.DragDrop, AddressOf List_DragDrop AddHandler lstUnselected.DragLeave, AddressOf List_DragLeave AddHandler lstSelected.MouseDown, AddressOf List_MouseDown AddHandler lstSelected.DragOver, AddressOf List_DragOver AddHandler lstSelected.DragDrop, AddressOf List_DragDrop AddHandler lstSelected.DragLeave, AddressOf List_DragLeave End Sub Private Sub List_MouseDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) Dim this_list As ListBox = DirectCast(sender, ListBox) this_list.SelectedIndex = this_list.IndexFromPoint(e.X, e.Y) If this_list.SelectedIndex < 0 Then Exit Sub m_DragSource = this_list this_list.DoDragDrop(this_list.SelectedItem.ToString,DragDropEffects.Move) m_DragSource = Nothing End Sub Private Sub List_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) If m_DragSource Is Nothing Then Exit Sub e.Effect = DragDropEffects.Move Dim this_list As ListBox = DirectCast(sender, ListBox) Dim pt As Point = this_list.PointToClient(New Point(e.X, e.Y)) Dim drop_index As Integer = this_list.IndexFromPoint(pt.X, pt.Y) this_list.SelectedIndex = drop_index End Sub Private Sub List_DragLeave(ByVal sender As Object, ByVal e As System.EventArgs) Dim this_list As ListBox = DirectCast(sender, ListBox) this_list.SelectedIndex = -1 End Sub " Accept the drop. Private Sub List_DragDrop(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) Dim this_list As ListBox = DirectCast(sender, ListBox) MoveItem(e.Data.GetData(DataFormats.Text).ToString, _ m_DragSource, this_list, e.X, e.Y) End Sub " Move the value txt from drag_source to drop_target. Private Sub MoveItem(ByVal txt As String, ByVal drag_source As ListBox, _ ByVal drop_target As ListBox, ByVal X As Integer, ByVal Y As Integer) Dim drop_index As Integer = drop_target.SelectedIndex If drop_index < 0 Then drop_index = drop_target.Items.Add(txt) Else drop_target.Items.Insert(drop_target.SelectedIndex, txt) End If drop_target.SelectedIndex = drop_index If drag_source Is drop_target Then Dim target_index As Integer = drag_source.FindStringExact(txt) If target_index = drop_index Then _ target_index = drag_source.FindStringExact(txt, target_index) drag_source.Items.RemoveAt(target_index) Else drag_source.Items.Remove(txt) End If End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Public Class Form1
Inherits System.Windows.Forms.Form "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overloads 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.Label2 = New System.Windows.Forms.Label Me.Label1 = New System.Windows.Forms.Label Me.lstSelected = New System.Windows.Forms.ListBox Me.lstUnselected = New System.Windows.Forms.ListBox Me.SuspendLayout() " "Label2 " Me.Label2.Location = New System.Drawing.Point(152, 0) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(144, 16) Me.Label2.TabIndex = 11 Me.Label2.Text = "Right" Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "Label1 " Me.Label1.Location = New System.Drawing.Point(0, 0) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(144, 16) Me.Label1.TabIndex = 10 Me.Label1.Text = "Left" Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "lstSelected " Me.lstSelected.FormattingEnabled = True Me.lstSelected.Items.AddRange(New Object() {"1", "2", "3", "4", "5", "6", "7", "8"}) Me.lstSelected.Location = New System.Drawing.Point(152, 16) Me.lstSelected.Name = "lstSelected" Me.lstSelected.Size = New System.Drawing.Size(144, 264) Me.lstSelected.TabIndex = 9 " "lstUnselected " Me.lstUnselected.FormattingEnabled = True Me.lstUnselected.Items.AddRange(New Object() {"A", "B", "C", "D", "E", "F", "G", "H"}) Me.lstUnselected.Location = New System.Drawing.Point(0, 16) Me.lstUnselected.Name = "lstUnselected" Me.lstUnselected.Size = New System.Drawing.Size(144, 264) Me.lstUnselected.TabIndex = 8 " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(297, 281) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.lstSelected) Me.Controls.Add(Me.lstUnselected) Me.Name = "Form1" Me.Text = "DragBetweenListBoxes" Me.ResumeLayout(False) End Sub Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents lstSelected As System.Windows.Forms.ListBox Friend WithEvents lstUnselected As System.Windows.Forms.ListBox
End Class
</source>
ListBox.DragOver
<source lang="vbnet"> Imports System.Drawing.Drawing2D Imports System Imports System.Drawing.Text Imports System.Drawing Imports System.Windows.Forms Imports System.Math Public Class MainClass
Shared Sub Main() Dim form1 As Form = New Form1() Application.Run(form1) End Sub
End Class Public Class Form1
Private m_DragSource As ListBox = Nothing Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load lstUnselected.AllowDrop = True lstSelected.AllowDrop = True " Add event handlers. AddHandler lstUnselected.MouseDown, AddressOf List_MouseDown AddHandler lstUnselected.DragOver, AddressOf List_DragOver AddHandler lstUnselected.DragDrop, AddressOf List_DragDrop AddHandler lstUnselected.DragLeave, AddressOf List_DragLeave AddHandler lstSelected.MouseDown, AddressOf List_MouseDown AddHandler lstSelected.DragOver, AddressOf List_DragOver AddHandler lstSelected.DragDrop, AddressOf List_DragDrop AddHandler lstSelected.DragLeave, AddressOf List_DragLeave End Sub Private Sub List_MouseDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) Dim this_list As ListBox = DirectCast(sender, ListBox) this_list.SelectedIndex = this_list.IndexFromPoint(e.X, e.Y) If this_list.SelectedIndex < 0 Then Exit Sub m_DragSource = this_list this_list.DoDragDrop(this_list.SelectedItem.ToString,DragDropEffects.Move) m_DragSource = Nothing End Sub Private Sub List_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) If m_DragSource Is Nothing Then Exit Sub e.Effect = DragDropEffects.Move Dim this_list As ListBox = DirectCast(sender, ListBox) Dim pt As Point = this_list.PointToClient(New Point(e.X, e.Y)) Dim drop_index As Integer = this_list.IndexFromPoint(pt.X, pt.Y) this_list.SelectedIndex = drop_index End Sub Private Sub List_DragLeave(ByVal sender As Object, ByVal e As System.EventArgs) Dim this_list As ListBox = DirectCast(sender, ListBox) this_list.SelectedIndex = -1 End Sub " Accept the drop. Private Sub List_DragDrop(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) Dim this_list As ListBox = DirectCast(sender, ListBox) MoveItem(e.Data.GetData(DataFormats.Text).ToString, _ m_DragSource, this_list, e.X, e.Y) End Sub " Move the value txt from drag_source to drop_target. Private Sub MoveItem(ByVal txt As String, ByVal drag_source As ListBox, _ ByVal drop_target As ListBox, ByVal X As Integer, ByVal Y As Integer) Dim drop_index As Integer = drop_target.SelectedIndex If drop_index < 0 Then drop_index = drop_target.Items.Add(txt) Else drop_target.Items.Insert(drop_target.SelectedIndex, txt) End If drop_target.SelectedIndex = drop_index If drag_source Is drop_target Then Dim target_index As Integer = drag_source.FindStringExact(txt) If target_index = drop_index Then _ target_index = drag_source.FindStringExact(txt, target_index) drag_source.Items.RemoveAt(target_index) Else drag_source.Items.Remove(txt) End If End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Public Class Form1
Inherits System.Windows.Forms.Form "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overloads 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.Label2 = New System.Windows.Forms.Label Me.Label1 = New System.Windows.Forms.Label Me.lstSelected = New System.Windows.Forms.ListBox Me.lstUnselected = New System.Windows.Forms.ListBox Me.SuspendLayout() " "Label2 " Me.Label2.Location = New System.Drawing.Point(152, 0) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(144, 16) Me.Label2.TabIndex = 11 Me.Label2.Text = "Right" Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "Label1 " Me.Label1.Location = New System.Drawing.Point(0, 0) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(144, 16) Me.Label1.TabIndex = 10 Me.Label1.Text = "Left" Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "lstSelected " Me.lstSelected.FormattingEnabled = True Me.lstSelected.Items.AddRange(New Object() {"1", "2", "3", "4", "5", "6", "7", "8"}) Me.lstSelected.Location = New System.Drawing.Point(152, 16) Me.lstSelected.Name = "lstSelected" Me.lstSelected.Size = New System.Drawing.Size(144, 264) Me.lstSelected.TabIndex = 9 " "lstUnselected " Me.lstUnselected.FormattingEnabled = True Me.lstUnselected.Items.AddRange(New Object() {"A", "B", "C", "D", "E", "F", "G", "H"}) Me.lstUnselected.Location = New System.Drawing.Point(0, 16) Me.lstUnselected.Name = "lstUnselected" Me.lstUnselected.Size = New System.Drawing.Size(144, 264) Me.lstUnselected.TabIndex = 8 " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(297, 281) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.lstSelected) Me.Controls.Add(Me.lstUnselected) Me.Name = "Form1" Me.Text = "DragBetweenListBoxes" Me.ResumeLayout(False) End Sub Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents lstSelected As System.Windows.Forms.ListBox Friend WithEvents lstUnselected As System.Windows.Forms.ListBox
End Class
</source>
ListBox.DrawMode
<source lang="vbnet"> Imports System Imports System.Drawing Imports System.Windows.Forms
Public Class MainClass
Public Shared Sub Main() Application.Run(New OwnerDrawnFixedListBox()) End Sub
End Class Public Class OwnerDrawnFixedListBox
Inherits System.Windows.Forms.Form
- 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() {"A", "B", "C"}) Me.listBox1.Location = New System.Drawing.Point(8, 16) 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(264, 198) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.listBox1}) Me.Name = "OwnerDrawnFixedListBox" Me.Text = "OwnerDrawnFixedListBox" Me.ResumeLayout(False) End Sub
- 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), e.Bounds.X, e.Bounds.Y) e.DrawFocusRectangle() End Sub
End Class
</source>
ListBox.IndexFromPoint
<source lang="vbnet">
Imports System.Drawing.Drawing2D Imports System Imports System.Drawing.Text Imports System.Drawing Imports System.Windows.Forms Imports System.Math Public Class MainClass
Shared Sub Main() Dim form1 As Form = New Form1() Application.Run(form1) End Sub
End Class Public Class Form1
Private m_DragSource As ListBox = Nothing Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load lstUnselected.AllowDrop = True lstSelected.AllowDrop = True " Add event handlers. AddHandler lstUnselected.MouseDown, AddressOf List_MouseDown AddHandler lstUnselected.DragOver, AddressOf List_DragOver AddHandler lstUnselected.DragDrop, AddressOf List_DragDrop AddHandler lstUnselected.DragLeave, AddressOf List_DragLeave AddHandler lstSelected.MouseDown, AddressOf List_MouseDown AddHandler lstSelected.DragOver, AddressOf List_DragOver AddHandler lstSelected.DragDrop, AddressOf List_DragDrop AddHandler lstSelected.DragLeave, AddressOf List_DragLeave End Sub Private Sub List_MouseDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) Dim this_list As ListBox = DirectCast(sender, ListBox) this_list.SelectedIndex = this_list.IndexFromPoint(e.X, e.Y) If this_list.SelectedIndex < 0 Then Exit Sub m_DragSource = this_list this_list.DoDragDrop(this_list.SelectedItem.ToString,DragDropEffects.Move) m_DragSource = Nothing End Sub Private Sub List_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) If m_DragSource Is Nothing Then Exit Sub e.Effect = DragDropEffects.Move Dim this_list As ListBox = DirectCast(sender, ListBox) Dim pt As Point = this_list.PointToClient(New Point(e.X, e.Y)) Dim drop_index As Integer = this_list.IndexFromPoint(pt.X, pt.Y) this_list.SelectedIndex = drop_index End Sub Private Sub List_DragLeave(ByVal sender As Object, ByVal e As System.EventArgs) Dim this_list As ListBox = DirectCast(sender, ListBox) this_list.SelectedIndex = -1 End Sub " Accept the drop. Private Sub List_DragDrop(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) Dim this_list As ListBox = DirectCast(sender, ListBox) MoveItem(e.Data.GetData(DataFormats.Text).ToString, _ m_DragSource, this_list, e.X, e.Y) End Sub " Move the value txt from drag_source to drop_target. Private Sub MoveItem(ByVal txt As String, ByVal drag_source As ListBox, _ ByVal drop_target As ListBox, ByVal X As Integer, ByVal Y As Integer) Dim drop_index As Integer = drop_target.SelectedIndex If drop_index < 0 Then drop_index = drop_target.Items.Add(txt) Else drop_target.Items.Insert(drop_target.SelectedIndex, txt) End If drop_target.SelectedIndex = drop_index If drag_source Is drop_target Then Dim target_index As Integer = drag_source.FindStringExact(txt) If target_index = drop_index Then _ target_index = drag_source.FindStringExact(txt, target_index) drag_source.Items.RemoveAt(target_index) Else drag_source.Items.Remove(txt) End If End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Public Class Form1
Inherits System.Windows.Forms.Form "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overloads 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.Label2 = New System.Windows.Forms.Label Me.Label1 = New System.Windows.Forms.Label Me.lstSelected = New System.Windows.Forms.ListBox Me.lstUnselected = New System.Windows.Forms.ListBox Me.SuspendLayout() " "Label2 " Me.Label2.Location = New System.Drawing.Point(152, 0) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(144, 16) Me.Label2.TabIndex = 11 Me.Label2.Text = "Right" Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "Label1 " Me.Label1.Location = New System.Drawing.Point(0, 0) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(144, 16) Me.Label1.TabIndex = 10 Me.Label1.Text = "Left" Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "lstSelected " Me.lstSelected.FormattingEnabled = True Me.lstSelected.Items.AddRange(New Object() {"1", "2", "3", "4", "5", "6", "7", "8"}) Me.lstSelected.Location = New System.Drawing.Point(152, 16) Me.lstSelected.Name = "lstSelected" Me.lstSelected.Size = New System.Drawing.Size(144, 264) Me.lstSelected.TabIndex = 9 " "lstUnselected " Me.lstUnselected.FormattingEnabled = True Me.lstUnselected.Items.AddRange(New Object() {"A", "B", "C", "D", "E", "F", "G", "H"}) Me.lstUnselected.Location = New System.Drawing.Point(0, 16) Me.lstUnselected.Name = "lstUnselected" Me.lstUnselected.Size = New System.Drawing.Size(144, 264) Me.lstUnselected.TabIndex = 8 " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(297, 281) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.lstSelected) Me.Controls.Add(Me.lstUnselected) Me.Name = "Form1" Me.Text = "DragBetweenListBoxes" Me.ResumeLayout(False) End Sub Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents lstSelected As System.Windows.Forms.ListBox Friend WithEvents lstUnselected As System.Windows.Forms.ListBox
End Class
</source>
ListBox.ItemHeight
<source lang="vbnet"> Imports System Imports System.Drawing Imports System.Windows.Forms
Public Class MainClass
Public Shared Sub Main() Application.Run(New OwnerDrawnFixedListBox()) End Sub
End Class Public Class OwnerDrawnFixedListBox
Inherits System.Windows.Forms.Form
- 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() {"A", "B", "C"}) Me.listBox1.Location = New System.Drawing.Point(8, 16) 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(264, 198) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.listBox1}) Me.Name = "OwnerDrawnFixedListBox" Me.Text = "OwnerDrawnFixedListBox" Me.ResumeLayout(False) End Sub
- 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), e.Bounds.X, e.Bounds.Y) e.DrawFocusRectangle() End Sub
End Class
</source>
ListBox.Items.Add
<source lang="vbnet">
Imports System Imports System.Drawing Imports System.Windows.Forms Public Class MainClass
Shared Sub Main() Dim myform As Form = New FrmCheckedListBox() Application.Run(myform) End Sub " Main
End Class Public Class FrmCheckedListBox
Inherits Form
- 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 " list of available book titles Friend WithEvents chklstInput As CheckedListBox " user selection list Friend WithEvents lstDisplay As ListBox "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.chklstInput = New System.Windows.Forms.CheckedListBox() Me.lstDisplay = New System.Windows.Forms.ListBox() Me.SuspendLayout() " "chklstInput " Me.chklstInput.HorizontalScrollbar = True Me.chklstInput.Items.AddRange(New Object() {"C++", "Java", "VB", "C#", "Perl", "Python HTP", "C", "SQL"}) Me.chklstInput.Location = New System.Drawing.Point(16, 16) Me.chklstInput.Name = "chklstInput" Me.chklstInput.Size = New System.Drawing.Size(152, 124) Me.chklstInput.TabIndex = 1 " "lstDisplay " Me.lstDisplay.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right) Me.lstDisplay.HorizontalScrollbar = True Me.lstDisplay.Location = New System.Drawing.Point(184, 16) Me.lstDisplay.Name = "lstDisplay" Me.lstDisplay.Size = New System.Drawing.Size(128, 121) Me.lstDisplay.TabIndex = 2 " "FrmCheckedListBox " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(328, 157) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lstDisplay, Me.chklstInput}) Me.Name = "FrmCheckedListBox" Me.Text = "Checked List Box Test" Me.ResumeLayout(False) End Sub
- End Region
Private Sub chklstInput_ItemCheck _ (ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.ItemCheckEventArgs) _ Handles chklstInput.ItemCheck Dim item As String = chklstInput.SelectedItem If e.NewValue = CheckState.Checked Then lstDisplay.Items.Add(item) Else lstDisplay.Items.Remove(item) End If End Sub
End Class
</source>
ListBox.Items.Clear()
<source lang="vbnet"> Imports System Imports System.Windows.Forms Public Class MainClass
Shared Sub Main() Dim form1 As Form1 = new Form1 Application.Run(form1) End Sub
End Class Public Class Form1
Inherits System.Windows.Forms.Form
- 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 Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents ListBox1 As System.Windows.Forms.ListBox "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.Button1 = New System.Windows.Forms.Button() Me.ListBox1 = New System.Windows.Forms.ListBox() Me.SuspendLayout() " "Button1 " Me.Button1.Location = New System.Drawing.Point(32, 328) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(280, 32) Me.Button1.TabIndex = 0 Me.Button1.Text = "Show hidden files" " "ListBox1 " Me.ListBox1.HorizontalScrollbar = True Me.ListBox1.ItemHeight = 16 Me.ListBox1.Location = New System.Drawing.Point(24, 56) Me.ListBox1.Name = "ListBox1" Me.ListBox1.ScrollAlwaysVisible = True Me.ListBox1.Size = New System.Drawing.Size(280, 244) Me.ListBox1.TabIndex = 1 " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15) Me.ClientSize = New System.Drawing.Size(328, 384) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.ListBox1, Me.Button1}) Me.Name = "Form1" Me.Text = "Add to List Box" Me.ResumeLayout(False) End Sub
- End Region
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Me.Cursor = System.Windows.Forms.Cursors.WaitCursor ListBox1.Items.Clear() ListBox1.Items.Add("Item A") Me.Cursor = System.Windows.Forms.Cursors.Default End Sub
End Class
</source>
ListBox.Items.Remove
<source lang="vbnet">
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class MainClass
Shared Sub Main() Dim myform As Form = New FrmCheckedListBox() Application.Run(myform) End Sub " Main
End Class Public Class FrmCheckedListBox
Inherits Form
- 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 " list of available book titles Friend WithEvents chklstInput As CheckedListBox " user selection list Friend WithEvents lstDisplay As ListBox "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.chklstInput = New System.Windows.Forms.CheckedListBox() Me.lstDisplay = New System.Windows.Forms.ListBox() Me.SuspendLayout() " "chklstInput " Me.chklstInput.HorizontalScrollbar = True Me.chklstInput.Items.AddRange(New Object() {"C++", "Java", "VB", "C#", "Perl", "Python HTP", "C", "SQL"}) Me.chklstInput.Location = New System.Drawing.Point(16, 16) Me.chklstInput.Name = "chklstInput" Me.chklstInput.Size = New System.Drawing.Size(152, 124) Me.chklstInput.TabIndex = 1 " "lstDisplay " Me.lstDisplay.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right) Me.lstDisplay.HorizontalScrollbar = True Me.lstDisplay.Location = New System.Drawing.Point(184, 16) Me.lstDisplay.Name = "lstDisplay" Me.lstDisplay.Size = New System.Drawing.Size(128, 121) Me.lstDisplay.TabIndex = 2 " "FrmCheckedListBox " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(328, 157) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lstDisplay, Me.chklstInput}) Me.Name = "FrmCheckedListBox" Me.Text = "Checked List Box Test" Me.ResumeLayout(False) End Sub
- End Region
Private Sub chklstInput_ItemCheck _ (ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.ItemCheckEventArgs) _ Handles chklstInput.ItemCheck Dim item As String = chklstInput.SelectedItem If e.NewValue = CheckState.Checked Then lstDisplay.Items.Add(item) Else lstDisplay.Items.Remove(item) End If End Sub
End Class
</source>
ListBox.Items.RemoveAt
<source lang="vbnet"> Imports System.Windows.Forms public class DualList
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Private Sub btnAddSource_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddSource.Click lstSource.Items.Add("A") End Sub Private Sub btnAddDestination_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddDestination.Click lstDestination.Items.Add("B") End Sub Private Sub btnClearSource_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearSource.Click lstSource.Items.Clear() End Sub Private Sub btnClearDestination_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearDestination.Click lstDestination.Items.Clear() End Sub Private Sub btnRemoveSource_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemoveSource.Click If lstSource.SelectedItems.Count > 0 Then lstSource.Items.Remove(lstSource.SelectedItem) End If End Sub Private Sub btnRemoveDestination_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemoveDestination.Click Dim intCounter As Integer For intCounter = 0 To lstDestination.SelectedIndices.Count - 1 lstDestination.Items.RemoveAt(lstDestination.SelectedIndices(0)) Next intCounter End Sub Private Sub btnMoveDestination_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveDestination.Click If lstSource.SelectedItems.Count > 0 Then lstDestination.Items.Add(lstSource.SelectedItem) lstSource.Items.RemoveAt(lstSource.SelectedIndex) End If End Sub Private Sub btnMoveSource_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveSource.Click Dim intCounter As Integer For intCounter = 0 To lstDestination.SelectedIndices.Count - 1 lstSource.Items.Add(lstDestination.Items(lstDestination.SelectedIndices(0))) lstDestination.Items.Remove(lstDestination.Items(lstDestination.SelectedIndices(0))) Next intCounter End Sub Private Sub btnMoveAllDestination_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveAllDestination.Click Dim intCounter As Integer For intCounter = 0 To lstSource.Items.Count - 1 lstDestination.Items.Add(lstSource.Items(0)) lstSource.Items.RemoveAt(0) Next intCounter End Sub Private Sub btnMoveAllSource_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveAllSource.Click Dim intCounter As Integer For intCounter = 0 To lstDestination.Items.Count - 1 lstSource.Items.Add(lstDestination.Items(0)) lstDestination.Items.RemoveAt(0) Next intCounter End Sub
End Class Partial Public Class Form1
Inherits System.Windows.Forms.Form <System.Diagnostics.DebuggerNonUserCode()> _ Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() End Sub "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overloads 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.lstSource = New System.Windows.Forms.ListBox Me.lstDestination = New System.Windows.Forms.ListBox Me.btnMoveAllDestination = New System.Windows.Forms.Button Me.btnMoveDestination = New System.Windows.Forms.Button Me.btnMoveSource = New System.Windows.Forms.Button Me.btnMoveAllSource = New System.Windows.Forms.Button Me.btnAddSource = New System.Windows.Forms.Button Me.btnRemoveSource = New System.Windows.Forms.Button Me.btnClearSource = New System.Windows.Forms.Button Me.btnAddDestination = New System.Windows.Forms.Button Me.btnRemoveDestination = New System.Windows.Forms.Button Me.btnClearDestination = New System.Windows.Forms.Button Me.Label1 = New System.Windows.Forms.Label Me.Label2 = New System.Windows.Forms.Label Me.SuspendLayout() " "lstSource " Me.lstSource.FormattingEnabled = True Me.lstSource.Location = New System.Drawing.Point(21, 25) Me.lstSource.Name = "lstSource" Me.lstSource.Size = New System.Drawing.Size(162, 199) Me.lstSource.TabIndex = 0 " "lstDestination " Me.lstDestination.FormattingEnabled = True Me.lstDestination.Location = New System.Drawing.Point(254, 25) Me.lstDestination.Name = "lstDestination" Me.lstDestination.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended Me.lstDestination.Size = New System.Drawing.Size(162, 199) Me.lstDestination.Sorted = True Me.lstDestination.TabIndex = 1 " "btnMoveAllDestination " Me.btnMoveAllDestination.Location = New System.Drawing.Point(201, 48) Me.btnMoveAllDestination.Name = "btnMoveAllDestination" Me.btnMoveAllDestination.Size = New System.Drawing.Size(36, 28) Me.btnMoveAllDestination.TabIndex = 2 Me.btnMoveAllDestination.Text = ">>" " "btnMoveDestination " Me.btnMoveDestination.Location = New System.Drawing.Point(201, 83) Me.btnMoveDestination.Name = "btnMoveDestination" Me.btnMoveDestination.Size = New System.Drawing.Size(36, 28) Me.btnMoveDestination.TabIndex = 3 Me.btnMoveDestination.Text = ">" " "btnMoveSource " Me.btnMoveSource.Location = New System.Drawing.Point(201, 141) Me.btnMoveSource.Name = "btnMoveSource" Me.btnMoveSource.Size = New System.Drawing.Size(36, 28) Me.btnMoveSource.TabIndex = 4 Me.btnMoveSource.Text = "<" " "btnMoveAllSource " Me.btnMoveAllSource.Location = New System.Drawing.Point(201, 176) Me.btnMoveAllSource.Name = "btnMoveAllSource" Me.btnMoveAllSource.Size = New System.Drawing.Size(36, 28) Me.btnMoveAllSource.TabIndex = 5 Me.btnMoveAllSource.Text = "<<" " "btnAddSource " Me.btnAddSource.Location = New System.Drawing.Point(21, 237) Me.btnAddSource.Name = "btnAddSource" Me.btnAddSource.Size = New System.Drawing.Size(161, 24) Me.btnAddSource.TabIndex = 6 Me.btnAddSource.Text = "Add Item" " "btnRemoveSource " Me.btnRemoveSource.Location = New System.Drawing.Point(22, 268) Me.btnRemoveSource.Name = "btnRemoveSource" Me.btnRemoveSource.Size = New System.Drawing.Size(161, 24) Me.btnRemoveSource.TabIndex = 7 Me.btnRemoveSource.Text = "Remove Item" " "btnClearSource " Me.btnClearSource.Location = New System.Drawing.Point(22, 299) Me.btnClearSource.Name = "btnClearSource" Me.btnClearSource.Size = New System.Drawing.Size(161, 24) Me.btnClearSource.TabIndex = 8 Me.btnClearSource.Text = "Clear Items" " "btnAddDestination " Me.btnAddDestination.Location = New System.Drawing.Point(254, 237) Me.btnAddDestination.Name = "btnAddDestination" Me.btnAddDestination.Size = New System.Drawing.Size(161, 24) Me.btnAddDestination.TabIndex = 9 Me.btnAddDestination.Text = "Add Item" " "btnRemoveDestination " Me.btnRemoveDestination.Location = New System.Drawing.Point(254, 268) Me.btnRemoveDestination.Name = "btnRemoveDestination" Me.btnRemoveDestination.Size = New System.Drawing.Size(161, 24) Me.btnRemoveDestination.TabIndex = 10 Me.btnRemoveDestination.Text = "Remove Item(s)" " "btnClearDestination " Me.btnClearDestination.Location = New System.Drawing.Point(254, 299) Me.btnClearDestination.Name = "btnClearDestination" Me.btnClearDestination.Size = New System.Drawing.Size(161, 24) Me.btnClearDestination.TabIndex = 11 Me.btnClearDestination.Text = "Clear Items" " "Label1 " Me.Label1.AutoSize = True Me.Label1.Location = New System.Drawing.Point(20, 6) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(74, 14) Me.Label1.TabIndex = 12 Me.Label1.Text = "Unsorted List:" " "Label2 " Me.Label2.AutoSize = True Me.Label2.Location = New System.Drawing.Point(254, 6) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(61, 14) Me.Label2.TabIndex = 13 Me.Label2.Text = "Sorted List:" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(438, 340) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.btnClearDestination) Me.Controls.Add(Me.btnRemoveDestination) Me.Controls.Add(Me.btnAddDestination) Me.Controls.Add(Me.btnClearSource) Me.Controls.Add(Me.btnRemoveSource) Me.Controls.Add(Me.btnAddSource) Me.Controls.Add(Me.btnMoveAllSource) Me.Controls.Add(Me.btnMoveSource) Me.Controls.Add(Me.btnMoveDestination) Me.Controls.Add(Me.btnMoveAllDestination) Me.Controls.Add(Me.lstDestination) Me.Controls.Add(Me.lstSource) Me.Name = "Form1" Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen Me.Text = "ListDemo" Me.ResumeLayout(False) Me.PerformLayout() End Sub Friend WithEvents lstSource As System.Windows.Forms.ListBox Friend WithEvents lstDestination As System.Windows.Forms.ListBox Friend WithEvents btnMoveAllDestination As System.Windows.Forms.Button Friend WithEvents btnMoveDestination As System.Windows.Forms.Button Friend WithEvents btnMoveSource As System.Windows.Forms.Button Friend WithEvents btnMoveAllSource As System.Windows.Forms.Button Friend WithEvents btnAddSource As System.Windows.Forms.Button Friend WithEvents btnRemoveSource As System.Windows.Forms.Button Friend WithEvents btnClearSource As System.Windows.Forms.Button Friend WithEvents btnAddDestination As System.Windows.Forms.Button Friend WithEvents btnRemoveDestination As System.Windows.Forms.Button Friend WithEvents btnClearDestination As System.Windows.Forms.Button Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Label2 As System.Windows.Forms.Label
End Class
</source>
ListBox.MeasureItem
<source lang="vbnet"> Imports System Imports System.Drawing Imports System.Windows.Forms
Public Class MainClass
Public Shared Sub Main() Application.Run(New OwnerDrawnVariableListBox()) End Sub
End Class Public Class OwnerDrawnVariableListBox
Inherits System.Windows.Forms.Form
- 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() {"A", "a", "B"}) Me.listBox2.Location = New System.Drawing.Point(6, 15) 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, 222) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.listBox2}) Me.Name = "OwnerDrawnVariableListBox" Me.Text = "OwnerDrawnVariableListBox" Me.ResumeLayout(False) End Sub
- 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 textFont = New Font(e.Font.FontFamily, e.Font.Size * 2) e.Graphics.DrawString(listBox2.Items(e.Index).ToString(), _ textFont, _ New SolidBrush(e.ForeColor), _ e.Bounds.X, e.Bounds.Y) 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 *= 2 End Sub
End Class
</source>
ListBox.MouseDown
<source lang="vbnet"> Imports System.Drawing.Drawing2D Imports System Imports System.Drawing.Text Imports System.Drawing Imports System.Windows.Forms Imports System.Math Public Class MainClass
Shared Sub Main() Dim form1 As Form = New Form1() Application.Run(form1) End Sub
End Class Public Class Form1
Private m_DragSource As ListBox = Nothing Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load lstUnselected.AllowDrop = True lstSelected.AllowDrop = True " Add event handlers. AddHandler lstUnselected.MouseDown, AddressOf List_MouseDown AddHandler lstUnselected.DragOver, AddressOf List_DragOver AddHandler lstUnselected.DragDrop, AddressOf List_DragDrop AddHandler lstUnselected.DragLeave, AddressOf List_DragLeave AddHandler lstSelected.MouseDown, AddressOf List_MouseDown AddHandler lstSelected.DragOver, AddressOf List_DragOver AddHandler lstSelected.DragDrop, AddressOf List_DragDrop AddHandler lstSelected.DragLeave, AddressOf List_DragLeave End Sub Private Sub List_MouseDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) Dim this_list As ListBox = DirectCast(sender, ListBox) this_list.SelectedIndex = this_list.IndexFromPoint(e.X, e.Y) If this_list.SelectedIndex < 0 Then Exit Sub m_DragSource = this_list this_list.DoDragDrop(this_list.SelectedItem.ToString,DragDropEffects.Move) m_DragSource = Nothing End Sub Private Sub List_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) If m_DragSource Is Nothing Then Exit Sub e.Effect = DragDropEffects.Move Dim this_list As ListBox = DirectCast(sender, ListBox) Dim pt As Point = this_list.PointToClient(New Point(e.X, e.Y)) Dim drop_index As Integer = this_list.IndexFromPoint(pt.X, pt.Y) this_list.SelectedIndex = drop_index End Sub Private Sub List_DragLeave(ByVal sender As Object, ByVal e As System.EventArgs) Dim this_list As ListBox = DirectCast(sender, ListBox) this_list.SelectedIndex = -1 End Sub " Accept the drop. Private Sub List_DragDrop(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) Dim this_list As ListBox = DirectCast(sender, ListBox) MoveItem(e.Data.GetData(DataFormats.Text).ToString, _ m_DragSource, this_list, e.X, e.Y) End Sub " Move the value txt from drag_source to drop_target. Private Sub MoveItem(ByVal txt As String, ByVal drag_source As ListBox, _ ByVal drop_target As ListBox, ByVal X As Integer, ByVal Y As Integer) Dim drop_index As Integer = drop_target.SelectedIndex If drop_index < 0 Then drop_index = drop_target.Items.Add(txt) Else drop_target.Items.Insert(drop_target.SelectedIndex, txt) End If drop_target.SelectedIndex = drop_index If drag_source Is drop_target Then Dim target_index As Integer = drag_source.FindStringExact(txt) If target_index = drop_index Then _ target_index = drag_source.FindStringExact(txt, target_index) drag_source.Items.RemoveAt(target_index) Else drag_source.Items.Remove(txt) End If End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Public Class Form1
Inherits System.Windows.Forms.Form "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overloads 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.Label2 = New System.Windows.Forms.Label Me.Label1 = New System.Windows.Forms.Label Me.lstSelected = New System.Windows.Forms.ListBox Me.lstUnselected = New System.Windows.Forms.ListBox Me.SuspendLayout() " "Label2 " Me.Label2.Location = New System.Drawing.Point(152, 0) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(144, 16) Me.Label2.TabIndex = 11 Me.Label2.Text = "Right" Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "Label1 " Me.Label1.Location = New System.Drawing.Point(0, 0) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(144, 16) Me.Label1.TabIndex = 10 Me.Label1.Text = "Left" Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "lstSelected " Me.lstSelected.FormattingEnabled = True Me.lstSelected.Items.AddRange(New Object() {"1", "2", "3", "4", "5", "6", "7", "8"}) Me.lstSelected.Location = New System.Drawing.Point(152, 16) Me.lstSelected.Name = "lstSelected" Me.lstSelected.Size = New System.Drawing.Size(144, 264) Me.lstSelected.TabIndex = 9 " "lstUnselected " Me.lstUnselected.FormattingEnabled = True Me.lstUnselected.Items.AddRange(New Object() {"A", "B", "C", "D", "E", "F", "G", "H"}) Me.lstUnselected.Location = New System.Drawing.Point(0, 16) Me.lstUnselected.Name = "lstUnselected" Me.lstUnselected.Size = New System.Drawing.Size(144, 264) Me.lstUnselected.TabIndex = 8 " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(297, 281) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.lstSelected) Me.Controls.Add(Me.lstUnselected) Me.Name = "Form1" Me.Text = "DragBetweenListBoxes" Me.ResumeLayout(False) End Sub Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents lstSelected As System.Windows.Forms.ListBox Friend WithEvents lstUnselected As System.Windows.Forms.ListBox
End Class
</source>
ListBox.PointToClient
<source lang="vbnet"> Imports System.Drawing.Drawing2D Imports System Imports System.Drawing.Text Imports System.Drawing Imports System.Windows.Forms Imports System.Math Public Class MainClass
Shared Sub Main() Dim form1 As Form = New Form1() Application.Run(form1) End Sub
End Class Public Class Form1
Private m_DragSource As ListBox = Nothing Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load lstUnselected.AllowDrop = True lstSelected.AllowDrop = True " Add event handlers. AddHandler lstUnselected.MouseDown, AddressOf List_MouseDown AddHandler lstUnselected.DragOver, AddressOf List_DragOver AddHandler lstUnselected.DragDrop, AddressOf List_DragDrop AddHandler lstUnselected.DragLeave, AddressOf List_DragLeave AddHandler lstSelected.MouseDown, AddressOf List_MouseDown AddHandler lstSelected.DragOver, AddressOf List_DragOver AddHandler lstSelected.DragDrop, AddressOf List_DragDrop AddHandler lstSelected.DragLeave, AddressOf List_DragLeave End Sub Private Sub List_MouseDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) Dim this_list As ListBox = DirectCast(sender, ListBox) this_list.SelectedIndex = this_list.IndexFromPoint(e.X, e.Y) If this_list.SelectedIndex < 0 Then Exit Sub m_DragSource = this_list this_list.DoDragDrop(this_list.SelectedItem.ToString,DragDropEffects.Move) m_DragSource = Nothing End Sub Private Sub List_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) If m_DragSource Is Nothing Then Exit Sub e.Effect = DragDropEffects.Move Dim this_list As ListBox = DirectCast(sender, ListBox) Dim pt As Point = this_list.PointToClient(New Point(e.X, e.Y)) Dim drop_index As Integer = this_list.IndexFromPoint(pt.X, pt.Y) this_list.SelectedIndex = drop_index End Sub Private Sub List_DragLeave(ByVal sender As Object, ByVal e As System.EventArgs) Dim this_list As ListBox = DirectCast(sender, ListBox) this_list.SelectedIndex = -1 End Sub " Accept the drop. Private Sub List_DragDrop(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) Dim this_list As ListBox = DirectCast(sender, ListBox) MoveItem(e.Data.GetData(DataFormats.Text).ToString, _ m_DragSource, this_list, e.X, e.Y) End Sub " Move the value txt from drag_source to drop_target. Private Sub MoveItem(ByVal txt As String, ByVal drag_source As ListBox, _ ByVal drop_target As ListBox, ByVal X As Integer, ByVal Y As Integer) Dim drop_index As Integer = drop_target.SelectedIndex If drop_index < 0 Then drop_index = drop_target.Items.Add(txt) Else drop_target.Items.Insert(drop_target.SelectedIndex, txt) End If drop_target.SelectedIndex = drop_index If drag_source Is drop_target Then Dim target_index As Integer = drag_source.FindStringExact(txt) If target_index = drop_index Then _ target_index = drag_source.FindStringExact(txt, target_index) drag_source.Items.RemoveAt(target_index) Else drag_source.Items.Remove(txt) End If End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Public Class Form1
Inherits System.Windows.Forms.Form "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overloads 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.Label2 = New System.Windows.Forms.Label Me.Label1 = New System.Windows.Forms.Label Me.lstSelected = New System.Windows.Forms.ListBox Me.lstUnselected = New System.Windows.Forms.ListBox Me.SuspendLayout() " "Label2 " Me.Label2.Location = New System.Drawing.Point(152, 0) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(144, 16) Me.Label2.TabIndex = 11 Me.Label2.Text = "Right" Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "Label1 " Me.Label1.Location = New System.Drawing.Point(0, 0) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(144, 16) Me.Label1.TabIndex = 10 Me.Label1.Text = "Left" Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "lstSelected " Me.lstSelected.FormattingEnabled = True Me.lstSelected.Items.AddRange(New Object() {"1", "2", "3", "4", "5", "6", "7", "8"}) Me.lstSelected.Location = New System.Drawing.Point(152, 16) Me.lstSelected.Name = "lstSelected" Me.lstSelected.Size = New System.Drawing.Size(144, 264) Me.lstSelected.TabIndex = 9 " "lstUnselected " Me.lstUnselected.FormattingEnabled = True Me.lstUnselected.Items.AddRange(New Object() {"A", "B", "C", "D", "E", "F", "G", "H"}) Me.lstUnselected.Location = New System.Drawing.Point(0, 16) Me.lstUnselected.Name = "lstUnselected" Me.lstUnselected.Size = New System.Drawing.Size(144, 264) Me.lstUnselected.TabIndex = 8 " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(297, 281) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.lstSelected) Me.Controls.Add(Me.lstUnselected) Me.Name = "Form1" Me.Text = "DragBetweenListBoxes" Me.ResumeLayout(False) End Sub Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents lstSelected As System.Windows.Forms.ListBox Friend WithEvents lstUnselected As System.Windows.Forms.ListBox
End Class
</source>
ListBox.SelectedIndexChanged
<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
- 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
- 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.SelectedIndices
<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
- 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
- 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>
ListBox.SelectionMode
<source lang="vbnet"> Imports System Imports System.Drawing Imports System.Reflection Imports System.Windows.Forms public class MainClass
Shared Sub Main() Dim form1 As Form = New Form1 Application.Run(form1) End Sub
End Class
Public Class Form1
Inherits System.Windows.Forms.Form
- 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 Friend WithEvents listBox1 As System.Windows.Forms.ListBox Friend WithEvents chkMulti As System.Windows.Forms.CheckBox Friend WithEvents lblWindow As System.Windows.Forms.Label "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.chkMulti = New System.Windows.Forms.CheckBox() Me.lblWindow = New System.Windows.Forms.Label() Me.listBox1 = New System.Windows.Forms.ListBox() Me.SuspendLayout() " "chkMulti " Me.chkMulti.Location = New System.Drawing.Point(192, 64) Me.chkMulti.Name = "chkMulti" Me.chkMulti.Size = New System.Drawing.Size(80, 16) Me.chkMulti.TabIndex = 1 Me.chkMulti.Text = "MultiSelect" " "lblWindow " Me.lblWindow.Location = New System.Drawing.Point(32, 192) Me.lblWindow.Name = "lblWindow" Me.lblWindow.Size = New System.Drawing.Size(232, 16) Me.lblWindow.TabIndex = 2 " "listBox1 " Me.listBox1.Location = New System.Drawing.Point(24, 24) Me.listBox1.Name = "listBox1" Me.listBox1.Size = New System.Drawing.Size(144, 160) Me.listBox1.TabIndex = 0 " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 216) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblWindow, Me.chkMulti, Me.listBox1}) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub
- End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim x As Integer For x = 1 To 20 listBox1().Items.Add("Entry # " & CStr(x)) Next End Sub Private Sub chkMulti_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkMulti.CheckedChanged If chkMulti().CheckState = CheckState.Checked Then listBox1().SelectionMode = SelectionMode.MultiExtended Else listBox1().SelectionMode = SelectionMode.One End If End Sub
End Class
</source>
ListBox.SetSelected
<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
- 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
- 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.TopIndex
<source lang="vbnet"> Imports System Imports System.Windows.Forms
Public Class MainClass
Shared Sub Main() Dim form1 As Form = New Form1 Application.Run(form1)
End Sub
End Class Public Class Form1
Inherits System.Windows.Forms.Form
- 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 Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents txt As System.Windows.Forms.TextBox Friend WithEvents pic As System.Windows.Forms.PictureBox Friend WithEvents lstLog As System.Windows.Forms.ListBox Friend WithEvents cmd As System.Windows.Forms.Button Friend WithEvents Label3 As System.Windows.Forms.Label "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. Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox Friend WithEvents Label4 As System.Windows.Forms.Label <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.txt = New System.Windows.Forms.TextBox() Me.Label1 = New System.Windows.Forms.Label() Me.Label2 = New System.Windows.Forms.Label() Me.lstLog = New System.Windows.Forms.ListBox() Me.pic = New System.Windows.Forms.PictureBox() Me.cmd = New System.Windows.Forms.Button() Me.Label3 = New System.Windows.Forms.Label() Me.GroupBox1 = New System.Windows.Forms.GroupBox() Me.Label4 = New System.Windows.Forms.Label() Me.GroupBox1.SuspendLayout() Me.SuspendLayout() " "txt " Me.txt.Location = New System.Drawing.Point(156, 20) Me.txt.Name = "txt" Me.txt.Size = New System.Drawing.Size(192, 21) Me.txt.TabIndex = 1 Me.txt.Text = "" " "Label1 " Me.Label1.Location = New System.Drawing.Point(6, 24) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(144, 16) Me.Label1.TabIndex = 2 Me.Label1.Text = "Test keyboard events here:" " "Label2 " Me.Label2.Location = New System.Drawing.Point(20, 52) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(128, 16) Me.Label2.TabIndex = 2 Me.Label2.Text = "Test mouse events here:" " "lstLog " Me.lstLog.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _ Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right) Me.lstLog.IntegralHeight = False Me.lstLog.Location = New System.Drawing.Point(8, 160) Me.lstLog.Name = "lstLog" Me.lstLog.Size = New System.Drawing.Size(384, 212) Me.lstLog.TabIndex = 0 " "pic " Me.pic.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.pic.Location = New System.Drawing.Point(156, 48) Me.pic.Name = "pic" Me.pic.Size = New System.Drawing.Size(192, 48) Me.pic.TabIndex = 3 Me.pic.TabStop = False " "cmd " Me.cmd.FlatStyle = System.Windows.Forms.FlatStyle.System Me.cmd.Location = New System.Drawing.Point(156, 100) Me.cmd.Name = "cmd" Me.cmd.Size = New System.Drawing.Size(88, 28) Me.cmd.TabIndex = 4 Me.cmd.Text = "Button1" " "Label3 " Me.Label3.Location = New System.Drawing.Point(24, 104) Me.Label3.Name = "Label3" Me.Label3.Size = New System.Drawing.Size(64, 24) Me.Label3.TabIndex = 5 Me.Label3.Text = "Label3" " "GroupBox1 " Me.GroupBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right) Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label4, Me.Label1, Me.pic, Me.txt, Me.cmd, Me.Label2}) Me.GroupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System Me.GroupBox1.Location = New System.Drawing.Point(8, 4) Me.GroupBox1.Name = "GroupBox1" Me.GroupBox1.Size = New System.Drawing.Size(384, 148) Me.GroupBox1.TabIndex = 6 Me.GroupBox1.TabStop = False " "Label4 " Me.Label4.Location = New System.Drawing.Point(92, 108) Me.Label4.Name = "Label4" Me.Label4.Size = New System.Drawing.Size(56, 16) Me.Label4.TabIndex = 5 Me.Label4.Text = "And here:" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14) Me.ClientSize = New System.Drawing.Size(400, 378) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.GroupBox1, Me.Label3, Me.lstLog}) Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Name = "Form1" Me.Text = "Event Tracker" Me.GroupBox1.ResumeLayout(False) Me.ResumeLayout(False) End Sub
- End Region
Private Sub pic_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles pic.MouseEnter, cmd.MouseEnter Log("Mouse Enter") End Sub Private Sub pic_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pic.MouseMove log("Mouse Move: " ) End Sub Private Sub Log(ByVal data As String) lstLog.Items.Add(data) Dim ItemsPerPage As Integer = lstLog.Height \ lstLog.ItemHeight lstLog.TopIndex = lstLog.Items.Count - ItemsPerPage End Sub
End Class
</source>