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:
Related
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!
I know that this is not a good practice, but I would like to simulate a mouse click event on a textbox, or a window in my wpf application.
I need this, because I am calling this application with some kind of interceptor and the window doesn't get the full focus, it is in front, textbox has a focus but u can't directlly type on textbox.
on window load I call these methods
this.Activate();
this.Topmost = true;
TextBox1.Focus();
Keyboard.Focus(TextBox1);
Thank you.
I had the same issue in VB. i just wanted the focus in the SpinEdit1 (or any control) when the form load. Here is:
private void formX_Load(object sender, EventArgs e)
{
this.ActiveControl = SpinEdit1;
}
(I am newbie and this is probably a duplicate question.)
To see which control is clicked on form, I have a method like this;
public void IdentifyControl(object sender, System.Windows.Input.MouseEventArgs e)
{
Control ctrl = sender as Control;
if (ctrl != null)
SelectedControl = ctrl;
this.Cursor = new System.Windows.Forms.Cursor(System.Windows.Forms.Cursor.Current.Handle);
MessageBox.Show(""+ctrl.GetChildAtPoint(System.Windows.Forms.Cursor.Position).ToString());
}
and trying to call it from mouse click.
In my first attempt, I added this function to main forms MouseClick event but it only worked for form itself but controls in form. Then I tried to create a general click event and use by Mouse class.
The main point I stuck is that I couldn't create the suitable parameters Mouse.AddMouseUpHandler(DependencyObject element,
MouseButtonEventHandler handler)
Its probably because of I don't know event handling but maybe I am all in a wrong way.
You can create a mouse click event just double clicking on any buttons, labels, etc, and the Visual studio will create a code for you, like this:
private void YOUROBJECT_Click(object sender, EventArgs e)
{
}
Just put the method you want into this click event.
Hope this can help you!
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 ToolTipStatusLabel in a StatusStrip located at the bottom of a Windows Form. I want to show some information when the mouse hovers over it. I found the ToolTipText which does display but down and right. It seems to come up to some degree when the window is maximized but flickers badly as well (the display is a list so it could be quite long).
I cannot find a way to change the way the tip displays so that it goes up and right. Neither can I find a way to attach a ToolTip to the StatusLabel. I have read that I can control the location of a ToolTip using the Placement Properties but they are not available (as far as I can tell( for the StatusLable ToolTipText.
An alternative would be for me to handle this via the MouseEnter and MouseLeave events of the StatusLabel and write some sort of hown grown borderless window. I'd rather not if there is some other way.
Many thanks in advance
Try attaching the tooltip to the StatusStrip control, and then from there, you should be able to show the tip on the label's MouseHover event:
ToolTip tt = new ToolTip();
public Form1() {
InitializeComponent();
}
private void toolStripStatusLabel1_MouseHover(object sender, EventArgs e) {
tt.Show("This is my tool tip",
statusStrip1,
new Point(toolStripStatusLabel1.Bounds.Right,
toolStripStatusLabel1.Bounds.Top - 10));
}
private void toolStripStatusLabel1_MouseLeave(object sender, EventArgs e) {
tt.Hide(statusStrip1);
}
Visual Studio 2017
' ToolStripStatusLabel1 ToolTipText için;
Private Sub ssLDBStatus_MouseHover(sender As Object, e As EventArgs) Handles ssLDBStatus.MouseHover
ToolTip1.SetToolTip(StatusStrip1, "test")
End Sub