Skip to content

Commit 057b0ba

Browse files
CopilotBillWagner
andcommitted
Add concrete ConvertEventHandler example to AddHandler documentation
Co-authored-by: BillWagner <[email protected]>
1 parent e1b5be6 commit 057b0ba

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

docs/visual-basic/language-reference/statements/addhandler-statement.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,16 @@ The parts `AddressOf eventhandler` and `expression` are mutually exclusive.
4545
4646
## Example
4747

48-
[!code-vb[VbVbalrEvents#17](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb#17)]
48+
The following example demonstrates how to use `AddHandler` with `ConvertEventHandler` delegates for data binding scenarios. This shows a practical use case where event handlers are attached to `Format` and `Parse` events of a `Binding` object to convert between decimal values and currency strings.
49+
50+
[!code-vb[VbVbalrEvents#17](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb#17)]
51+
52+
This example shows:
53+
54+
- Creating a `Binding` object for data binding
55+
- Using `AddHandler` to attach `ConvertEventHandler` delegates to the `Format` and `Parse` events
56+
- Implementing event handler methods that convert between decimal and currency string formats
57+
- A simpler example demonstrating basic `AddHandler` usage with custom events and lambda expressions
4958

5059
## See also
5160

samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,69 @@ Class Class647cd825e8774910b4f18d168beebe6a
225225
' AddHandler Statement
226226

227227
' <snippet17>
228-
Sub TestEvents()
228+
Imports System.Windows.Forms
229+
Imports System.Data
230+
231+
Public Class DataBindingExample
232+
Private textBox1 As TextBox
233+
Private ds As DataSet
234+
235+
Public Sub New()
236+
' Initialize controls and data
237+
textBox1 = New TextBox()
238+
ds = New DataSet()
239+
240+
' Setup sample data
241+
SetupSampleData()
242+
243+
' Demonstrate AddHandler with ConvertEventHandler
244+
BindControlWithAddHandler()
245+
End Sub
246+
247+
Private Sub SetupSampleData()
248+
' Create a sample DataTable with decimal values
249+
Dim table As New DataTable("Orders")
250+
table.Columns.Add("OrderAmount", GetType(Decimal))
251+
table.Rows.Add(123.45D)
252+
table.Rows.Add(67.89D)
253+
ds.Tables.Add(table)
254+
End Sub
255+
256+
Private Sub BindControlWithAddHandler()
257+
' Create a binding for the TextBox to the OrderAmount column
258+
Dim binding As New Binding("Text", ds, "Orders.OrderAmount")
259+
260+
' Use AddHandler to associate ConvertEventHandler delegates with events
261+
AddHandler binding.Format, AddressOf DecimalToCurrency
262+
AddHandler binding.Parse, AddressOf CurrencyToDecimal
263+
264+
' Add the binding to the TextBox
265+
textBox1.DataBindings.Add(binding)
266+
End Sub
267+
268+
Private Sub DecimalToCurrency(ByVal sender As Object, ByVal e As ConvertEventArgs)
269+
' Convert decimal value to currency string format
270+
If e.DesiredType IsNot GetType(String) Then
271+
Return
272+
End If
273+
274+
' Format the decimal value as currency
275+
e.Value = CDec(e.Value).ToString("c")
276+
End Sub
277+
278+
Private Sub CurrencyToDecimal(ByVal sender As Object, ByVal e As ConvertEventArgs)
279+
' Convert currency string back to decimal value
280+
If e.DesiredType IsNot GetType(Decimal) Then
281+
Return
282+
End If
283+
284+
' Parse the currency string back to decimal
285+
e.Value = Convert.ToDecimal(e.Value.ToString())
286+
End Sub
287+
End Class
288+
289+
' Simple example for basic AddHandler usage
290+
Sub TestBasicEvents()
229291
Dim Obj As New Class1
230292
' Associate an event handler with an event.
231293
AddHandler Obj.Ev_Event, AddressOf EventHandler

0 commit comments

Comments
 (0)