Results 1 to 5 of 5

Thread: [RESOLVED] Handling components

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2018
    Posts
    20

    Resolved [RESOLVED] Handling components

    Hi,
    Something that I've never to the bottom of - don't know if it's possible, but would be grateful for the nearest method if not please. The pseudo-code below will hopefully illustrate what I would ideally like to do, but, in brief, my application has many buttons, each pointing to a file. I would like to make a lot of the handling around the buttons slicker by referring to the buttons themselves, instead of iterating through arrays all the time. Changing to dictionaries would be good too. I'll use a simple display-change as an example of what I'm trying to get at e.g. Dictionary keys = red, green, blue; Dictionary values = Button1, Button2 ... Button49 etc:

    For each value in colorDictionary.keys.red
    value.backcolor=red
    Next

    Or more generally, is it possible to do something like:

    GoToThisSub(Button9, "Red")
    ...
    Sub GoToThisSub(butt, col)
    butt.backcolor=col
    End Sub

    Thanks a lot,
    Steve

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Handling components

    It's really not clear what you're asking. Are you saying that you want to click a Button and then do something to that Button, or that you want to be able to do something to a number of Buttons at the same time based on some criteria, or something else? If you're saying that you want to be able to use a single method to handle a Click event for multiple Buttons then that's simple. You just add all Buttons to the Handles clause and then access the one that was clicked via the 'sender' parameter, e.g.
    vb.net Code:
    1. Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button1.Click,
    2.                                                                     Button2.Click,
    3.                                                                     Button3.Click
    4.     Dim btn = DirectCast(sender, Button)
    5.  
    6.     'Use btn here.
    7. End Sub
    You can easily create such an event handler by selecting all the Buttons in the designer, opening the Properties window, clicking the Events button and then double-clicking the Click event. You can also use the Properties window to select an existing event handler for an event. Another option is to add the handlers in code:
    vb.net Code:
    1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    2.     Array.ForEach(Controls.OfType(Of Button)().ToArray(),
    3.                   Sub(btn) AddHandler btn.Click, AddressOf Buttons_Click)
    4. End Sub
    5.  
    6. Private Sub Buttons_Click(sender As Object, e As EventArgs)
    7.     Dim btn = DirectCast(sender, Button)
    8.  
    9.     'Use btn here.
    10. End Sub
    11.  
    12. Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
    13.     Array.ForEach(Controls.OfType(Of Button)().ToArray(),
    14.                   Sub(btn) RemoveHandler btn.Click, AddressOf Buttons_Click)
    15. End Sub
    If you're actually saying that you want to affect a filtered list of Buttons then you can use a LINQ query to do the filtering. The example above already uses LINQ and you can add a Where clause to filter, e.g.
    vb.net Code:
    1. 'Loop through all the Buttons that have a BackColor of Red.
    2. For Each btn In Controls.OfType(Of Button)().Where(Function(b) b.BackColor = Color.Red)
    3.     'Use btn here.
    4. Next

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2018
    Posts
    20

    Re: Handling components

    Hi jmcilhinney,

    Actually, my vagueness has paid dividends! All three of those are new to me (not a pro!) and really useful. The multiple handler was the simple option I was sort of looking for, but I never found that method. However, the middle option will be put to great use - I can see where I would use the filtering option too.

    Very many thanks - have a good day,

    Steve

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2018
    Posts
    20

    Red face Re: Handling components

    Hi jmcilhinney,

    Actually, my vagueness has paid dividends! All three of those are new to me (not a pro!) and really useful. The multiple handler was the simple option I was sort of looking for, but I never found that method. However, the middle option will be put to great use - I can see where I would use the filtering option too.

    Very many thanks - have a good day,

    Steve

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Handling components

    Always happy to answer three questions in one, even if two weren't asked. By the way, don't forget to use the Thread Tools menu to mark this thread Resolved if your issue has been resolved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width