|
1 |
| -Class Class072b9cf6629846f1849e4edc1631564c |
| 1 | +Imports System.Windows.Forms |
| 2 | +Imports System.Data |
| 3 | + |
| 4 | +Class Class072b9cf6629846f1849e4edc1631564c |
2 | 5 | ' WithEvents and the Handles Clause
|
3 | 6 |
|
4 | 7 | ' <snippet1>
|
@@ -225,35 +228,72 @@ Class Class647cd825e8774910b4f18d168beebe6a
|
225 | 228 | ' AddHandler Statement
|
226 | 229 |
|
227 | 230 | ' <snippet17>
|
228 |
| - Sub TestEvents() |
| 231 | + Public Class DataBindingExample |
| 232 | + Private textBox1 As TextBox |
| 233 | + Private ds As DataSet |
| 234 | + |
| 235 | + Public Sub New() |
| 236 | + textBox1 = New TextBox() |
| 237 | + ds = New DataSet() |
| 238 | + SetupSampleData() |
| 239 | + BindControlWithAddHandler() |
| 240 | + End Sub |
| 241 | + |
| 242 | + Private Sub SetupSampleData() |
| 243 | + Dim table As New DataTable("Orders") |
| 244 | + table.Columns.Add("OrderAmount", GetType(Decimal)) |
| 245 | + table.Rows.Add(123.45D) |
| 246 | + table.Rows.Add(67.89D) |
| 247 | + ds.Tables.Add(table) |
| 248 | + End Sub |
| 249 | + |
| 250 | + Private Sub BindControlWithAddHandler() |
| 251 | + Dim binding As New Binding("Text", ds, "Orders.OrderAmount") |
| 252 | + |
| 253 | + ' Use AddHandler to associate ConvertEventHandler delegates |
| 254 | + AddHandler binding.Format, AddressOf DecimalToCurrency |
| 255 | + AddHandler binding.Parse, AddressOf CurrencyToDecimal |
| 256 | + |
| 257 | + textBox1.DataBindings.Add(binding) |
| 258 | + End Sub |
| 259 | + |
| 260 | + Private Sub DecimalToCurrency(ByVal sender As Object, ByVal e As ConvertEventArgs) |
| 261 | + If e.DesiredType IsNot GetType(String) Then |
| 262 | + Return |
| 263 | + End If |
| 264 | + e.Value = CDec(e.Value).ToString("c") |
| 265 | + End Sub |
| 266 | + |
| 267 | + Private Sub CurrencyToDecimal(ByVal sender As Object, ByVal e As ConvertEventArgs) |
| 268 | + If e.DesiredType IsNot GetType(Decimal) Then |
| 269 | + Return |
| 270 | + End If |
| 271 | + e.Value = Convert.ToDecimal(e.Value.ToString()) |
| 272 | + End Sub |
| 273 | + End Class |
| 274 | + |
| 275 | + ' Simple example for basic AddHandler usage |
| 276 | + Sub TestBasicEvents() |
229 | 277 | Dim Obj As New Class1
|
230 |
| - ' Associate an event handler with an event. |
231 | 278 | AddHandler Obj.Ev_Event, AddressOf EventHandler
|
232 |
| - ' Call the method to raise the event. |
233 | 279 | Obj.CauseSomeEvent()
|
234 |
| - ' Stop handling events. |
235 | 280 | RemoveHandler Obj.Ev_Event, AddressOf EventHandler
|
236 |
| - ' This event will not be handled. |
237 | 281 | Obj.CauseSomeEvent()
|
238 |
| - ' Associate an event handler with an event, using a lambda. |
239 |
| - ' This handler cannot be removed. |
| 282 | + |
| 283 | + ' Lambda expression example |
240 | 284 | AddHandler Obj.Ev_Event, Sub ()
|
241 | 285 | MsgBox("Lambda caught event.")
|
242 | 286 | End Sub
|
243 |
| - ' This event will be handled by the lambda above. |
244 | 287 | Obj.CauseSomeEvent()
|
245 | 288 | End Sub
|
246 | 289 |
|
247 | 290 | Sub EventHandler()
|
248 |
| - ' Handle the event. |
249 | 291 | MsgBox("EventHandler caught event.")
|
250 | 292 | End Sub
|
251 | 293 |
|
252 | 294 | Public Class Class1
|
253 |
| - ' Declare an event. |
254 | 295 | Public Event Ev_Event()
|
255 | 296 | Sub CauseSomeEvent()
|
256 |
| - ' Raise an event. |
257 | 297 | RaiseEvent Ev_Event()
|
258 | 298 | End Sub
|
259 | 299 | End Class
|
|
0 commit comments