VB.Net/GUI/ListBox — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:42, 26 мая 2010
Содержание
- 1 Add Item to a List Box by clicking a Button
- 2 Add Item to ListBox
- 3 Add, remove and clear list box items
- 4 Customize ListBox Font and Item Height
- 5 Drag and drop between two ListBox
- 6 Draw selected Item in ListBox
- 7 Enum Binding
- 8 Get Selected Item from ListBox
- 9 Owner Draw ListBox
- 10 Set ListBox TopIndex
- 11 Toggling list box Selection Mode: select one or select more than one
Add Item to a List Box by clicking a Button
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
Add Item to ListBox
Imports System
Imports System.Drawing
Imports System.Data
Imports System.IO
Imports System.Collections
Imports System.Windows.Forms
Imports System.Xml
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 lstEmails As System.Windows.Forms.ListBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.lstEmails = New System.Windows.Forms.ListBox()
Me.SuspendLayout()
"
"lstEmails
"
Me.lstEmails.IntegralHeight = False
Me.lstEmails.Location = New System.Drawing.Point(16, 16)
Me.lstEmails.Name = "lstEmails"
Me.lstEmails.Size = New System.Drawing.Size(264, 224)
Me.lstEmails.TabIndex = 0
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 253)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lstEmails})
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
lstEmails.Items.Add("a")
End Sub
End Class
Add, remove and clear list box items
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class MainClass
Shared Sub Main()
Dim myform As Form = New FrmListBox()
Application.Run(myform)
End Sub " Main
End Class
Public Class FrmListBox
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
" contains user-input list of elements
Friend WithEvents lstDisplay As ListBox
" user input textbox
Friend WithEvents txtInput As TextBox
" add, remove, clear and exit command buttons
Friend WithEvents cmdAdd As Button
Friend WithEvents cmdRemove As Button
Friend WithEvents cmdClear As Button
"Required by the Windows Form Designer
Private components As System.ruponentModel.Container
"NOTE: The following procedure is required by the Windows Form Designer
"It can be modified using the Windows Form Designer.
"Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.cmdClear = New System.Windows.Forms.Button()
Me.cmdAdd = New System.Windows.Forms.Button()
Me.txtInput = New System.Windows.Forms.TextBox()
Me.cmdRemove = New System.Windows.Forms.Button()
Me.lstDisplay = New System.Windows.Forms.ListBox()
Me.SuspendLayout()
"
"cmdClear
"
Me.cmdClear.Location = New System.Drawing.Point(160, 144)
Me.cmdClear.Name = "cmdClear"
Me.cmdClear.Size = New System.Drawing.Size(104, 40)
Me.cmdClear.TabIndex = 4
Me.cmdClear.Text = "Clear"
"
"cmdAdd
"
Me.cmdAdd.Location = New System.Drawing.Point(160, 48)
Me.cmdAdd.Name = "cmdAdd"
Me.cmdAdd.Size = New System.Drawing.Size(104, 40)
Me.cmdAdd.TabIndex = 2
Me.cmdAdd.Text = "Add"
"
"txtInput
"
Me.txtInput.Location = New System.Drawing.Point(160, 8)
Me.txtInput.Name = "txtInput"
Me.txtInput.Size = New System.Drawing.Size(104, 20)
Me.txtInput.TabIndex = 1
Me.txtInput.Text = ""
"
"cmdRemove
"
Me.cmdRemove.Location = New System.Drawing.Point(160, 96)
Me.cmdRemove.Name = "cmdRemove"
Me.cmdRemove.Size = New System.Drawing.Size(104, 40)
Me.cmdRemove.TabIndex = 3
Me.cmdRemove.Text = "Remove"
"
"lstDisplay
"
Me.lstDisplay.Location = New System.Drawing.Point(16, 8)
Me.lstDisplay.Name = "lstDisplay"
Me.lstDisplay.Size = New System.Drawing.Size(120, 238)
Me.lstDisplay.TabIndex = 0
"
"FrmListBox
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cmdClear, Me.cmdRemove, Me.cmdAdd, Me.txtInput, Me.lstDisplay})
Me.Name = "FrmListBox"
Me.Text = "ListBoxTest"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub cmdAdd_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdAdd.Click
lstDisplay.Items.Add(txtInput.Text)
txtInput.Text = ""
End Sub
Private Sub cmdRemove_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdRemove.Click
If lstDisplay.SelectedIndex <> -1 Then
lstDisplay.Items.RemoveAt(lstDisplay.SelectedIndex)
End If
End Sub
Private Sub cmdClear_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdClear.Click
lstDisplay.Items.Clear()
End Sub " cmdClear_Click
End Class
Customize ListBox Font and Item Height
Imports System
Imports System.ruponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Configuration
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
Public Class MainClass
Shared Sub Main()
Dim myform As Form = New OwnerDrawnVariableListBox()
Application.Run(myform)
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", "B", "C"})
Me.listBox2.Location = New System.Drawing.Point(6, 37)
Me.listBox2.Name = "listBox2"
Me.listBox2.Size = New System.Drawing.Size(280, 192)
Me.listBox2.TabIndex = 5
"
"OwnerDrawnVariableListBox
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.listBox2})
Me.Name = "OwnerDrawnVariableListBox"
Me.Text = "OwnerDrawnVariableListBox"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub listBox2_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles listBox2.DrawItem
e.DrawBackground()
Dim textFont As Font
If e.Index Mod 2 = 0 Then
textFont = New Font(e.Font.FontFamily, e.Font.Size * 2)
Else
textFont = e.Font
End If
e.Graphics.DrawString(listBox2.Items(e.Index).ToString(), textFont, New SolidBrush(e.ForeColor), RectangleF.op_Implicit(e.Bounds))
e.DrawFocusRectangle()
End Sub
Private Sub listBox2_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles listBox2.MeasureItem
If e.Index Mod 2 = 0 Then
e.ItemHeight = e.ItemHeight * 2
End If
End Sub
End Class
Drag and drop between two ListBox
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)
" Select the item at this point.
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
" Highlight the item under the mouse.
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
Draw selected Item in ListBox
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
Enum Binding
Imports System.Windows.Forms
Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button()
Me.ListBox1 = New System.Windows.Forms.ListBox()
Me.Button2 = New System.Windows.Forms.Button()
Me.SuspendLayout()
"
Me.Button1.Location = New System.Drawing.Point(104, 24)
Me.Button1.Size = New System.Drawing.Size(184, 24)
Me.Button1.Text = "Bind to Enum"
"
Me.ListBox1.Location = New System.Drawing.Point(104, 112)
Me.ListBox1.Size = New System.Drawing.Size(184, 82)
"
Me.Button2.Location = New System.Drawing.Point(104, 64)
Me.Button2.Size = New System.Drawing.Size(184, 24)
Me.Button2.Text = "Display Value of Selected"
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(392, 246)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button2, Me.ListBox1, Me.Button1})
Me.ResumeLayout(False)
End Sub
Public Enum Test
A = 1
B = 2
C = 3
End Enum
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.DataSource = System.Enum.GetNames(GetType(Test))
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim strNames As Array = System.Enum.GetValues(GetType(Test))
Dim strValue As String = strNames(ListBox1.SelectedIndex)
MessageBox.Show(strValue)
End Sub
End Class
Get Selected Item from ListBox
Imports System.Windows.Forms
Imports System.Drawing
Imports System.IO
Module Module1
Sub Main()
Application.Run(New Form1)
End Sub
End Module
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 Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ListBox1 = New System.Windows.Forms.ListBox()
Me.Button1 = New System.Windows.Forms.Button()
Me.SuspendLayout()
"
"ListBox1
"
Me.ListBox1.Items.AddRange(New Object() {"Windows", "Linux", "Mac OS", "Palm OS", "Windows CE", "Unix"})
Me.ListBox1.Location = New System.Drawing.Point(32, 32)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(120, 69)
Me.ListBox1.TabIndex = 0
"
"Button1
"
Me.Button1.Location = New System.Drawing.Point(104, 144)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 1
Me.Button1.Text = "Done"
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(280, 205)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.ListBox1})
Me.Name = "Form1"
Me.Text = "ListBoxDemo"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If (ListBox1.SelectedItem <> "") Then
MessageBox.Show("You selected: " & ListBox1.SelectedItem)
Else
MessageBox.Show("You must select an item")
End If
End Sub
End Class
Owner Draw ListBox
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
Set ListBox TopIndex
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
Toggling list box Selection Mode: select one or select more than one
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