-
Mar 7th, 2018, 10:50 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Handlers for same controls and events
Im pretty sure we can make this code looks well but i dont know how.
On form i have 7 checkboxes .
Each of those checkboxes on checked change is calling the same event.
Code:
Private Sub Ebay_Mo_CheckedChanged(sender As Object, e As EventArgs) Handles Ebay_Mo.CheckedChanged
If bLoadEbay = True Then
Save_Ebay()
End If
End Sub
Private Sub Ebay_Tu_CheckedChanged(sender As Object, e As EventArgs) Handles Ebay_Tu.CheckedChanged
If bLoadEbay = True Then
Save_Ebay()
End If
End Sub
Private Sub Ebay_We_CheckedChanged(sender As Object, e As EventArgs) Handles Ebay_We.CheckedChanged
If bLoadEbay = True Then
Save_Ebay()
End If
End Sub
Private Sub Ebay_Th_CheckedChanged(sender As Object, e As EventArgs) Handles Ebay_Th.CheckedChanged
If bLoadEbay = True Then
Save_Ebay()
End If
End Sub
Private Sub Ebay_Fr_CheckedChanged(sender As Object, e As EventArgs) Handles Ebay_Fr.CheckedChanged
If bLoadEbay = True Then
Save_Ebay()
End If
End Sub
Private Sub Ebay_Sa_CheckedChanged(sender As Object, e As EventArgs) Handles Ebay_Sa.CheckedChanged
If bLoadEbay = True Then
Save_Ebay()
End If
End Sub
Private Sub Ebay_Su_CheckedChanged(sender As Object, e As EventArgs) Handles Ebay_Su.CheckedChanged
If bLoadEbay = True Then
Save_Ebay()
End If
End Sub
Private Sub Ebay_At_ValueChanged(sender As Object, e As EventArgs) Handles Ebay_At.ValueChanged
If bLoadEbay = True Then
Save_Ebay()
End If
End Sub
I tried with adding handlers like
HTML Code:
Private Sub Ebay_Sa_CheckedChanged(sender As Object, e As EventArgs) Handles Ebay_Sa.CheckedChanged, Ebay_Mo.CheckedChanged.....
If bLoadEbay = True Then
Save_Ebay()
End If
End Sub
Assuming on each form i have 7 check boxes it generates a lot of unnecessary code.
-
Mar 7th, 2018, 10:58 AM
#2
Re: Handlers for same controls and events
-
Mar 7th, 2018, 11:03 AM
#3
Re: Handlers for same controls and events
So, you are chaining the event handelrs, the sender IS the control that raised the event, what is left? Is something not working?
My usual boring signature: Nothing
-
Mar 7th, 2018, 11:20 AM
#4
Thread Starter
Addicted Member
Re: Handlers for same controls and events
Thanks for fast replies there is no errors here. Everything works as a charm but do i really need to use this much code in this case. Is there simpler solution ?
-
Mar 7th, 2018, 11:32 AM
#5
Addicted Member
Re: Handlers for same controls and events
I can't see the point of your code here, you are testing a Boolean bLoadEbay for true, regardless of the actual checked status of the checkbox controls, which you could do as suggested in post #3, seems a bit pointless to me?
-
Mar 7th, 2018, 11:42 AM
#6
Thread Starter
Addicted Member
Re: Handlers for same controls and events
bLoadEbay means that form is previously loaded and on next checked value change it need to save the current state to ini file.
Something like
bLoadEbay = False
Form load event
fill previously states of checked boxes
bLoadEbay = True
-
Mar 7th, 2018, 12:12 PM
#7
Re: Handlers for same controls and events
Are you talking about the code of all those Handles things? If so, there is a quicker way to add them all on there from the designer, I just have used it so rarely that I'm not sure that I have it right. I believe you select all the controls in the designer that you want to add the handler for, then above the Properties window is a little lightning bolt for event handlers. On that, select the event you want them to handle and the Handles clause gets chained together automatically. That reduces the typing, but it still looks kind of ugly. If you are concerned of the aesthetics of the code, the only other option would be to use AddHandler in a loop to add the handlers dynamically on form load (or in the constructor). That would look cleaner, has no real performance implications, but would take a bit more typing.
My usual boring signature: Nothing
-
Mar 7th, 2018, 01:17 PM
#8
Re: Handlers for same controls and events
Code:
Private Sub FormCtrls_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each Ctrl As Control In Controls
If TypeOf Ctrl Is CheckBox Then
Dim ChkBox As CheckBox = DirectCast(Ctrl, CheckBox)
AddHandler ChkBox.CheckedChanged, AddressOf dostuff
End If
Next
End Sub
Public Sub dostuff(sender As Object, e As EventArgs)
'TODO: Stuff
End Sub
-
Mar 7th, 2018, 10:08 PM
#9
Re: Handlers for same controls and events
Originally Posted by t3cho
I tried with adding handlers like
HTML Code:
Private Sub Ebay_Sa_CheckedChanged(sender As Object, e As EventArgs) Handles Ebay_Sa.CheckedChanged, Ebay_Mo.CheckedChanged.....
If bLoadEbay = True Then
Save_Ebay()
End If
End Sub
Assuming on each form i have 7 check boxes it generates a lot of unnecessary code.
I'm not quite sure what the issue is. Is it a problem to have multiple items in the Handles clause?
-
Mar 7th, 2018, 10:10 PM
#10
Re: Handlers for same controls and events
Originally Posted by kpmc
Code:
Private Sub FormCtrls_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each Ctrl As Control In Controls
If TypeOf Ctrl Is CheckBox Then
Dim ChkBox As CheckBox = DirectCast(Ctrl, CheckBox)
AddHandler ChkBox.CheckedChanged, AddressOf dostuff
End If
Next
End Sub
Public Sub dostuff(sender As Object, e As EventArgs)
'TODO: Stuff
End Sub
If you were going to go that way, this:
vb.net Code:
For Each Ctrl As Control In Controls If TypeOf Ctrl Is CheckBox Then Dim ChkBox As CheckBox = DirectCast(Ctrl, CheckBox)
can be replaced with this:
vb.net Code:
For Each ChkBox In Controls.OfType(Of CheckBox)()
-
Mar 7th, 2018, 11:07 PM
#11
Re: Handlers for same controls and events
Originally Posted by jmcilhinney
If you were going to go that way, this:
vb.net Code:
For Each Ctrl As Control In Controls If TypeOf Ctrl Is CheckBox Then Dim ChkBox As CheckBox = DirectCast(Ctrl, CheckBox)
can be replaced with this:
vb.net Code:
For Each ChkBox In Controls.OfType(Of CheckBox)()
Thanks for the tip
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|