none
How do I make button.PerformClick() work after I have hidden the button with button.Visible = False in Visual Basic 2013 RRS feed

  • Question

  • I have a form where I can view images side by side from two different folders and these images are presented in two separate ‘frames’ on the form.  I have controls (buttons on the form) that allow me to scroll through each image folder.  I also set up to be able to ‘sync’ the movement of scrolling and use only one set of controls.  I then wanted to make the first set of controls invisible to the user.  However I find that the button.PerformClick() no longer works after I set the button.Visible property to false.

    How do I get the Click handler to respond to the commands in the code?

    Here is some of my code:

    This code moves the images in Group 0 by one when clicked

        Private Sub BtnForwrdImgGrp0_Click(sender As Object, e As EventArgs) Handles btnForwrdImgGrp0.Click 'Used to move one Image later (next image) in the Group 0 Series
            If imgDispPnt(viewFldr0Pntr) + 3 < imgNamesCnt(viewFldr0Pntr) Then 'Move to the next image
                imgDispPnt(viewFldr0Pntr) = imgDispPnt(viewFldr0Pntr) + 1
                lblImgDel01.Text = lblImgDel02.Text
                lblImgDel02.Text = lblImgDel03.Text
                lblImgDel03.Text = Nothing
                Chk4Grp0Del()
                LoadCoverViewForm()
            End If
        End Sub

    This code moves the images in Group 1 by one when clicked and when the sets are synced (as indicated by the linkSetsToCompareIsSet flag) it will call the btnForwrdImgGrp0 button handler.

        Private Sub BtnForwrdImgGrp1_Click(sender As Object, e As EventArgs) Handles btnForwrdImgGrp1.Click 'Used to move one Image later (next image) in the Group 1 Series
            If imgDispPnt(viewFldr1Pntr) + 3 < imgNamesCnt(viewFldr1Pntr) Then 'Move to the next image
                imgDispPnt(viewFldr1Pntr) = imgDispPnt(viewFldr1Pntr) + 1
                lblImgDel11.Text = lblImgDel12.Text
                lblImgDel12.Text = lblImgDel13.Text
                lblImgDel13.Text = Nothing
                If linkSetsToCompareIsSet = True Then
                    btnForwrdImgGrp0.PerformClick()
                End If
                Chk4Grp1Del()
                LoadCoverViewForm()
            End If
        End Sub

    This is the code that sets the syncing function and makes the button controls for Group 0 visible or not visible.

        Private Sub btnLinkSetsToCompare_Click(sender As Object, e As EventArgs) Handles btnLinkSetsToCompare.Click
            'TODO: 129: V1.5.3.1.y  This links the two sets for comparisons.
            '    TD129:  Manually set the relative locations where you want to start the comparisons then click the button.
            '    Toggling the button will turn the syncing on and off
            '    Only display this button if the "Freeze This Set" button is active Else blank out the button 
            '    When button is active: 
            '      1. Set it to a different color 
            '      2. Blank out the Next set and Last set buttons
            '      3. Sync both sets to the actions of First, Last, Forward and Back buttons
            '
    
            If linkSetsToCompareIsSet = True Then 'Toggle the linkSetsToCompareIsSet flag off and return the button to original color
                btnLinkSetsToCompare.BackColor = DefaultBackColor  'Set to orignal color
                btnLinkSetsToCompare.UseVisualStyleBackColor = True  'Set button to original style
                linkSetsToCompareIsSet = False
                ActivateGroup0Controls()
            Else
                btnLinkSetsToCompare.BackColor = Color.ForestGreen ' Make the button ugly
                linkSetsToCompareIsSet = True
                BlankOutGroup0Controls()
            End If
    
        End Sub
    
        Private Sub BlankOutGroup0Controls()  'Blank out the move controls in Group 0 and the Previous and Next buttons  TD129
            btnPrevSet.Visible = False
            btnNextSet.Visible = False
            btnBackImgGrp0.Visible = False
            btnBackImgGrp0x2.Visible = False
            btnForwrdImgGrp0.Visible = False
            btnForwrdImgGrp0x2.Visible = False
            btnFrstImgGrp0.Visible = False
            btnLastImgGrp0.Visible = False
            btnRfrshGrp1.Visible = False
    
        End Sub
    
        Private Sub ActivateGroup0Controls()  'Activate the move controls in Group 0 and the Previous and Next buttons  TD129
            btnPrevSet.Visible = True
            btnNextSet.Visible = True
            btnBackImgGrp0.Visible = True
            btnBackImgGrp0x2.Visible = True
            btnForwrdImgGrp0.Visible = True
            btnForwrdImgGrp0x2.Visible = True
            btnFrstImgGrp0.Visible = True
            btnLastImgGrp0.Visible = True
            btnRfrshGrp1.Visible = True
    
        End Sub
    


    Can anyone tell me how to make the code button.PerformClick() work when that button is not Visible?

    Friday, June 15, 2018 6:32 PM

Answers

  • Check this too (instead of PerformClick):

       btnForwrdImgGrp0_Click(Nothing, Nothing)


    • Marked as answer by TJBlues Friday, June 15, 2018 10:03 PM
    Friday, June 15, 2018 7:50 PM

