Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15539

VS 2019 [RESOLVED] Adding a parameter to the ColumnChanged event

$
0
0
I have several forms that are bound to DataTables. I add delegates for handling the ColumnChanged and RowChanged events.

Code:

AddHandler dsMain.Tables("Fuel").ColumnChanged, AddressOf Column_Changed
AddHandler dsMain.Tables("Fuel").RowChanged, AddressOf Row_Changed

I use these delegates for some housekeeping tasks of the form. For example, in the Column_Changed method I enable/disable the "Save" and "Cancel" buttons of the form's BindingNavigator and change the "ModDate" value of the DataRow.

Code:

bnMainCancel.Enabled = True
bnMainSave.Enabled = True

tbl = CType(sender, DataTable)

If tbl.Columns("ModDate") IsNot Nothing AndAlso e.Column.ColumnName <> "ModDate" Then
    '...this fires the bound DataTable's ColumnChanged event that will
    '  in turn call the Column_Changed procedure (again)
    e.Row("ModDate") = Date.Now
End If

This approach has worked well for many years. The downside is that every form in my project has to have its own Column_Changed method (lots of code duplication and additional maintenance).

I would like to only have a single Column_Changed method in a code module that is outside of my forms (modUtilities.Column_Changed) but to make this work, I need to pass the calling form's Form object (myForm) to the modUtilities.Column_Changed method. The modUtilities.Column_Changed code would then use this object.

Code:

Select Case myForm.Name
    Case "Fuel"
        CType(myForm, frmFuel).bnMainCancel.Enabled = True
        CType(myForm, frmFuel).bnMainSave.Enabled = True
End Select

tbl = CType(sender, DataTable)

If tbl.Columns("ModDate") IsNot Nothing AndAlso e.Column.ColumnName <> "ModDate" Then
    '...this fires the bound DataTable's ColumnChanged event that will
    '  in turn call the Column_Changed procedure (again)
    e.Row("ModDate") = Date.Now
End If

My research on this issue has found a couple of ways to use Lambda expressions or even the Tag property to pass additional parameters to an event.

https://stackoverflow.com/questions/...with-parameter
https://stackoverflow.com/questions/...in-vb-net-2008
https://stackoverflow.com/questions/...nloaddatacompl

I'm not familiar with Lambda expressions and the DataTable object does not have a Tag property, so I need some more guidance.

Any advice on this question will be much appreciated!

Viewing all articles
Browse latest Browse all 15539

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>