Flat button changes color on mouse press - c#

3D buttons aren't a problem. It's the flat buttons (when Button.FlatStyle == FlatStyle.Flat).
I set the background color of the button Color.Black. When I press and hold the left mouse button, the background color changes to a gray color, which I am trying to prevent.
I've tried some Mouse events triggered by the button, especially MouseDown. I've also used the BackColorChanged event and still didn't work.
In short, when the mouse is down, the button is grey. When the mouse is up, the button is black. How do I prevent that? How do I keep the button black?

Changing property Button.FlatAppearance.MouseDownBackColor works like a charm:
private void FocusBtn_MouseDown(object sender, MouseEventArgs e)
{
FocusBtn.FlatAppearance.MouseDownBackColor = Color.Black;
}

Related

How to change button color on mouse hover and override default behaviour

I am working on a C# forms application. i want to set custom color for the buttons when mouse is over them
By default what happens is, when mouse moves over a button its color changes slightly.
I changed the color using the onmouseover function. the issue i am having is the when i move the cursor over the button the button color changes to the default mouse over color for a split second and then it changes to the color i have set.
so what is happening is when i move the mouse over the button changes color twice. I want it to change directly to the color i have set for the button
Assuming you're using Windows forms Control.MouseHover event. There's an intentional delay on this event. If you want your color change to trigger immediately try using Control.MouseEnter event, see: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.mouseenter?view=net-5.0

How to detect a click on a component that is not visible

Is it possible to detect a click on a picturebox that is not visible
I have tried this :
private void PictureBox1_Click(object sender, EventArgs e)
{
MessageBox.Show("teste");
}
But even if the picture box has no image and that background color is transparent, it hides the elements behind it.
I explain myself a little better;
I do a tic-tac-toe and I have the image of the cross that is not visible when the game is started; as soon as I press on a delimited area (where I want to place the cross), I want it to become visible
How can I do that?
Thank you.
I would use the PictureBox with an ImageList containing 2 images: the first is the background of your game field and the second is the cross.
In your Click Event I would toggle the pictures on click then.

Mouse Cursor Flickering while peforming DragOver event on Panel control in winform in C#

I am trying to implement a DRAG DROP event in windows form. The Drag event starts when I select some text in text box, click and hold the right mouse button and start dragging the text from the text box, passes over a panel(Right mouse button still hold/clicked) and then the dragged text has to be dropped over a tree view(release right mouse button).
I want my mouse cursor to change when i am dragging over the panel control. The Cursor do change to my expected cursor icon as of now but it flickers a lot and cursor image is not constant.
I am implementing the logic in Drag Over event handler which i think gets fired again and again automatically while dragging and this results in flickering or some sort of refreshing.
My implementation is as follows:
ABC.Desginer.cs:
this.splitterPanel.DragOver += new System.Windows.Forms.DragEventHandler(this.splitterPanel_DragOver);
ABC.cs:
private void splitterPanel_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
{
Bitmap bmp = new Bitmap(ABC.DefInstance.ImageList1.Images[3]);
Cursor c = new Cursor(new Bitmap(bmp, 32, 32).GetHicon());
frmFlat2Obj.DefInstance.splitterPanel.Cursor = c;
DestroyIcon(c.Handle);
}
}
My question is: How exactly shall I stop or reduce the mouse cursor flickering while dragging over the Panel??
THANKS IN ADVANCE :)

How can I make an imitation of a button control with a picture box, that behaves exactly like a button?

Buttons do that: if the user preses and holds he left mouse button over a button control and moves the cursor out of the control, the button control changes it's appearance back to default, and if the mouse button is still being held, and the cursor enters a button control, the control changes it's appearance to it's pressed version. I'm trying to imitate a button using a PictureBox, but when the mouse leaves the PictureBox before the left mouse button is released, the PictureBox's picture doesn't change until the mouse button is released.
I'm trying to do this because the button control can't look the way I want to.
How can I make an imitation of a button control with a picture box, that behaves exactly like a button?
This is by design, the control sets the Capture property to true so it will keep receiving mouse messages while the button is held down when you move the mouse outside of the control rectangle.
You could turn it off when you see it moving out:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e) {
var box = (PictureBox)sender;
if (!box.DisplayRectangle.Contains(e.Location)) box.Capture = false;
}
Try using the DragLeave event as opposed to the MouseLeave event.

Why are my disabled buttons changing color?

My problem is: When my application is opened, I disable all buttons (in form_Load) and their color changes to the color of the background. But then I do some action (like clicking on a button), and in this action I disable the buttons again.
Now some of these buttons become GRAY and others become as the background.
Why is this? I don't want the the gray effect. Normally when I disable the button at the beginning of the application, it becomes the color I expect, but when I try to disable them again this strange behavior appeared. What to do?
My code is like:
private void _btnDownload2PC_Click(object sender, EventArgs e)
{
// do action
_btnDownloadToPC.Enabled=false; // its color became gray
_btnDownloadToPhone.Enabled=false; // its color became like the
// background color and it can't
// be pressed
}
I figured out that the problem is when I use the button_MouseLeave() or button_MouseMove() functions. For example:
private void _btnOneToCort_MouseLeave(object sender, EventArgs e)
{
this._btnOneToCart.Image=global::MyProject.Properties.Resources.button3over;
}
but this doesn't make sense. Why does this function change my button settings (I don't know what these are) When I use these function this strange behavior appears, but when I don't, everything goes right?
You can programmatically access the color of a button. Set a breakpoint and do that to see if they really are changing.
My guess: the image that you are setting has a gray background that is different than the standard disabled color. You will need to edit the image and remove the background.
Why don't disable buttons in designer? If not acceptable, do that in form constructor not Form_Load.
Also, how can you click on button if it's disabled?
Are form color settings default? Have you changed windows theme color preferences?

Categories