source: trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/ListBox.ab@ 611

Last change on this file since 611 was 611, checked in by イグトランス (egtra), 16 years ago

ListBox.Itemsを実装。サンプルも修正。

File size: 4.8 KB
Line 
1'Classes/ActiveBasic/Windows/UI/ListBox.ab
2#require <Classes/ActiveBasic/Windows/UI/Control.ab>
3
4Namespace ActiveBasic
5Namespace Windows
6Namespace UI
7
8/*!
9@brief リストボックスコントロールのクラス
10@date 2008/07/21
11@auther Egtra
12*/
13Class ListBox
14 Inherits Control
15Public
16 Sub ListBox()
17 items = New Detail.ListBoxItemList(This)
18 End Sub
19
20 Function Items() As System.Collections.Generic.IList<String>
21 Items = items
22 End Function
23
24 Sub SetSelected(i As Long, select As Boolean)
25 Dim now = SendMessage(LB_GETSEL, i As WPARAM, 0)
26 lrCheck(now, i, "ListBox.SetSelected")
27 If (now > 0) <> (select As Long <> 0) Then
28 '現状とselectが異なればLB_SETCURSELで反転させる。
29 lrCheck(SendMessage(LB_SETCURSEL, i As WPARAM, 0), i, "ListBox.SetSelected")
30 End If
31 End Sub
32Protected
33 Override Sub GetCreateStruct(ByRef cs As CREATESTRUCT)
34 With cs
35 .lpszClass = "LISTBOX"
36 End With
37 End Sub
38
39Private
40 items As Detail.ListBoxItemList
41
42 Sub lrCheck(lr As LRESULT, i As Long, s As String)
43 Detail.LRCheck(lr, i, items.Count, s)
44 End Sub
45
46 Sub lrCheck(lr As LRESULT, s As String)
47 Detail.LRCheck(lr, s)
48 End Sub
49End Class
50
51Namespace Detail
52
53Class ListBoxItemList
54 Implements System.Collections.Generic.IList<String>
55Public
56 Sub ListBoxItemList(l As ListBox)
57 base = l
58 End Sub
59
60 Function GetEnumerator() As System.Collections.Generic.IEnumerator<String>
61 End Function
62
63 Function Count() As Long
64 Count = base.SendMessage(LB_GETCOUNT) As Long
65 lrCheck(Count, "ListBox.Item.Count")
66 End Function
67
68 Function IsReadOnly() As Boolean
69 Return False
70 End Function
71
72 Sub Add(s As String)
73 lrCheck(base.SendMessage(LB_ADDSTRING, 0, ToTCStr(s) As LPARAM), "ListBox.Item.Add")
74 End Sub
75
76 Sub Clear()
77 Do : Loop Until removeAtImpl(0) = LB_ERR
78 End Sub
79
80 Function Remove(s As String) As Boolean
81 Dim i = IndexOf(s)
82 If i = -1 Then
83 Remove = False
84 Else
85 removeAtImpl(i)
86 Remove = True
87 End If
88 End Function
89
90 Sub Operator [](index As Long, item As String)
91 Item[index] = item
92 End Sub
93
94 Function Operator [](index As Long) As String
95 Return Item[index]
96 End Function
97
98 Virtual Sub Item(i As Long, s As String)
99 lrCheck(removeAtImpl(i), i, "ListBox.Item[] (set)")
100 lrCheck(base.SendMessage(LB_INSERTSTRING, 0, ToTCStr(s) As LPARAM), i, "ListBox.Item[] (set)")
101 End Sub
102
103 Virtual Function Item(i As Long) As String
104 Dim len = base.SendMessage(LB_GETTEXTLEN, i As WPARAM, 0)
105 lrCheck(len, i, "ListBox.Item[] (get)")
106 Dim sb = New System.Text.StringBuilder(len)
107 sb.Length = len
108 len = base.SendMessage(LB_GETTEXT, i As WPARAM, StrPtr(sb) As LPARAM)
109 lrCheck(len, i, "ListBox.Item[] (get)")
110 sb.Length = len
111 Item = sb.ToString
112 End Function
113
114 Virtual Function IndexOf(s As String) As Long
115 '見つからなかったときに返るLB_ERRはちょうど-1。
116 IndexOf = base.SendMessage(LB_FINDSTRINGEXACT, -1 As WPARAM, ToTCStr(s) As LPARAM) As Long
117 End Function
118
119 Virtual Sub Insert(i As Long, s As String)
120 Dim lr = base.SendMessage(LB_INSERTSTRING, 0, ToTCStr(s) As LPARAM)
121 lrCheck(lr, i, "ListBox.Item.Insert")
122 End Sub
123
124 Virtual Sub RemoveAt(i As Long)
125 Dim lr = removeAtImpl(i)
126 lrCheck(lr, i, "ListBox.Item.RemoveAt")
127 End Sub
128Private
129 Function removeAtImpl(i As Long) As LRESULT
130 removeAtImpl = base.SendMessage(LB_DELETESTRING, i As WPARAM, 0)
131 End Function
132
133 Sub lrCheck(lr As LRESULT, s As String)
134 LRCheck(lr, s)
135 End Sub
136
137 Sub lrCheck(lr As LRESULT, i As Long, s As String)
138 LRCheck(lr, i, Count, s)
139 End Sub
140
141 base As ListBox
142End Class
143
144/*!
145@brief List Boxからの戻り値がエラー値の場合に例外を投げる。
146@param[in] lr SendMessageなどの戻り値
147@param[in] s 例外を投げるときに添加するメッセージ
148@throw SystemException lrがエラー値のとき
149@date 2008/08/21
150@auther Egtra
151*/
152Sub LRCheck(lr As LRESULT, s As String)
153 If lr = LB_ERR Then
154 Throw New System.SystemException(s & " (LB_ERR)")
155 ElseIf lr = LB_ERRSPACE Then
156 Throw New System.SystemException(s & " (LB_ERRSPACE)")
157 End If
158End Sub
159
160/*!
161@brief List Boxからの戻り値がエラー値の場合に例外を投げる。
162@param[in] lr SendMessageなどの戻り値
163@param[in] i インデックス
164@param[in] max インデックスの許容される最大値
165@param[in] s 例外を投げるときに添加するメッセージ
166@throw SystemException lrがエラー値のとき
167@throw OutOfRangeException lrがLB_ERRで、なおかつiが範囲外のとき
168@date 2008/08/21
169@auther Egtra
170こちらは、iが範囲外かならOutOfRangeExceptionを投げるのが特徴
171*/
172Sub LRCheck(lr As LRESULT, i As Long, max As Long, s As String)
173 If lr = LB_ERR Then
174 If i < 0 Or i >= max Then
175 Throw New System.ArgumentOutOfRangeException(s)
176 Else
177 Throw New System.SystemException(s & " (LB_ERR)")
178 End If
179 ElseIf lr = LB_ERRSPACE Then
180 Throw New System.SystemException(s & " (LB_ERRSPACE)")
181 End If
182End Sub
183
184End Namespace
185
186End Namespace 'UI
187End Namespace 'Widnows
188End Namespace 'ActiveBasic
Note: See TracBrowser for help on using the repository browser.