Hi, I'm trying to hide 5 to 12 buttons on a form until a screenshot is saved to the clipboard. I have it set right now so the user clicks a button(btnSavePDF) and then whichever buttons are visible are made not visible. But I haven't a clue how to restore just those buttons. I'm trying to avoid restoring them all unless they were all originally visible. Please help.

Here is the code I have so far:

Private Sub btnSavePDF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSavePDF.Click

         Try
            If btnChange1.Visible = True Then btnChange1.Visible = False
            If btnChange2.Visible = True Then btnChange2.Visible = False
            If btnChange3.Visible = True Then btnChange3.Visible = False
            If btnChange4.Visible = True Then btnChange4.Visible = False
            If btnChange5.Visible = True Then btnChange5.Visible = False
            If btnChange6.Visible = True Then btnChange6.Visible = False
            If btnChange7.Visible = True Then btnChange7.Visible = False
            btnNewSystem.Visible = False
            btnOptional.Visible = False
            btnPrint.Visible = False
            btnSavePDF.Visible = False
            btnRecalc.Visible = False
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

        ' Create a blank bitmap.
        Dim bmpScreenshot As New Bitmap(850, 690, PixelFormat.Format32bppArgb)
       
        ' Create a graphics object from the bitmap.
        Dim gfxScreenshot As Graphics = Graphics.FromImage(bmpScreenshot)
        
        ' Take a screenshot of frmReport.
        gfxScreenshot.CopyFromScreen(frmMain.lblSizer.Left + 122, frmMain.lblSizer.Top + 24, _
                                     0, 0, frmMain.lblSizer.Size, CopyPixelOperation.SourceCopy)

        Clipboard.SetImage(bmpScreenshot)

        ' Restore buttons to their orginal state.
        ' uh ? Help...

Here's an example of creating a list of visible buttons on a form and hiding them for one second and them making them visible again.

Dim buttons = (From btn As Button In Me.Controls.OfType(Of Button)() _
                      Where btn.Visible = True _
                      Select btn).ToList()

        Dim button As Button
        For Each button In buttons
            button.Visible = False
        Next

        Threading.Thread.CurrentThread.Sleep(1000)

        For Each button In buttons
            button.Visible = True
        Next

Hi,
You need to do a workaround like using a collection to store only the visible buttons that you need to reset to their state again

Public Sub Test()
        Dim coll As New Collection()

        Dim b As New Button()

        If b.Visible = True Then
            coll.Add(b)
            b.Visible = False
        End If
    End Sub

Then you loop the collection and set all the buttons visibility again to true.

Thanks for your fast responses. I'll give these solutions a shot first chance I get.

Thank you both for your help. These solutions both work excellently, I ended up creating a sub that hides the buttons as suggested with button from me.controls(of type)... and after the screenshot is taken my code calls another sub that restores the buttons' visibility. Thank you again for your valuable help and time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.