Loop through controls checkbox and add to dictionary

  • Thread starter Thread starter Hugh Self Taught
  • Start date Start date
H

Hugh Self Taught

Guest
Hi Gurus,

With the features of VB.Net I'm progressing but get still get stuck. I'm trying to make a routine whereby I test for changed data in a form so user can be prompted to save, ignore or cancel. I have the following bits of code so far but the checkbox & datetimepicker are driving me nuts!

Private frmTxtboxDictionary As New Dictionary(Of TextBox, String)
Private frmChkboxDictionary As New Dictionary(Of CheckBox, CheckState)
Private frmCboboxDictionary As New Dictionary(Of ComboBox, String)
Private frmDatePickDictionary As New Dictionary(Of DateTimePicker, String)

Then after form data is populated

For Each ctl As Control In Controls.OfType(Of TextBox)()
frmTxtboxDictionary.Add(ctl, ctl.Text)
Next
For Each ctl As Control In Controls.OfType(Of ComboBox)()
frmCboboxDictionary.Add(ctl, ctl.Text)
Next
For Each ctl As Control In Controls.OfType(Of CheckBox)()
frmChkboxDictionary.Add(ctl, ctl.Text)
Next
For Each ctl As Control In Controls.OfType(Of DateTimePicker)()
frmDatePickDictionary.Add(ctl, ctl.Text)
Next

Then on closing event or move to another record, the following

For Each chkBox In Controls.OfType(Of CheckBox)()
'If chkBox.Checked Then
Dim oldValue = (From kp As KeyValuePair(Of CheckBox, CheckState) In frmChkboxDictionary
Where kp.Key Is chkBox
Select kp.Value).First()
If oldValue.ToString() <> chkBox.Text Then
IsDirty = True
End If
'End If
Next

So my dilemma is adding the correct values for checkbox & datetimepicker to the dictionary. Please point me in the right direction as the other 2 seem to work. The error I get for the checkbox is conversion from string "name of checkbox" to type integer is not valid

Continue reading...
 
Back
Top