This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.vb
200 lines (187 loc) · 7.12 KB
/
Form1.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Imports DevExpress.XtraEditors
Namespace WindowsApplication11
''' <summary>
''' Summary description for Form1.
''' </summary>
Public Class Form1
Inherits System.Windows.Forms.Form
Private WithEvents checkedListBoxControl1 As DevExpress.XtraEditors.CheckedListBoxControl
Private dataSet1 As System.Data.DataSet
Private dataTable1 As System.Data.DataTable
Private dataColumn1 As System.Data.DataColumn
Private dataColumn2 As System.Data.DataColumn
Private dataColumn3 As System.Data.DataColumn
Private WithEvents simpleButton1 As DevExpress.XtraEditors.SimpleButton
Private WithEvents simpleButton2 As DevExpress.XtraEditors.SimpleButton
''' <summary>
''' Required designer variable.
''' </summary>
Private components As System.ComponentModel.Container = Nothing
Public Sub New()
'
' Required for Windows Form Designer support
'
InitializeComponent()
'
' TODO: Add any constructor code after InitializeComponent call
'
End Sub
''' <summary>
''' Clean up any resources being used.
''' </summary>
Protected Overrides Overloads Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If components IsNot Nothing Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
#Region "Windows Form Designer generated code"
''' <summary>
''' Required method for Designer support - do not modify
''' the contents of this method with the code editor.
''' </summary>
Private Sub InitializeComponent()
Me.checkedListBoxControl1 = New DevExpress.XtraEditors.CheckedListBoxControl()
Me.dataTable1 = New System.Data.DataTable()
Me.dataColumn1 = New System.Data.DataColumn()
Me.dataColumn2 = New System.Data.DataColumn()
Me.dataColumn3 = New System.Data.DataColumn()
Me.dataSet1 = New System.Data.DataSet()
Me.simpleButton1 = New DevExpress.XtraEditors.SimpleButton()
Me.simpleButton2 = New DevExpress.XtraEditors.SimpleButton()
CType(Me.checkedListBoxControl1, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.dataTable1, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.dataSet1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
' checkedListBoxControl1
'
Me.checkedListBoxControl1.CheckOnClick = True
Me.checkedListBoxControl1.DataSource = Me.dataTable1
Me.checkedListBoxControl1.DisplayMember = "Name"
Me.checkedListBoxControl1.Location = New System.Drawing.Point(7, 7)
Me.checkedListBoxControl1.Name = "checkedListBoxControl1"
Me.checkedListBoxControl1.Size = New System.Drawing.Size(200, 166)
Me.checkedListBoxControl1.TabIndex = 0
Me.checkedListBoxControl1.ValueMember = "ID"
' Me.checkedListBoxControl1.ItemCheck += New DevExpress.XtraEditors.Controls.ItemCheckEventHandler(Me.checkedListBoxControl1_ItemCheck);
'
' dataTable1
'
Me.dataTable1.Columns.AddRange(New System.Data.DataColumn() { Me.dataColumn1, Me.dataColumn2, Me.dataColumn3})
Me.dataTable1.Constraints.AddRange(New System.Data.Constraint() { New System.Data.UniqueConstraint("Constraint1", New String() { "ID"}, True)})
Me.dataTable1.PrimaryKey = New System.Data.DataColumn() { Me.dataColumn1}
Me.dataTable1.TableName = "Table1"
'
' dataColumn1
'
Me.dataColumn1.AllowDBNull = False
Me.dataColumn1.ColumnName = "ID"
Me.dataColumn1.DataType = GetType(System.Guid)
'
' dataColumn2
'
Me.dataColumn2.ColumnName = "Name"
'
' dataColumn3
'
Me.dataColumn3.ColumnName = "Checked"
Me.dataColumn3.DataType = GetType(Boolean)
'
' dataSet1
'
Me.dataSet1.DataSetName = "NewDataSet"
Me.dataSet1.Locale = New System.Globalization.CultureInfo("en-US")
Me.dataSet1.Tables.AddRange(New System.Data.DataTable() { Me.dataTable1})
'
' simpleButton1
'
Me.simpleButton1.Location = New System.Drawing.Point(7, 187)
Me.simpleButton1.Name = "simpleButton1"
Me.simpleButton1.Size = New System.Drawing.Size(200, 20)
Me.simpleButton1.TabIndex = 1
Me.simpleButton1.Text = "Get checked rows"
' Me.simpleButton1.Click += New System.EventHandler(Me.simpleButton1_Click);
'
' simpleButton2
'
Me.simpleButton2.Location = New System.Drawing.Point(8, 216)
Me.simpleButton2.Name = "simpleButton2"
Me.simpleButton2.Size = New System.Drawing.Size(200, 20)
Me.simpleButton2.TabIndex = 2
Me.simpleButton2.Text = "Toggle 1st row check"
' Me.simpleButton2.Click += New System.EventHandler(Me.simpleButton2_Click);
'
' Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(219, 250)
Me.Controls.Add(Me.simpleButton2)
Me.Controls.Add(Me.simpleButton1)
Me.Controls.Add(Me.checkedListBoxControl1)
Me.Name = "Form1"
Me.Text = "How to get the checked rows of a data-bound CheckedListBoxControl"
' Me.Load += New System.EventHandler(Me.Form1_Load);
CType(Me.checkedListBoxControl1, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.dataTable1, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.dataSet1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
''' <summary>
''' The main entry point for the application.
''' </summary>
<STAThread> _
Shared Sub Main()
Application.Run(New Form1())
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To 4
dataTable1.Rows.Add(New Object() {Guid.NewGuid(), "Item " & i.ToString(), i Mod 2 = 0})
Next i
FillCheckedItems()
End Sub
Private Sub simpleButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles simpleButton1.Click
Dim result As String = ""
For Each item As Object In checkedListBoxControl1.CheckedItems
Dim row As DataRowView = TryCast(item, DataRowView)
result &= String.Format("{0}" & Constants.vbTab & "{1}" & Constants.vbTab & "{2}" & Constants.vbCrLf, row("ID"), row("Name"), row("Checked"))
Next item
MessageBox.Show(result)
End Sub
Private lockItemCheck As Boolean = False
Private Sub FillCheckedItems()
lockItemCheck = True
Try
For i As Integer = 0 To dataTable1.DefaultView.Count - 1
Dim row As DataRowView = dataTable1.DefaultView(i)
If True.Equals(row("Checked")) Then
checkedListBoxControl1.SetItemChecked(i, True)
End If
Next i
Finally
lockItemCheck = False
End Try
End Sub
Private Sub checkedListBoxControl1_ItemCheck(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ItemCheckEventArgs) Handles checkedListBoxControl1.ItemCheck
If lockItemCheck Then
Return
End If
Dim row As DataRowView = dataTable1.DefaultView(e.Index)
row("Checked") = e.State = CheckState.Checked
End Sub
Private Sub simpleButton2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles simpleButton2.Click
Dim val As Boolean = checkedListBoxControl1.GetItemChecked(0)
checkedListBoxControl1.SetItemChecked(0, (Not val))
End Sub
End Class
End Namespace