VB.Net Tutorial/GUI/CheckedListBox — различия между версиями

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

Текущая версия на 15:57, 26 мая 2010

Add value to Checked List Box

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

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

End class Public Class Form1

   Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
   Public Sub New()
       MyBase.New()
       "This call is required by the Windows Form Designer.
       InitializeComponent()
       "Add any initialization after the InitializeComponent() call
   End Sub
   "Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing Then
           If Not (components Is Nothing) Then
               components.Dispose()
           End If
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   Friend WithEvents CheckedListBox1 As System.Windows.Forms.CheckedListBox
   Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.CheckedListBox1 = New System.Windows.Forms.CheckedListBox
       Me.TextBox1 = New System.Windows.Forms.TextBox
       Me.SuspendLayout()
       "
       "CheckedListBox1
       "
       Me.CheckedListBox1.CheckOnClick = True
       Me.CheckedListBox1.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
       Me.CheckedListBox1.Location = New System.Drawing.Point(96, 6)
       Me.CheckedListBox1.Name = "CheckedListBox1"
       Me.CheckedListBox1.Size = New System.Drawing.Size(120, 150)
       Me.CheckedListBox1.TabIndex = 0
       "
       "TextBox1
       "
       Me.TextBox1.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
       Me.TextBox1.Location = New System.Drawing.Point(16, 184)
       Me.TextBox1.Name = "TextBox1"
       Me.TextBox1.Size = New System.Drawing.Size(296, 20)
       Me.TextBox1.TabIndex = 2
       Me.TextBox1.Text = "TextBox1"
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(15, 37)
       Me.ClientSize = New System.Drawing.Size(336, 229)
       Me.Controls.Add(Me.TextBox1)
       Me.Controls.Add(Me.CheckedListBox1)
       Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 24.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
       Me.Name = "Form1"
       Me.Text = "Form1"
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       CheckedListBox1.Items.Add("Item 1", True)
       CheckedListBox1.Items.Add("Item 2", True)
       CheckedListBox1.Items.Add("Item 3", True)
       CheckedListBox1.Items.Add("Item 4", False)
       CheckedListBox1.Items.Add("Item 5", True)
       CheckedListBox1.Items.Add("Item 6", True)
       CheckedListBox1.Items.Add("Item 7", True)
       CheckedListBox1.Items.Add("Item 8", False)
       CheckedListBox1.Items.Add("Item 9", True)
       CheckedListBox1.Items.Add("Item 10", True)
       CheckedListBox1.Items.Add("Item 11", True)
   End Sub
   Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
       Select Case e.NewValue
           Case CheckState.Checked
               TextBox1.Text = "You checked Item " & e.Index
           Case CheckState.Unchecked
               TextBox1.Text = "You unchecked Item " & e.Index
       End Select
   End Sub

End Class</source>

CheckedListBox: Add Items, Get Selected Items and Get Selected Indices

<source lang="vbnet">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
  1. Region " Windows Form Designer generated code "
   Public Sub New()
       MyBase.New()
       "This call is required by the Windows Form Designer.
       InitializeComponent()
       "Add any initialization after the InitializeComponent() call
   End Sub
   "Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing Then
           If Not (components Is Nothing) Then
               components.Dispose()
           End If
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   Friend WithEvents 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
  1. 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</source>

Checked List Box selection event

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

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

End class Public Class Form1

   Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
   Public Sub New()
       MyBase.New()
       "This call is required by the Windows Form Designer.
       InitializeComponent()
       "Add any initialization after the InitializeComponent() call
   End Sub
   "Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing Then
           If Not (components Is Nothing) Then
               components.Dispose()
           End If
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   Friend WithEvents CheckedListBox1 As System.Windows.Forms.CheckedListBox
   Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.CheckedListBox1 = New System.Windows.Forms.CheckedListBox
       Me.TextBox1 = New System.Windows.Forms.TextBox
       Me.SuspendLayout()
       "
       "CheckedListBox1
       "
       Me.CheckedListBox1.CheckOnClick = True
       Me.CheckedListBox1.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
       Me.CheckedListBox1.Location = New System.Drawing.Point(96, 6)
       Me.CheckedListBox1.Name = "CheckedListBox1"
       Me.CheckedListBox1.Size = New System.Drawing.Size(120, 150)
       Me.CheckedListBox1.TabIndex = 0
       "
       "TextBox1
       "
       Me.TextBox1.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
       Me.TextBox1.Location = New System.Drawing.Point(16, 184)
       Me.TextBox1.Name = "TextBox1"
       Me.TextBox1.Size = New System.Drawing.Size(296, 20)
       Me.TextBox1.TabIndex = 2
       Me.TextBox1.Text = "TextBox1"
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(15, 37)
       Me.ClientSize = New System.Drawing.Size(336, 229)
       Me.Controls.Add(Me.TextBox1)
       Me.Controls.Add(Me.CheckedListBox1)
       Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 24.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
       Me.Name = "Form1"
       Me.Text = "Form1"
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       CheckedListBox1.Items.Add("Item 1", True)
       CheckedListBox1.Items.Add("Item 2", True)
       CheckedListBox1.Items.Add("Item 3", True)
       CheckedListBox1.Items.Add("Item 4", False)
       CheckedListBox1.Items.Add("Item 5", True)
       CheckedListBox1.Items.Add("Item 6", True)
       CheckedListBox1.Items.Add("Item 7", True)
       CheckedListBox1.Items.Add("Item 8", False)
       CheckedListBox1.Items.Add("Item 9", True)
       CheckedListBox1.Items.Add("Item 10", True)
       CheckedListBox1.Items.Add("Item 11", True)
   End Sub
   Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
       Select Case e.NewValue
           Case CheckState.Checked
               TextBox1.Text = "You checked Item " & e.Index
           Case CheckState.Unchecked
               TextBox1.Text = "You unchecked Item " & e.Index
       End Select
   End Sub

End Class</source>

Using the checked list boxes to add items to a list box

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

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

End class Public Class FrmCheckedListBox

  Inherits Form
  1. Region " Windows Form Designer generated code "
  Public Sub New()
     MyBase.New()
     "This call is required by the Windows Form Designer.
     InitializeComponent()
     "Add any initialization after the InitializeComponent() call
  End Sub
  "Form overrides dispose to clean up the component list.
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
     If disposing Then
        If Not (components Is Nothing) Then
           components.Dispose()
        End If
     End If
     MyBase.Dispose(disposing)
  End Sub
  " 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() {"A", "B", "C", "D", "E", "F", "G", "H"})
     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
  1. 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>