I have a button and inside my button I have an image control. When the users click on the image I'd like to animate the button so it appears the button was pressed. I don't really care whether the actual button press event fires or not--it's the illusion of a button press I want to see.
Note: the only options I see on the web involve writing directly to the Windows API--a level of complexity and non-upgradability I really don't want to get into.
Why not just use two different images, one for a normal state, and another for when your button is being pressed.
If you want to go for more complicated route try using GDI+. Here is a quick sample tutorial on how to do this.
Why are you using an image control inside your button control instead of using the button control's Image property?
Using the Image property of the button will give you a button with an image that the user can press and that will raise the OnClick event without doing any extra work or re-implementing features that are already available.
I ended up making my picturbox look like a button by giving it a raised border style. Then on the mouseclick event I simulate the look of a button press by changing the border style for a few hundred miliseconds.
Private Sub simulateButtonPress(ByRef pictureBox As Infragistics.Win.UltraWinEditors.UltraPictureBox)
pictureBox.BorderStyle = Infragistics.Win.UIElementBorderStyle.Inset
Application.DoEvents()
System.Threading.Thread.Sleep(400)
pictureBox.BorderStyle = Infragistics.Win.UIElementBorderStyle.Raised
Application.DoEvents()
End Sub
Related
VB.NET
Visual Studio
I have several buttons that I add at runtime, and I have a MouseDown event handler for the click event. Left click works just fine, but the right click event fires, but doesn't do what I need it to.
If e.Button = Windows.Forms.MouseButtons.Right Then
If sender.Bounds.Contains(e.Location) = True Then
ContextMenuStrip.Show(Cursor.Position)
End If
End If
I shortened this to make it easier to read.
When I look at e.Location, it shows the mouse location in relation to the button.
So if my button is at 400, 600, the location of the mouse should be in that area, but the mouse location comes back with 20, 30 because it is at 20,30 inside the button.
How can I do this right click event properly?
MouseDown event raises when the mouse is down on your control, so the mouse is surely in your control bounds and you don't need to check if the button contains e.Location.
To show the context menu strip, if you assign the context menu strip to ContextMenuStrip property of your control, then you don't need to do anything and the menu will show automatically. But if for any reason you want to handle MouseDown event, you can use either of these options:
ContextMenuStrip1.Show(DirectCast(sender, Control), e.Location)
ContextMenuStrip1.Show(MousePosition)
Note: Just for learning purpose if you want to check if e.Location is in your button, you can use either of these options:
Button1.ClientRectangle.Contains(e.Location)
Button1.Bounds.Contains(Button1.Parent.PointToClient(Button1.PointToScreen(e.Location)))
Or some other combinations using PointToClient, PointToScreen, RectangleToClient, RectangleToScreen methods of controls.
i have developed custom control which inherits Button control, actually i`m creating button with one picturebox and label on Button(picturebox and label are child controls). I created same method and wired child controls to behave as clicking on parent(Button), it works but its slow no mather what i do(tried with InvokeOnClick, me.performclick, Me.OnClick(New EventArgs()), tried with same method for all events), but clicking on label or picture box is slow, i need it to be fast as clicking directly on button cause in my application is very important to be able to click on button twice in second for example, if you click on label or picturebox twice at second it will fire just one time not 2.
What i have been thinking of is to make label and picturebox invisible for event is it possible or anyother idea ?
Thanks in forward
You are doing battle with ControlStyles.StandardDoubleClick. Which is turned on for the PictureBox and Label controls but turned off for Button.
So when you are clicking rapidly on either the picturebox or the label then they'll generate the DoubleClick event instead of the Click event. You'll interpret that as "slow" since you probably didn't write a handler for that event. Subscribing the event and calling Me.OnClick is a workaround.
Just don't do this. PictureBox and Label are point-and-click convenience controls but they are extraordinarily wasteful. It takes just two lines of code in the button's OnPaint() method override to get the exact same outcome, minus the overhead and the event problems. Use e.Graphics.DrawImage() to draw the pb's image and TextRenderer.DrawText() to draw the label's text.
Using MouseDown event ensures the event is triggered once per click, regardless of click/doubleclick.
I'm creating a windows store app and i want to
change the button color when the mouse hover on the button
use an image as a button
I am using VS 2012 and using VB.net.
Thanks
You can create a mousehover event in your code behind and in that you can do whatever you want.
A perfect and precise answer of both of your questions is the following video
Click here
I’m trying to make a button that displays an image, that’s easy, but the image is an editable icon so I want the image on the button to be updated every time the form is started how can this be done?
I am making the assumption that you are using WinForms and saving your editied icon to a file.
Take a look at this MSDN page on ButtonBase.Image Property.
From above link:
button1.Image = Image.FromFile("C:\Graphics\MyBitmap.bmp")
You can put this in the New Subroutine after the InitializeComponent Statement. or in your Form Load event.
Use Graphics method and also handle the Paint event of Button. You can add text and primitive drawing on Image or Button region.
As the question is asked ... how can i prevent that from happening?
I have ZOOM IN and ZOOM OUT button that receive focus after pressed.
The button in CF does not have focus property so im guessing im required to do another painful workaround...
Any ideas?
This is the way the system draws the buttons when clicked. If you don't want this you'll have to redraw the button (and it various states) yourself