VB.Net by API/System.Windows.Forms/CheckedListBox — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 16:40, 26 мая 2010
Содержание
- 1 CheckedListBox.BeginUpdate()
- 2 CheckedListBox.CheckedIndices
- 3 CheckedListBox.CheckedItemCollection
- 4 CheckedListBox.CheckedItems
- 5 CheckedListBox.CheckedListBox
- 6 CheckedListBox.DataSource
- 7 CheckedListBox.EndUpdate()
- 8 CheckedListBox.ItemCheck
- 9 CheckedListBox.Items.Add
- 10 CheckedListBox.SelectedItem
CheckedListBox.BeginUpdate()
Imports System.Windows.Forms
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 CheckedListBox1 As System.Windows.Forms.CheckedListBox
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.CheckedListBox1 = New System.Windows.Forms.CheckedListBox()
Me.Button1 = New System.Windows.Forms.Button()
Me.SuspendLayout()
"
"CheckedListBox1
"
Me.CheckedListBox1.Location = New System.Drawing.Point(24, 32)
Me.CheckedListBox1.Name = "CheckedListBox1"
Me.CheckedListBox1.Size = New System.Drawing.Size(360, 64)
Me.CheckedListBox1.TabIndex = 0
"
"Button1
"
Me.Button1.Location = New System.Drawing.Point(152, 120)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(96, 23)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Show Choice"
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(416, 165)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.CheckedListBox1})
Me.Name = "Form1"
Me.Text = "CheckListBoxDemo"
Me.ResumeLayout(False)
End Sub
#End Region
Public List() As String = {"A","B","C","D"}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CheckedListBox1.BeginUpdate()
CheckedListBox1.DataSource = List
CheckedListBox1.EndUpdate()
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If (CheckedListBox1.SelectedItem <> "") Then
Dim Entry As Object
For Each Entry In CheckedListBox1.CheckedItems
MessageBox.Show(Entry.ToString())
Next
Else
MessageBox.Show("You must select an item")
End If
End Sub
End Class
CheckedListBox.CheckedIndices
Imports System.Windows.Forms
public class CheckedListBoxAddItemGetSelected
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 btnAddItems As System.Windows.Forms.Button
Friend WithEvents clb1 As System.Windows.Forms.CheckedListBox
Friend WithEvents btnGetCheckedIndices As System.Windows.Forms.Button
Friend WithEvents btnGetCheckedItems As System.Windows.Forms.Button
Friend WithEvents ColorDialog1 As System.Windows.Forms.ColorDialog
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.btnAddItems = New System.Windows.Forms.Button()
Me.clb1 = New System.Windows.Forms.CheckedListBox()
Me.btnGetCheckedItems = New System.Windows.Forms.Button()
Me.btnGetCheckedIndices = New System.Windows.Forms.Button()
Me.ColorDialog1 = New System.Windows.Forms.ColorDialog()
Me.SuspendLayout()
"
"btnAddItems
"
Me.btnAddItems.BackColor = System.Drawing.SystemColors.ControlLight
Me.btnAddItems.Location = New System.Drawing.Point(176, 8)
Me.btnAddItems.Name = "btnAddItems"
Me.btnAddItems.Size = New System.Drawing.Size(104, 24)
Me.btnAddItems.TabIndex = 3
Me.btnAddItems.Text = "Add Items"
"
"clb1
"
Me.clb1.Location = New System.Drawing.Point(16, 8)
Me.clb1.Name = "clb1"
Me.clb1.Size = New System.Drawing.Size(144, 79)
Me.clb1.TabIndex = 4
"
"btnGetCheckedItems
"
Me.btnGetCheckedItems.BackColor = System.Drawing.SystemColors.ControlLight
Me.btnGetCheckedItems.Location = New System.Drawing.Point(176, 40)
Me.btnGetCheckedItems.Name = "btnGetCheckedItems"
Me.btnGetCheckedItems.Size = New System.Drawing.Size(104, 24)
Me.btnGetCheckedItems.TabIndex = 5
Me.btnGetCheckedItems.Text = "Checked by Item"
"
"btnGetCheckedIndices
"
Me.btnGetCheckedIndices.BackColor = System.Drawing.SystemColors.ControlLight
Me.btnGetCheckedIndices.Location = New System.Drawing.Point(176, 72)
Me.btnGetCheckedIndices.Name = "btnGetCheckedIndices"
Me.btnGetCheckedIndices.Size = New System.Drawing.Size(104, 24)
Me.btnGetCheckedIndices.TabIndex = 6
Me.btnGetCheckedIndices.Text = "Checked by Index"
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(296, 277)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnGetCheckedIndices, Me.btnGetCheckedItems, Me.clb1, Me.btnAddItems})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnAddItems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddItems.Click
clb1.Items.Add("A", True)
clb1.Items.Add("B", True)
clb1.Items.Add("C", True)
clb1.Items.Add("D", False)
End Sub
Private Sub btnGetCheckedItems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetCheckedItems.Click
Dim o As Object
For Each o In clb1.CheckedItems
Console.WriteLine(o.ToString)
Next o
End Sub
Private Sub btnGetCheckedIndices_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetCheckedIndices.Click
Dim o As Object
For Each o In clb1.CheckedIndices
Console.WriteLine(o)
Next o
End Sub
End Class
CheckedListBox.CheckedItemCollection
Imports System
Imports System.Data
Imports System.Windows.Forms
Imports System.Drawing
Public Class MainClass
Shared Sub Main()
Dim form1 As Form = New Form1
Application.Run(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
clbStudents.Items.Add(New Student("A", "A"), True)
clbStudents.Items.Add(New Student("B", "B"), False)
clbStudents.Items.Add(New Student("C", "C"), _
CheckState.Indeterminate)
End Sub
Private Sub btnListSelections_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListSelections.Click
Dim checked_items As CheckedListBox.CheckedItemCollection = clbStudents.CheckedItems
Dim txt As String = " Selected Students:" & vbCrLf
For Each item As Object In checked_items
txt &= item.ToString & vbCrLf
Next item
MessageBox.Show(txt)
End Sub
End Class
Public Class Student
Public FirstName As String
Public LastName As String
Public Sub New(ByVal first_name As String, ByVal last_name As String)
FirstName = first_name
LastName = last_name
End Sub
Public Overrides Function ToString() As String
Return FirstName & " " & LastName
End Function
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.clbStudents = New System.Windows.Forms.CheckedListBox
Me.btnListSelections = New System.Windows.Forms.Button
Me.SuspendLayout()
"
"clbStudents
"
Me.clbStudents.FormattingEnabled = True
Me.clbStudents.Location = New System.Drawing.Point(8, 8)
Me.clbStudents.Name = "clbStudents"
Me.clbStudents.Size = New System.Drawing.Size(160, 123)
Me.clbStudents.TabIndex = 0
"
"btnListSelections
"
Me.btnListSelections.Location = New System.Drawing.Point(8, 152)
Me.btnListSelections.Name = "btnListSelections"
Me.btnListSelections.Size = New System.Drawing.Size(160, 23)
Me.btnListSelections.TabIndex = 1
Me.btnListSelections.Text = "List Selections"
"
"Form1
"
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(180, 187)
Me.Controls.Add(Me.btnListSelections)
Me.Controls.Add(Me.clbStudents)
Me.Name = "Form1"
Me.Text = "UseCheckedListBox"
Me.ResumeLayout(False)
End Sub
Friend WithEvents clbStudents As System.Windows.Forms.CheckedListBox
Friend WithEvents btnListSelections As System.Windows.Forms.Button
End Class
CheckedListBox.CheckedItems
Imports System
Imports System.Data
Imports System.Windows.Forms
Imports System.Drawing
Public Class MainClass
Shared Sub Main()
Dim form1 As Form = New Form1
Application.Run(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
clbStudents.Items.Add(New Student("A", "A"), True)
clbStudents.Items.Add(New Student("B", "B"), False)
clbStudents.Items.Add(New Student("C", "C"), _
CheckState.Indeterminate)
End Sub
Private Sub btnListSelections_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListSelections.Click
Dim checked_items As CheckedListBox.CheckedItemCollection = clbStudents.CheckedItems
Dim txt As String = " Selected Students:" & vbCrLf
For Each item As Object In checked_items
txt &= item.ToString & vbCrLf
Next item
MessageBox.Show(txt)
End Sub
End Class
Public Class Student
Public FirstName As String
Public LastName As String
Public Sub New(ByVal first_name As String, ByVal last_name As String)
FirstName = first_name
LastName = last_name
End Sub
Public Overrides Function ToString() As String
Return FirstName & " " & LastName
End Function
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.clbStudents = New System.Windows.Forms.CheckedListBox
Me.btnListSelections = New System.Windows.Forms.Button
Me.SuspendLayout()
"
"clbStudents
"
Me.clbStudents.FormattingEnabled = True
Me.clbStudents.Location = New System.Drawing.Point(8, 8)
Me.clbStudents.Name = "clbStudents"
Me.clbStudents.Size = New System.Drawing.Size(160, 123)
Me.clbStudents.TabIndex = 0
"
"btnListSelections
"
Me.btnListSelections.Location = New System.Drawing.Point(8, 152)
Me.btnListSelections.Name = "btnListSelections"
Me.btnListSelections.Size = New System.Drawing.Size(160, 23)
Me.btnListSelections.TabIndex = 1
Me.btnListSelections.Text = "List Selections"
"
"Form1
"
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(180, 187)
Me.Controls.Add(Me.btnListSelections)
Me.Controls.Add(Me.clbStudents)
Me.Name = "Form1"
Me.Text = "UseCheckedListBox"
Me.ResumeLayout(False)
End Sub
Friend WithEvents clbStudents As System.Windows.Forms.CheckedListBox
Friend WithEvents btnListSelections As System.Windows.Forms.Button
End Class
CheckedListBox.CheckedListBox
Imports System.Windows.Forms
public class CheckedListBoxAddObjects
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.clbEmployees = New System.Windows.Forms.CheckedListBox
Me.btnListSelections = New System.Windows.Forms.Button
Me.SuspendLayout()
"
"clbEmployees
"
Me.clbEmployees.FormattingEnabled = True
Me.clbEmployees.Location = New System.Drawing.Point(8, 8)
Me.clbEmployees.Name = "clbEmployees"
Me.clbEmployees.Size = New System.Drawing.Size(160, 123)
Me.clbEmployees.TabIndex = 0
"
"btnListSelections
"
Me.btnListSelections.Location = New System.Drawing.Point(8, 152)
Me.btnListSelections.Name = "btnListSelections"
Me.btnListSelections.Size = New System.Drawing.Size(160, 23)
Me.btnListSelections.TabIndex = 1
Me.btnListSelections.Text = "List Selections"
"
"Form1
"
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(180, 187)
Me.Controls.Add(Me.btnListSelections)
Me.Controls.Add(Me.clbEmployees)
Me.Name = "Form1"
Me.Text = "UseCheckedListBox"
Me.ResumeLayout(False)
clbEmployees.Items.Add(New Employee("A", "A"), True)
clbEmployees.Items.Add(New Employee("B", "B"), False)
clbEmployees.Items.Add(New Employee("C", "C"), CheckState.Indeterminate)
End Sub
Private Sub btnListSelections_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListSelections.Click
Dim checked_items As CheckedListBox.CheckedItemCollection = clbEmployees.CheckedItems
Console.WriteLine("Selected Employees:")
For Each item As Object In checked_items
Console.WriteLine(item.ToString)
Next item
End Sub
Friend WithEvents clbEmployees As System.Windows.Forms.CheckedListBox
Friend WithEvents btnListSelections As System.Windows.Forms.Button
End Class
Public Class Employee
Public FirstName As String
Public LastName As String
Public Sub New(ByVal first_name As String, ByVal last_name As String)
FirstName = first_name
LastName = last_name
End Sub
Public Overrides Function ToString() As String
Return FirstName & " " & LastName
End Function
End Class
CheckedListBox.DataSource
Imports System.Windows.Forms
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 CheckedListBox1 As System.Windows.Forms.CheckedListBox
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.CheckedListBox1 = New System.Windows.Forms.CheckedListBox()
Me.Button1 = New System.Windows.Forms.Button()
Me.SuspendLayout()
"
"CheckedListBox1
"
Me.CheckedListBox1.Location = New System.Drawing.Point(24, 32)
Me.CheckedListBox1.Name = "CheckedListBox1"
Me.CheckedListBox1.Size = New System.Drawing.Size(360, 64)
Me.CheckedListBox1.TabIndex = 0
"
"Button1
"
Me.Button1.Location = New System.Drawing.Point(152, 120)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(96, 23)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Show Choice"
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(416, 165)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.CheckedListBox1})
Me.Name = "Form1"
Me.Text = "CheckListBoxDemo"
Me.ResumeLayout(False)
End Sub
#End Region
Public List() As String = {"A","B","C","D"}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CheckedListBox1.BeginUpdate()
CheckedListBox1.DataSource = List
CheckedListBox1.EndUpdate()
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If (CheckedListBox1.SelectedItem <> "") Then
Dim Entry As Object
For Each Entry In CheckedListBox1.CheckedItems
MessageBox.Show(Entry.ToString())
Next
Else
MessageBox.Show("You must select an item")
End If
End Sub
End Class
CheckedListBox.EndUpdate()
Imports System.Windows.Forms
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 CheckedListBox1 As System.Windows.Forms.CheckedListBox
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.CheckedListBox1 = New System.Windows.Forms.CheckedListBox()
Me.Button1 = New System.Windows.Forms.Button()
Me.SuspendLayout()
"
"CheckedListBox1
"
Me.CheckedListBox1.Location = New System.Drawing.Point(24, 32)
Me.CheckedListBox1.Name = "CheckedListBox1"
Me.CheckedListBox1.Size = New System.Drawing.Size(360, 64)
Me.CheckedListBox1.TabIndex = 0
"
"Button1
"
Me.Button1.Location = New System.Drawing.Point(152, 120)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(96, 23)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Show Choice"
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(416, 165)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.CheckedListBox1})
Me.Name = "Form1"
Me.Text = "CheckListBoxDemo"
Me.ResumeLayout(False)
End Sub
#End Region
Public List() As String = {"A","B","C","D"}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CheckedListBox1.BeginUpdate()
CheckedListBox1.DataSource = List
CheckedListBox1.EndUpdate()
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If (CheckedListBox1.SelectedItem <> "") Then
Dim Entry As Object
For Each Entry In CheckedListBox1.CheckedItems
MessageBox.Show(Entry.ToString())
Next
Else
MessageBox.Show("You must select an item")
End If
End Sub
End Class
CheckedListBox.ItemCheck
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
CheckedListBox.Items.Add
Imports System.Windows.Forms
public class CheckedListBoxAddObjects
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.clbEmployees = New System.Windows.Forms.CheckedListBox
Me.btnListSelections = New System.Windows.Forms.Button
Me.SuspendLayout()
"
"clbEmployees
"
Me.clbEmployees.FormattingEnabled = True
Me.clbEmployees.Location = New System.Drawing.Point(8, 8)
Me.clbEmployees.Name = "clbEmployees"
Me.clbEmployees.Size = New System.Drawing.Size(160, 123)
Me.clbEmployees.TabIndex = 0
"
"btnListSelections
"
Me.btnListSelections.Location = New System.Drawing.Point(8, 152)
Me.btnListSelections.Name = "btnListSelections"
Me.btnListSelections.Size = New System.Drawing.Size(160, 23)
Me.btnListSelections.TabIndex = 1
Me.btnListSelections.Text = "List Selections"
"
"Form1
"
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(180, 187)
Me.Controls.Add(Me.btnListSelections)
Me.Controls.Add(Me.clbEmployees)
Me.Name = "Form1"
Me.Text = "UseCheckedListBox"
Me.ResumeLayout(False)
clbEmployees.Items.Add(New Employee("A", "A"), True)
clbEmployees.Items.Add(New Employee("B", "B"), False)
clbEmployees.Items.Add(New Employee("C", "C"), CheckState.Indeterminate)
End Sub
Private Sub btnListSelections_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListSelections.Click
Dim checked_items As CheckedListBox.CheckedItemCollection = clbEmployees.CheckedItems
Console.WriteLine("Selected Employees:")
For Each item As Object In checked_items
Console.WriteLine(item.ToString)
Next item
End Sub
Friend WithEvents clbEmployees As System.Windows.Forms.CheckedListBox
Friend WithEvents btnListSelections As System.Windows.Forms.Button
End Class
Public Class Employee
Public FirstName As String
Public LastName As String
Public Sub New(ByVal first_name As String, ByVal last_name As String)
FirstName = first_name
LastName = last_name
End Sub
Public Overrides Function ToString() As String
Return FirstName & " " & LastName
End Function
End Class
CheckedListBox.SelectedItem
Imports System.Windows.Forms
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 CheckedListBox1 As System.Windows.Forms.CheckedListBox
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.CheckedListBox1 = New System.Windows.Forms.CheckedListBox()
Me.Button1 = New System.Windows.Forms.Button()
Me.SuspendLayout()
"
"CheckedListBox1
"
Me.CheckedListBox1.Location = New System.Drawing.Point(24, 32)
Me.CheckedListBox1.Name = "CheckedListBox1"
Me.CheckedListBox1.Size = New System.Drawing.Size(360, 64)
Me.CheckedListBox1.TabIndex = 0
"
"Button1
"
Me.Button1.Location = New System.Drawing.Point(152, 120)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(96, 23)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Show Choice"
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(416, 165)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.CheckedListBox1})
Me.Name = "Form1"
Me.Text = "CheckListBoxDemo"
Me.ResumeLayout(False)
End Sub
#End Region
Public List() As String = {"A","B","C","D"}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CheckedListBox1.BeginUpdate()
CheckedListBox1.DataSource = List
CheckedListBox1.EndUpdate()
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If (CheckedListBox1.SelectedItem <> "") Then
Dim Entry As Object
For Each Entry In CheckedListBox1.CheckedItems
MessageBox.Show(Entry.ToString())
Next
Else
MessageBox.Show("You must select an item")
End If
End Sub
End Class