Skip to content

Commit b5ec1a0

Browse files
CopilotBillWagnergewarren
authored
Add concrete ConvertEventHandler example to AddHandler statement documentation (#48279)
* Initial plan * Add concrete ConvertEventHandler example to AddHandler documentation Co-authored-by: BillWagner <[email protected]> * Refine AddHandler ConvertEventHandler example - reduce comments and improve style Co-authored-by: BillWagner <[email protected]> * Move Imports statements to top of file and add System.Data reference Co-authored-by: BillWagner <[email protected]> * Apply suggestions from code review Co-authored-by: Genevieve Warren <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]> Co-authored-by: Bill Wagner <[email protected]> Co-authored-by: Genevieve Warren <[email protected]>
1 parent 4c64e70 commit b5ec1a0

File tree

3 files changed

+64
-13
lines changed

3 files changed

+64
-13
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 example 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+
- 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: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
Class Class072b9cf6629846f1849e4edc1631564c
1+
Imports System.Windows.Forms
2+
Imports System.Data
3+
4+
Class Class072b9cf6629846f1849e4edc1631564c
25
' WithEvents and the Handles Clause
36

47
' <snippet1>
@@ -225,35 +228,72 @@ Class Class647cd825e8774910b4f18d168beebe6a
225228
' AddHandler Statement
226229

227230
' <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()
229277
Dim Obj As New Class1
230-
' Associate an event handler with an event.
231278
AddHandler Obj.Ev_Event, AddressOf EventHandler
232-
' Call the method to raise the event.
233279
Obj.CauseSomeEvent()
234-
' Stop handling events.
235280
RemoveHandler Obj.Ev_Event, AddressOf EventHandler
236-
' This event will not be handled.
237281
Obj.CauseSomeEvent()
238-
' Associate an event handler with an event, using a lambda.
239-
' This handler cannot be removed.
282+
283+
' Lambda expression example
240284
AddHandler Obj.Ev_Event, Sub ()
241285
MsgBox("Lambda caught event.")
242286
End Sub
243-
' This event will be handled by the lambda above.
244287
Obj.CauseSomeEvent()
245288
End Sub
246289

247290
Sub EventHandler()
248-
' Handle the event.
249291
MsgBox("EventHandler caught event.")
250292
End Sub
251293

252294
Public Class Class1
253-
' Declare an event.
254295
Public Event Ev_Event()
255296
Sub CauseSomeEvent()
256-
' Raise an event.
257297
RaiseEvent Ev_Event()
258298
End Sub
259299
End Class

samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/VbVbalrEvents.vbproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
</PropertyGroup>
4242
<ItemGroup>
4343
<Reference Include="System" />
44+
<Reference Include="System.Data" />
4445
<Reference Include="System.Deployment" />
4546
<Reference Include="System.Drawing" />
4647
<Reference Include="System.Windows.Forms" />
@@ -50,6 +51,7 @@
5051
<Import Include="System" />
5152
<Import Include="System.Collections" />
5253
<Import Include="System.Collections.Generic" />
54+
<Import Include="System.Data" />
5355
<Import Include="System.Drawing" />
5456
<Import Include="System.Diagnostics" />
5557
<Import Include="System.Windows.Forms" />

0 commit comments

Comments
 (0)