VB.Net/GUI/CheckBoxList — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 19:40, 26 мая 2010
Содержание
Add Object to CheckedListBox: use ToString Method
<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 "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 lstEmployees As System.Windows.Forms.CheckedListBox <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.lstEmployees = New System.Windows.Forms.CheckedListBox() Me.SuspendLayout() " "lstEmployees " Me.lstEmployees.Location = New System.Drawing.Point(8, 12) Me.lstEmployees.Name = "lstEmployees" Me.lstEmployees.Size = New System.Drawing.Size(264, 196) Me.lstEmployees.TabIndex = 1 " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14) Me.ClientSize = New System.Drawing.Size(280, 218) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lstEmployees}) 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 = "List Control Example" Me.ResumeLayout(False) End Sub
- End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load lstEmployees.Items.Add(New Employee("A", "B", DateTime.Now)) lstEmployees.Items.Add(New Employee("C", "D", DateTime.Now)) lstEmployees.Items.Add(New Employee("E", "F", DateTime.Now)) End Sub
End Class Public Class Employee
Public FirstName As String Public LastName As String Public BirthDate As Date Public Sub New() End Sub Public Sub New(ByVal firstName, ByVal lastName, ByVal birthDate) Me.FirstName = firstName Me.LastName = lastName Me.BirthDate = birthDate End Sub Public Overrides Function ToString() As String Return FirstName & " " & LastName End Function
End Class
</source>
CheckBox List: add Object
<source lang="vbnet"> 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
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.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 " "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.clbStudents) Me.Name = "Form1" Me.Text = "UseCheckedListBox" Me.ResumeLayout(False) End Sub Friend WithEvents clbStudents As System.Windows.Forms.CheckedListBox
End Class
</source>
Check Box List: Get selected Items
<source lang="vbnet"> 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
</source>
Checked list boxes
<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>
Get selected item in a CheckBox List
<source lang="vbnet"> 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
</source>