PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Handlers for same controls and events-VBForums
Results 1 to 11 of 11

Thread: [RESOLVED] Handlers for same controls and events

  1. #1

    Thread Starter
    Addicted Member t3cho's Avatar
    Join Date
    Mar 2014
    Posts
    227

    Resolved [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 .

    Name:  Untitled.jpg
Views: 107
Size:  17.8 KB

    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.

  2. #2
    Hyperactive Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    467

    Re: Handlers for same controls and events

    Hint: sender as Object
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,750

    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

  4. #4

    Thread Starter
    Addicted Member t3cho's Avatar
    Join Date
    Mar 2014
    Posts
    227

    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 ?

  5. #5
    Lively Member
    Join Date
    Jul 2017
    Location
    Exeter, UK
    Posts
    66

    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?

  6. #6

    Thread Starter
    Addicted Member t3cho's Avatar
    Join Date
    Mar 2014
    Posts
    227

    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

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,750

    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

  8. #8
    Fanatic Member
    Join Date
    Sep 2017
    Posts
    681

    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

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,617

    Re: Handlers for same controls and events

    Quote Originally Posted by t3cho View Post
    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,617

    Re: Handlers for same controls and events

    Quote Originally Posted by kpmc View Post
    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:
    1. For Each Ctrl As Control In Controls
    2.     If TypeOf Ctrl Is CheckBox Then
    3.         Dim ChkBox As CheckBox = DirectCast(Ctrl, CheckBox)
    can be replaced with this:
    vb.net Code:
    1. For Each ChkBox In Controls.OfType(Of CheckBox)()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    Fanatic Member
    Join Date
    Sep 2017
    Posts
    681

    Re: Handlers for same controls and events

    Quote Originally Posted by jmcilhinney View Post
    If you were going to go that way, this:
    vb.net Code:
    1. For Each Ctrl As Control In Controls
    2.     If TypeOf Ctrl Is CheckBox Then
    3.         Dim ChkBox As CheckBox = DirectCast(Ctrl, CheckBox)
    can be replaced with this:
    vb.net Code:
    1. 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
  •  



Featured


Click Here to Expand Forum to Full Width