I have done a bit of research but cannot seem to find what I am looking for.
What I want to do is make a "custom" button in a windows form. This would basically just be a matter of changing the default "grey" background to a custom image. A custom image would also be used for when hovering over and clicking the button.
This is not just a matter of changing the background image as the image I want to use has rounded edges with a transparent background and I want custom image for hovering / clicked. I want everything else about the button to behave in the same manner as a normal button.
Is this possible?
It is called owner-drawn button
refer to:
Mick Dohertys' .net Tips and Tricks - Tips / Button
GlowButton - A Glowing Button Control
A shiny orb button in GDI+
The solution I found was to set the FlatStyle of the button to Flat and set all the borders to 0. I then had a problem with the focus of the button (it displayed a little border). To solve this I followed this tutorial:
http://dotnetstep.blogspot.com/2009/06/remove-focus-rectangle-from-button.html
With this in place all I had to do was add events to the button so that the image was changed when a certain action was carried out on it:
private void button1_MouseLeave(object sender, EventArgs e)
{
this.button1.Image = Properties.Resources._default;
}
private void button1_MouseEnter(object sender, EventArgs e)
{
this.button1.Image = Properties.Resources._hover;
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
this.button1.Image = Properties.Resources._clicked;
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
this.button1.Image = Properties.Resources._default;
}
Hope this will help someone else!
Related
I just started working on C# with Visual Studio & Windows Forms Applications. I was trying to create a Calculator and I was wondering if I could change the cursor type on a button which is disabled, I can't figure out how to do it, please help me thank you!
Edit: here's the code I tried to do, it only works if the button's enabled...
private void txt_current_operation_MouseHover(object sender,EventArgs e) {
txt_current_operation.Cursor=Cursors.Hand;
}
Hack Answer
Assuming the Button is contained by the Form, you can handle the MouseMove event of the Form and change the cursor from there:
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Rectangle rc = txt_current_operation.RectangleToScreen(txt_current_operation.ClientRectangle);
this.Cursor = rc.Contains(Cursor.Position) ? Cursors.Hand : Cursors.Default;
}
If the Button was contained by a Panel, or some other container besides the Form, then you'd change to the MouseMove event of that container instead.
Demonstration:
I'de like you to take a look at this code:
I have a Button named Button1.
private void button1_MouseHover(object sender, EventArgs e)
{
button1.BackColor = Color.Black;
}
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.BackColor = Color.Blue;
}
This code works but the problem is there is a very small delay. About 1/2 second delay on changing the colors. I've tried the same thing in WPF and there is absolutely no delay in that. Basically I want the Mouse event to fire as quickly as possible.
In what ways can i accomplish that task ?
Thank you
Try using the MouseEnter event rather than MouseHover - the latter is fired 'after a delay' because Windows can't tell that the mouse is hovering unless it has been stationary for a short while.
Calling button1.Invalidate(false) will result in redrawing the control within the next frame. Place this line right after your color-changing code and see if it works.
I have a small (but annoying) problem.
You can quickly replicate it by doing the following:
New Project > Windows Store > Blank App (XAML)
Add a button to the grid. This also works with the default style. (note: TextButtonStyle is defined in SimpleStyles.xaml)
<Button Click="Click" Style="{StaticResource TextButtonStyle}" Content="Page 2"/>
Add the function to the code behind file:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof (Page2));
}
Next create another page, add the button, and navigate back to the MainPage in the Click event.
Next set on both pages add NavigationCacheMode="Enabled". For convenience set one of the buttons to be Left aligned and the other one Right.
Run the app. Move the mouse over the button. The state changes to reflect this. Click the button. Again the color changes. On the second page, do the same. On returning to the first page, the button is still in it's "PointerOver" visual state, since there was no PointerExited event called on the button.
How can I fix this? VisualStateMananger.GoToState() doesn't work.
We encountered a similar issue. We noticed that the state 'resets' when you hide the control.
We solved it, the dirty way:
void GridView_ItemClick(object sender, ItemClickEventArgs e)
{
var ctrl = this.ItemContainerGenerator.ContainerFromItem(e.ClickedItem);
((Control)ctrl).Visibility = Visibility.Collapsed;
((Control)ctrl).Visibility = Visibility.Visible;
}
Maybe you could try to do the following (didn't test this):
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
((Control)sender).Visibility = Visibility.Collapsed;
((Control)sender).Visibility = Visibility.Visible;
Frame.Navigate(typeof (Page2));
}
I have a ToolStripButton that performs an action. The user clicks the button and the button is disabled to prevent the action being performed twice. After the action is complete the button is re-enabled. It all works perfectly...except:
Because the button is disabled it does not fire the "MouseLeave" event and as a result the appearance of the button is not updated. To be absolutely clear, when the mouse enters a ToolStripButton the button is highlighted in orange (by default) with a black box around it. This highlight is not being removed when I re-enable the button. The mouse cursor is, by this time, long gone from the control. Mousing over the button naturally fixes the button by redrawing it.
What I would like to do would be some method on the ToolStripButton that "resets" its appearance. Such a method may even exist on the ToolStrip, but despite searching I have been unable to find anything like this.
As an alternative I could fire the "Mouse Leave" event on the button directly. As far as I know there is no way to easily do this in C# .NET.
Any advice at this point in time would be most appreciated, naturally I don't want to tear up my application and replace the tool strip.
Update:
I reproduced your problem, trying figuring out!
I didn't get a better way other than reset the style in the click event
private void toolStripButton1_Click(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.FromKnownColor(KnownColor.Control);
toolStripButton1.Enabled = false;
}
private void toolStripButton1_MouseEnter(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.Red;
}
private void toolStripButton1_MouseLeave(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.FromKnownColor(KnownColor.Control);
}
Hope this helps!
Have you tried Control.Invalidate()?
from MSDN: Invalidates the entire surface of the control and causes the control to be redrawn.
I had the same problem. I "fixed" it by hiding and then showing back the ToolStripButton using the Visible property after the task was complete.
Before disabling ToolStrip or ToolStripItem:
private void RemoveHighlightFromToolStrip(ToolStrip toolStrip)
{
foreach (ToolStripItem item in toolStrip.Items)
{
if (item.Pressed || item.Selected)
{
item.Visible = false;
item.Visible = true;
}
}
}
also you can just hide and show entire ToolStrip, but this may affect other controls in your form (i.e. if you have some docked DataGridView it would be redrawn)
I have a picture on background in a picture box and i have another picturebox small one above the first.now second picbox is not visible its hidden i want that if user clicks and holds mouse button small picturebox displays and as soon he removes hold i.e releases click picturebox vanishes.
now i thought of doing in click event i should make small one visiable but how to check if click is pressed not released?
sorry if i messed up with question
Use the mouse down event:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
}
there you can set the focus on whatever you want
and when you release:
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
}
Why not use the MouseDown event in that case?