All replies

  • In short, take code out of the button click event, place into a procedure. Now call the procedure in the click event.

    Now in code even if the button is not visible you can can the appropriate procedure.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, June 15, 2018 6:38 PM
    Moderator
  • Yup I thought of that one but it would make me refactor about 700 lines of code.  I happened to show the simplest example. So I am still hoping for a simple fix maybe some other property instead of button.Visible that would make the Group 0 buttons inactive but still allow the button.PerformClick() to work.
    Friday, June 15, 2018 6:47 PM
  • Hi

    HUH! why on earth would you need to do that?

    If you follow the advice that Karen provided, you would only need to change all the button.PerformClick() items to the new 'ButtonPerformClickSub' or whatever you chose to call it.

    Unless you mean you have about 700 calls to button.PerformClick()!


    Regards Les, Livingston, Scotland

    Friday, June 15, 2018 7:18 PM
  • Yup I thought of that one but it would make me refactor about 700 lines of code.  I happened to show the simplest example. So I am still hoping for a simple fix maybe some other property instead of button.Visible that would make the Group 0 buttons inactive but still allow the button.PerformClick() to work.

    It's easy to do what I suggested as per the following image by selecting code in a click event or any event.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, June 15, 2018 7:29 PM
    Moderator
  • Check this too (instead of PerformClick):

       btnForwrdImgGrp0_Click(Nothing, Nothing)


    • Marked as answer by TJBlues Friday, June 15, 2018 10:03 PM
    Friday, June 15, 2018 7:50 PM
  • Check this too (instead of PerformClick):

       btnForwrdImgGrp0_Click(Nothing, Nothing)


    Better yet:

    btnForwardImgGrp0_Click(Me, EventArgs.Empty)


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Friday, June 15, 2018 8:07 PM
    Moderator
  • I provided only one example (the simplest example) where I have this issue on this one form.  I have 9 more buttons in this one form for which I would need to modify the code and check to see where the button.PerformClick() is used and then modify the routines in those areas.  I have 10 more forms with similar issues that I need to fix.  It is much simpler to use a mass substitute in the editor to change the property button.Visible to button.SomehingThatWorks then it is to modify all that code in all those forms.  I will modify all that code if I have to but I am still looking to see if there is a property that blocks the use from manually clicking on the button and still allows the code to simulate a click.  For example I am tinkering with an overlay that I can turn on and off thereby blocking the buttons from the users view.
    Friday, June 15, 2018 9:04 PM
  • Check this too (instead of PerformClick):

       btnForwrdImgGrp0_Click(Nothing, Nothing)


    Better yet:

    btnForwardImgGrp0_Click(Me, EventArgs.Nothing)


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    I get the following build error:

    Error 1 'Nothing' is not a member of 'System.EventArgs'.

    But it works if I use :

    btnForwardImgGrp0_Click(Me, Nothing)

    • Edited by TJBlues Friday, June 15, 2018 9:22 PM
    Friday, June 15, 2018 9:06 PM
  • Check this too (instead of PerformClick):

       btnForwrdImgGrp0_Click(Nothing, Nothing)


    OK this seems to work if I make that substitution.  Also:

    btnForwardImgGrp0_Click(Me, Nothing)

    will work.

    So in effect I am calling the Sub "BtnForwrdImgGrp0_Click" rather than creating a "click" with the BtnForwrdImgGrp0.PerformClick()

    So all I need to do is search for the ".PerformClick()" in my code and substitute it with "_Click(Me, Nothing)" or "_Click(Nothing, Nothing)

    Where can I read up on the significance of the parameters "Me" and "Nothing" in that call as it pertains to the button and button subroutine behavior?


    Friday, June 15, 2018 9:44 PM
  • Yup I thought of that one but it would make me refactor about 700 lines of code.  I happened to show the simplest example. So I am still hoping for a simple fix maybe some other property instead of button.Visible that would make the Group 0 buttons inactive but still allow the button.PerformClick() to work.

    It's easy to do what I suggested as per the following image by selecting code in a click event or any event.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Hi Karen.  Thanks for all your help.  However my copy of Visual Studio does not have that refactoring function built in.  Also see the other suggestions of using "BtnForwrdImgGrp0_Click(Me, Nothing)" instead of "BtnForwrdImgGrp0.PerformClick()".  That makes it much easier to modify my code rather than moving code segments around.
    Friday, June 15, 2018 9:53 PM
  • Me is the current container, in this case the form. Nothing in this case means no value is passed for event args, something must be passed as it's required. The internals will sort this out but in this case they are not used thus not an issue.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, June 15, 2018 10:33 PM
    Moderator
  • Check this too (instead of PerformClick):

       btnForwrdImgGrp0_Click(Nothing, Nothing)


    Better yet:

    btnForwardImgGrp0_Click(Me, EventArgs.Nothing)


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    I get the following build error:

    Error 1 'Nothing' is not a member of 'System.EventArgs'.

    But it works if I use :

    btnForwardImgGrp0_Click(Me, Nothing)


    I'm so sorry TJ... that was a dumb mistake because I was typing too fast.  I've fixed the post...  it is EventArgs.Empty.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Saturday, June 16, 2018 11:23 AM
    Moderator