Visual Basic > Visual Basic Forums > Visual Basic General > Make button invisible, but still function?
Ask a questionAsk a question
 

AnswerMake button invisible, but still function?

  • Tuesday, November 03, 2009 11:26 PMEndrien Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I'd like to make it so that you cannot see the button, but it will still work if you click where it is.
    How can I do this?

Answers

  • Wednesday, November 04, 2009 1:20 AM•.trujade.• Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    if you are talking about my post in this thread,
    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/c1e7f9de-9e39-4232-9b6e-ac4ece33b000

    what that does, is create an invisible rectangle in the location of your button. when you click the button, it sets it's self to visible=false.. the outline where your button was is then replaced by the rectangle.  anytime you click in that area, the button sets it's self to visible=true.. i hope my comment lines were detailed enough, although my noobieness is starting to wear off. ;o/ my apologies.

    add this to the code in the above thread for your invisible button, to see an outline of where to click.

        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            Dim R As New Rectangle(Button1.Location.X, Button1.Location.Y, Button1.Width, Button1.Height)
            e.Graphics.DrawRectangle(Pens.Blue, R)
        End Sub
    


    •.' trujade '.•
  • Wednesday, November 04, 2009 2:08 AMbdbodger Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Use the MouseClick event to determine if the spot clicked (e.x,e.y) is within the area of a rectangle such as the one trujade posted . It does not matter if it is the form or a picturebox all that matters is that you use the appropriate event handler and New Rectangle(X,Y,Width,Height) .

        Private Sub PictureBox1_MouseClick( _
        ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles PictureBox1.MouseClick
            Dim R As New Rectangle(50, 50, 50, 50)
            If R.Contains(e.X, e.Y) Then
                MessageBox.Show("The spot was hit")
            End If
        End Sub
    
        Private Sub Form1_MouseClick( _
        ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles MyBase.MouseClick
            Dim R As New Rectangle(50, 50, 50, 50)
            If R.Contains(e.X, e.Y) Then
                MessageBox.Show("The spot was hit")
            End If
        End Sub
    

    coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read .
  • Wednesday, November 04, 2009 7:00 AMCor LigthertMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    You know that a picturebox and a panel have the same events like a button?

    Public Class Form1
    Private Sub Panel1_MouseClick(ByVal sender As Object, _
       ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseClick
       MessageBox.Show("I did not see it, but I am clicked")
    End Sub
    End Class
    



    Success
    Cor
  • Wednesday, November 04, 2009 5:39 PMjgalley Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Place a panel on your form, then place your button inside the panel.
    set the panel border to none.
    size the panel to the button and place the button at 0,0

    in the button click event set the button visible to false
    in the panel click event set the button visible to true

    oops, Cor kind of suggested the same thing.  Sorry I missed that.
  • Wednesday, November 04, 2009 6:59 PMCor LigthertMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Open your form,

    Drag a panel  on that, go to the top of your code and see those two dropdown  boxes.

    In the left one you select the control (the panel) and in the righ one the Click event

    It will be made, paste in that messagebox.Show in that.

    The click f5 you wont see the pannel because it is transparent but as you click on it then that message will be showed.

    You can of couse as well set the background color of a button to transparant, and there are much more possibilities.




    Success
    Cor
  • Wednesday, November 04, 2009 2:18 AM•.trujade.• Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    And how do I get this location?
    ok. try this..
    place a button anywhere on your form.
    then click the button once, to highlight it and load the properties for it.
    locate the ( location ) of the button in the properties..
    those should be your first two numbers..
    locate the ( size ) of the button in the properties.
    those should be your last 2 numbers in that code.
    you now should have your area to click.
    delete the button and click in the area it was located when you got those numbers.

    make sure you have something as a msgbox, etc. show that you clicked that location, as bdboger supplied.

    •.' trujade '.•
  • Wednesday, November 04, 2009 5:08 PMAndrew B. Painter Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    If you're going to insist on nonstandard practices (like using a background image), you're going to have to figure some things out for yourself; you take the code provided to solve your basic problem and adapt it to your singular situation.

    In this case, you have to figure out either how to clip the backgroundimage segment immediately behind the button and present that segment on the button's surface (if the button is in a fixed position you can do this manually with Paint, or you can do it programmatically with System.Drawing and Graphics objects) or else you eliminate the button altogether and figure out how to trap mouse-clicks on the surface of the form itself and determine what area the mouse was in when the button went down, which triggers your code if the mouse cursor was in your targeted position.




    It never hurts to try. In a worst case scenario, you'll learn from it.

All Replies

  • Tuesday, November 03, 2009 11:33 PMjwavila Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    setting the Visible property to False won't work

    so you have to make it blend into the background

    try this:

    set the FlatStyle property to Flat
    click on the + next to FlatAppearance
    change the BorderColor to the color of your Form
    change the BorderSize to 0

    erase whatever is in the Text property

  • Tuesday, November 03, 2009 11:59 PMEndrien Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I can't blend it, the form has an image background.
  • Wednesday, November 04, 2009 12:47 AMjwavila Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    so what was wrong with the solution provided on your other post?

    If your Form has a background image, you'll have to determine if where you have clicked is within a certain area, which is what the other post showed you how to do.

    Just need to set the X and Y coordinates and the width and height of the rectangle the same as your button would be
  • Wednesday, November 04, 2009 12:48 AMEndrien Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    so what was wrong with the solution provided on your other post?

    If your Form has a background image, you'll have to determine if where you have clicked is within a certain area, which is what the other post showed you how to do.

    Just need to set the X and Y coordinates and the width and height of the rectangle the same as your button would be
    It didn't show me how to do it, he didn't explain it, if I copy pasted it it didn't work.
  • Wednesday, November 04, 2009 1:10 AMjwavila Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    If you rely on Copying and Pasting of code provided here, you'll never learn anything.

    That code provided shows the code to use and which event to put the code in.

    And saying "it didn't work" tells us nothing. What didn't work? Be specific - what did you do to try it? Post the code you tried. Trust me, that code works - but it might need a little tweaking in your situation.

    So try looking at the code and adapt it to your situation. For example, yes, that code won't work if you have another control docked in your main Form.

    In that case look for a similar event for your control and try it there.

    You need to try to work some of this out on your own.
  • Wednesday, November 04, 2009 1:20 AM•.trujade.• Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    if you are talking about my post in this thread,
    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/c1e7f9de-9e39-4232-9b6e-ac4ece33b000

    what that does, is create an invisible rectangle in the location of your button. when you click the button, it sets it's self to visible=false.. the outline where your button was is then replaced by the rectangle.  anytime you click in that area, the button sets it's self to visible=true.. i hope my comment lines were detailed enough, although my noobieness is starting to wear off. ;o/ my apologies.

    add this to the code in the above thread for your invisible button, to see an outline of where to click.

        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            Dim R As New Rectangle(Button1.Location.X, Button1.Location.Y, Button1.Width, Button1.Height)
            e.Graphics.DrawRectangle(Pens.Blue, R)
        End Sub
    


    •.' trujade '.•
  • Wednesday, November 04, 2009 1:24 AM•.trujade.• Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    ahh.. jwavilla , i just thought of something.. what if that is not a background image, but a picturebox as a background image.. it might block the user ( endrien ) from clicking the form, by clicking the image layered on top of it..  in that case, the code should be moved to the picturebox_click event, not the form_click event.
    hope this helps.


    •.' trujade '.•
  • Wednesday, November 04, 2009 1:37 AMEndrien Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    ahh.. jwavilla , i just thought of something.. what if that is not a background image, but a picturebox as a background image.. it might block the user ( endrien ) from clicking the form, by clicking the image layered on top of it..  in that case, the code should be moved to the picturebox_click event, not the form_click event.
    hope this helps.


    •.' trujade '.•

    Yes sorry I should have specified, it is a Picturebox that's the background(Good memory mate)

    Also this code *Does* work(The code from the previous discussion), but not the way I want it to. It will make it invisible after it is clicked, I want it invisible all the time.

  • Wednesday, November 04, 2009 1:56 AM•.trujade.• Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed AnswerHas Code
    endrien , then just replace this line,
     Dim R As New Rectangle(Button1.Location.X, Button1.Location.Y, Button1.Width, Button1.Height)
    
    

    with the location of where you want your invisible button..
    ex.
     Dim R As New Rectangle(50, 50, 50, 50)
    

    first number is your location by x ( left to right on your form ), second is location by y ( top to bottom ), third is the size of your area from x, and forth is size of area from y.

    •.' trujade '.•
  • Wednesday, November 04, 2009 2:01 AMEndrien Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    endrien , then just replace this line,
     Dim
     R As
     New
     Rectangle(Button1.Location.X, Button1.Location.Y, Button1.Width, Button1.Height)
    
    

    with the location of where you want your invisible button..
    ex.
     Dim
     R As
     New
     Rectangle(50, 50, 50, 50)
    

    first number is your location by x ( left to right on your form ), second is location by y ( top to bottom ), third is the size of your area from x, and forth is size of area from y.

    •.' trujade '.•


    And how do I get this location?
  • Wednesday, November 04, 2009 2:08 AMbdbodger Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Use the MouseClick event to determine if the spot clicked (e.x,e.y) is within the area of a rectangle such as the one trujade posted . It does not matter if it is the form or a picturebox all that matters is that you use the appropriate event handler and New Rectangle(X,Y,Width,Height) .

        Private Sub PictureBox1_MouseClick( _
        ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles PictureBox1.MouseClick
            Dim R As New Rectangle(50, 50, 50, 50)
            If R.Contains(e.X, e.Y) Then
                MessageBox.Show("The spot was hit")
            End If
        End Sub
    
        Private Sub Form1_MouseClick( _
        ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles MyBase.MouseClick
            Dim R As New Rectangle(50, 50, 50, 50)
            If R.Contains(e.X, e.Y) Then
                MessageBox.Show("The spot was hit")
            End If
        End Sub
    

    coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read .
  • Wednesday, November 04, 2009 2:18 AM•.trujade.• Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    And how do I get this location?
    ok. try this..
    place a button anywhere on your form.
    then click the button once, to highlight it and load the properties for it.
    locate the ( location ) of the button in the properties..
    those should be your first two numbers..
    locate the ( size ) of the button in the properties.
    those should be your last 2 numbers in that code.
    you now should have your area to click.
    delete the button and click in the area it was located when you got those numbers.

    make sure you have something as a msgbox, etc. show that you clicked that location, as bdboger supplied.

    •.' trujade '.•
  • Wednesday, November 04, 2009 2:58 AM•.trujade.• Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    off-topic....
    bdbodger , what size is your monitor/screen???? 20x10.. roflmao
    the way you recently post code only leads me to know that you use an itty bitty desktop area.. i currently have my resolution to 1440x900, and resizing it to 600 width seems to fit your code in the webbroswer but the rest just plops out..  do they even have resolutions for anything less than 800x600??
    ...
    just busting chops.
    the idea seems to help w/code needed to be scrolled in threads..  nice. ;o)


    •.' trujade '.•
  • Wednesday, November 04, 2009 3:05 AMCrazypennie Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     


    The font for the code is to big any way,

    It is good on my 24" screen, but on my laptop, it is terrible
  • Wednesday, November 04, 2009 6:09 AMbdbodger Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    off-topic....
    bdbodger , what size is your monitor/screen???? 20x10.. roflmao
    the way you recently post code only leads me to know that you use an itty bitty desktop area.. i currently have my resolution to 1440x900, and resizing it to 600 width seems to fit your code in the webbroswer but the rest just plops out..  do they even have resolutions for anything less than 800x600??
    ...
    just busting chops.
    the idea seems to help w/code needed to be scrolled in threads..  nice. ;o)


    •.' trujade '.•

    I use a 27 inch widescreen monitor it is a SyncMaster 275T that is 1920x1200 with all the inputs such as HDMI and composite . I don't like the scrollbars in the code window so I use a _ so the lines are not too long and I don't get scrollbars .
    coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read .
  • Wednesday, November 04, 2009 7:00 AMCor LigthertMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    You know that a picturebox and a panel have the same events like a button?

    Public Class Form1
    Private Sub Panel1_MouseClick(ByVal sender As Object, _
       ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseClick
       MessageBox.Show("I did not see it, but I am clicked")
    End Sub
    End Class
    



    Success
    Cor
  • Wednesday, November 04, 2009 4:58 PMEndrien Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I still don't know how to do this..
  • Wednesday, November 04, 2009 5:08 PMAndrew B. Painter Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    If you're going to insist on nonstandard practices (like using a background image), you're going to have to figure some things out for yourself; you take the code provided to solve your basic problem and adapt it to your singular situation.

    In this case, you have to figure out either how to clip the backgroundimage segment immediately behind the button and present that segment on the button's surface (if the button is in a fixed position you can do this manually with Paint, or you can do it programmatically with System.Drawing and Graphics objects) or else you eliminate the button altogether and figure out how to trap mouse-clicks on the surface of the form itself and determine what area the mouse was in when the button went down, which triggers your code if the mouse cursor was in your targeted position.




    It never hurts to try. In a worst case scenario, you'll learn from it.
  • Wednesday, November 04, 2009 5:39 PMjgalley Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Place a panel on your form, then place your button inside the panel.
    set the panel border to none.
    size the panel to the button and place the button at 0,0

    in the button click event set the button visible to false
    in the panel click event set the button visible to true

    oops, Cor kind of suggested the same thing.  Sorry I missed that.
  • Wednesday, November 04, 2009 6:59 PMCor LigthertMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Open your form,

    Drag a panel  on that, go to the top of your code and see those two dropdown  boxes.

    In the left one you select the control (the panel) and in the righ one the Click event

    It will be made, paste in that messagebox.Show in that.

    The click f5 you wont see the pannel because it is transparent but as you click on it then that message will be showed.

    You can of couse as well set the background color of a button to transparant, and there are much more possibilities.




    Success
    Cor