Visual Basic >
Visual Basic Forums
>
Visual Basic General
>
Make button invisible, but still function?
Make button invisible, but still function?
- 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
- 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 '.•- Proposed As Answer byAndrew B. Painter Friday, November 06, 2009 3:56 PM
- Marked As Answer byJeff ShanMSFT, ModeratorTuesday, November 10, 2009 1:46 AM
- 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 .- Proposed As Answer byAndrew B. Painter Friday, November 06, 2009 3:56 PM
- Marked As Answer byJeff ShanMSFT, ModeratorTuesday, November 10, 2009 1:46 AM
- 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- Proposed As Answer byAndrew B. Painter Friday, November 06, 2009 3:55 PM
- Marked As Answer byJeff ShanMSFT, ModeratorTuesday, November 10, 2009 1:46 AM
- 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,0in the button click event set the button visible to falsein the panel click event set the button visible to trueoops, Cor kind of suggested the same thing. Sorry I missed that.
- Proposed As Answer byAndrew B. Painter Friday, November 06, 2009 3:55 PM
- Marked As Answer byJeff ShanMSFT, ModeratorTuesday, November 10, 2009 1:47 AM
- 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- Proposed As Answer byAndrew B. Painter Friday, November 06, 2009 3:55 PM
- Marked As Answer byJeff ShanMSFT, ModeratorTuesday, November 10, 2009 1:47 AM
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 '.•- Marked As Answer byJeff ShanMSFT, ModeratorTuesday, November 10, 2009 1:46 AM
- 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.- Marked As Answer byJeff ShanMSFT, ModeratorTuesday, November 10, 2009 1:47 AM
All Replies
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- I can't blend it, the form has an image background.
- 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 so what was wrong with the solution provided on your other post?
It didn't show me how to do it, he didn't explain it, if I copy pasted it it didn't work.
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- 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. - 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 '.•- Proposed As Answer byAndrew B. Painter Friday, November 06, 2009 3:56 PM
- Marked As Answer byJeff ShanMSFT, ModeratorTuesday, November 10, 2009 1:46 AM
- 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 '.• 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.
- 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 '.•- Proposed As Answer byAndrew B. Painter Friday, November 06, 2009 3:56 PM
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?- 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 .- Proposed As Answer byAndrew B. Painter Friday, November 06, 2009 3:56 PM
- Marked As Answer byJeff ShanMSFT, ModeratorTuesday, November 10, 2009 1:46 AM
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 '.•- Marked As Answer byJeff ShanMSFT, ModeratorTuesday, November 10, 2009 1:46 AM
- 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 '.•
The font for the code is to big any way,
It is good on my 24" screen, but on my laptop, it is terribleoff-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 .- 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- Proposed As Answer byAndrew B. Painter Friday, November 06, 2009 3:55 PM
- Marked As Answer byJeff ShanMSFT, ModeratorTuesday, November 10, 2009 1:46 AM
- I still don't know how to do this..
- 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.- Marked As Answer byJeff ShanMSFT, ModeratorTuesday, November 10, 2009 1:47 AM
- 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,0in the button click event set the button visible to falsein the panel click event set the button visible to trueoops, Cor kind of suggested the same thing. Sorry I missed that.
- Proposed As Answer byAndrew B. Painter Friday, November 06, 2009 3:55 PM
- Marked As Answer byJeff ShanMSFT, ModeratorTuesday, November 10, 2009 1:47 AM
- 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- Proposed As Answer byAndrew B. Painter Friday, November 06, 2009 3:55 PM
- Marked As Answer byJeff ShanMSFT, ModeratorTuesday, November 10, 2009 1:47 AM


