@@ -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