I have the following problem: I have a panel which has a specific color, say red.
When the user presses his mouse, the color of this panel gets stored in a variable. Then the user moves, his mouse still pressed, over to another panel. When he releases the mouse there, this panel should get the background color of the first that had been stored in the variable. My code looks something like this:
public Color currentColor;
private void ColorPickMouseDown(object sender, MouseEventArgs e)
{
Panel pnlSender = (Panel)sender;
currentColor = pnlSender.BackColor;
}
private void AttempsColorChanger(object sender, MouseEventArgs e)
{
Panel pnl = (Panel)sender;
pnl.BackColor = currentColor;
}
I need to identify the sender first because there are many possible panels that can trigger this event. The first MouseDown method works totally fine, the color is stored nicely in the variable. The secon one however doesn't even get triggered when the user does what I described above. When the ser clicks on the second panel, it works (there is an MouseUp part in a click aswell I guess).
What's wrong here? Why is the event not triggered when the user holds the mouse key down before?
(This answer assumes you are using Windows Forms.)
It could be that you need to capture the mouse by setting this.Capture = true in the MouseDown of the source control. (See Control.Capture)
If you did that, the source window would get the MouseUp event, and it would be the source window that had to determine the destination window under the mouse coords. You can do that using Control.GetChildAtPoint() (see this answer on Stack Overflow).
Use Windows Forms Drag and Drop Support Instead! <- Click for more info
I'm going to suggest you bite the bullet and use the .Net Drag and Drop methods to do this. It requires some reading up, but it will be much better to use it.
You start a drag in response to a MouseDown event by calling Control.DoDragDrop().
Then you need to handle the Control.DragDrop event in the drop target control.
There's a few more things you might need to do to set it up; see the Control.DoDragDrop() documentation for an example.
(For WPF drag and drop support, see here.)
when your mouse enter the target control , mouse down triggerd ang get target BackColor! you need add an boolean flag to your code :
public Color currentColor;
bool flag=false;
private void ColorPickMouseDown(object sender, MouseEventArgs e)
{
if(flag==false)
{
flag=true
Panel pnlSender = (Panel)sender;
currentColor = pnlSender.BackColor;
}
}
//assume mouse up for panles
private void AttempsColorChanger(object sender, MouseEventArgs e)
{
if(flag==true)
{
Panel pnl = (Panel)sender;
pnl.BackColor = currentColor;
flag=flase;
}
}
and also you need change your flag in mouseMove( if )
As I mentioned in my comment Mouse Events are captured by the originating control, You would probably be better off using the DragDrop functionality built into Windows Forms. Something like this should work for you. I assigned common event handlers, so they can be assigned to all of your panels and just work.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void panel_MouseDown(object sender, MouseEventArgs e)
{
((Control)sender).DoDragDrop(((Control)sender).BackColor,DragDropEffects.All);
}
private void panel_DragDrop(object sender, DragEventArgs e)
{
((Control)sender).BackColor = (Color)e.Data.GetData(BackColor.GetType());
}
private void panel_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
}
I know it's an old question but I had the same issue and none of the above answers worked for me. In my case I had to handle the MouseMove event in the target control and check for the mouse to be released. I did set 'BringToFront' on my target panel just in case that helped at all.
public Color currentColor;
private void ColorPickMouseDown(object sender, MouseEventArgs e)
{
Panel pnlSender = (Panel)sender;
currentColor = pnlSender.BackColor;
}
private void panelTarget_MouseMove(object sender, MouseEventArgs e)
{
//the mouse button is released
if (SortMouseLocation == Point.Empty)
{
Panel pnl = (Panel)sender;
pnl.BackColor = currentColor;
}
}
Related
I'm doing some kind of a pixel film editor and my problem there is that I am using Labels which I have to click individually to change their color, I would like to change the color by holding down the mouse button and just hovering above the Label.
Is there any Event or such to do so? I didn't find one. At the moment I am using Mouse_Down.
A look on the Editor
my Mouse_Down Event:
private void Mouse_Down(object sender, MouseEventArgs e)
{
var ActiveLabel = sender as Label;
if (ActiveLabel != null)
{
ActiveLabel.BackColor = ActiveColor.BackColor;
}
}
So I want it like on GIMP or stuff, to not click any single "pixel" but instead click and hold the mouse button and move it around and color all pixels I move over whilst my mouse is pressed.
Here's the idea:
You have a flag (boolean) which indicates whether the user is currently painting or not. If the mouse hovers over a label (event MouseEnter), you check whether it should be painted or not. If it shall be painted, you apply the code you already have.
bool paintmode = false;
// Assign this to all labels, since you don't know
// where the user will start painting
private void Mouse_Down(object sender, MouseEventArgs e)
{
paintmode = true;
}
// Make sure you assign this to all items, even the Form
// as you'll never know where the user will release the mouse
private void Mouse_Up(object sender, MouseEventArgs e)
{
paintmode = false;
}
// Assign this only to labels which can get painted
private void Mouse_Enter(object sender, MouseEventArgs e)
{
if (paintmode)
{
var ActiveLabel = sender as Label;
if (ActiveLabel != null)
{
ActiveLabel.BackColor = ActiveColor.BackColor;
}
}
}
This question already has answers here:
Why doesn't doubleclick event fire after mouseDown event on same element fires?
(2 answers)
Closed 5 years ago.
I have a MenuStrip from which I want to drag some things on the form body(then some things happen such as the backcolor of the form is changing, etc). I am handling the MouseDown event, but the thing is that when I click on the option in the ToolStripMenu the same things happen(the backcolor of the form is changing, etc).
What I want is to somehow separate the MouseClick from the MouseDown. More precisely, when I click on one option of the MenuStrip I don't want anything to happen.
When I click, the MouseDown event fires. I want to ignore it unless the mouse's cursor moves.
private void salmonToolStripMenuItem_MouseDown(object sender, MouseEventArgs e)
{
//gets the cursor position at the moment when mouse down is activated
p1M = Cursor.Position.X;
p2M = Cursor.Position.Y;
//miscare shows if the mouse moved
if (miscare == true)
{
//do things
}
}
private void Rezervare1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void salmonToolStripMenuItem_MouseUp(object sender, MouseEventArgs e)
{
//gets the cursor position to see if it moved
p1m = Cursor.Position.X;
p2m = Cursor.Position.Y;
if (p1M != p1m || p2M != p2m)
{
miscare = true;//it means the cursor moved
}
else miscare = false;
}
If you want your logic to happen on mouse move, then lets handle MouseMove!
bool isMouseDown = false;
private void salmonToolStripMenuItem_MouseDown(object sender, MouseEventArgs e)
{
isMouseDown = true;
}
private void salmonToolStripMenuItem_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
}
private void salmonToolStripMenuItem_MouseMove(object sender, MouseEventArgs e)
{
if(isMouseDown)
{
//Do your thing
}
}
Also note, there are some strange edge cases you can hit with things like the form losing focus, or the user dragging outside of the bounds of the form. Something to be aware of and handle as needed.
I'd like to create a roulette game, and I've made a PictureBox with an image of a roulette table.
I've tried to set Visibility = False in the property and in code:
pictureBox1.Visible = !pictureBox1.Visible;
But when I click on the hidden PictureBox, nothing happens.
Do you have any idea how to make it work?
I want to make it visible from being invisible, by clicking on it.
Once you make the visibility of an Item false, it can no longer be clicked. You can handle the click event on the form or container level, check that the mouse coordinates are contained in the bounds of the PictureBox and use that to make it visible again.
i.e.
//Common eventhandler assigned to all of your PictureBox.Click Events
private void pictureBox_Click(object sender, EventArgs e)
{
((PictureBox)sender).Visible = false;
}
private void Form1_Click(object sender, EventArgs e)
{
foreach (var item in this.Controls) // or whatever your container control is
{
if(item is PictureBox)
{
PictureBox pb = (PictureBox)item;
if (pb.Bounds.Contains(PointToClient( MousePosition)))
{
pb.Visible = true;
}
}
}
}
Problem with your logic is that once the PictureBox is made invisible, you can no longer click it anymore, since it's no longer on the form, and while you can for sure click the empty space it left, you're still not clicking it.
A possibility would be to, instead of making visible/invisible, to put/remove its background picture, so it appears to have disappeared but in fact it's still there, still able to receive clicks and respond to events.
If you want to make the pictureBox1 not visible, you would write pictureBox1.Visible = false;
Edit: What you are doing should work. I created a winform and put a picture box in it. I double clicked the picture box to make Click event handler and typed pictureBox1.Visible = !pictureBox1.Visible;. When I debug the program and click on the picture box, it turns invisible. I can't think of why it isn't working for you.
Edit: I understand what you want now. To make it visible on the click of it when it is invisible. Unfortunately I don't know how to do that.
A maybe-silly but simple way might be to put one picture box on top of the other. pictureBox1 would be your picture, pictureBox2 would have no picture. Then you could do something like this:
private void pictureBox1_Click(object sender, EventArgs e)
{
pictureBox1.Visible = false;
pictureBox2.Visible = true;
}
private void pictureBox2_Click(object sender, EventArgs e)
{
pictureBox1.Visible = true;
pictureBox2.Visible = false;
}
You could put each of your PictureBox controls in a Panel control. Then handle the Click event of both the Panel and the PictureBox.
private void panel1_Click(object sender, EventArgs e)
{
pictureBox1.Visible = true;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
pictureBox1.Visible = false;
}
I have a program that uses OpenTk.GLControl. Now on my listener, every time the mouse hovers to the said control, say "glControl1", I want to get the mouse coordinates.
Is that possible? sample code below.
private void glControl1_MouseHover(object sender, EventArgs e)
{
// get the current mouse coordinates
// .........
}
OpenTK.GLControl inherits from System.Windows.Forms.Control. You can use the following code snippet to get the mouse position:
private void glControl1_MouseHover(object sender, EventArgs e)
{
Control control = sender as Control;
Point pt = control.PointToClient(Control.MousePosition);
}
Please refer to the MSDN WinForms documentation for more information.
The problem is that you're using the wrong event. Many UI actions in WinForms trigger multiple events per action; Hover is used for things like popping up tooltips. You don't get a coordinate in Hover because it's unnecessary.
What you want is the MouseMove event. This is used to track the mouse position:
private void glControl1_MouseMove(object sender, MouseEventArgs e)
{
foo = e.X;
bar = e.Y;
}
I dont know, what is OpenTk.GLControl, but:
I was handling swipe events on Windows Phone and did this:
private void PhoneApplicationPage_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
//string to save coordinates of tap
TapCoordinatesXBegin = e.GetPosition(LayoutRoot).X.ToString();
TapCoordinatesYBegin = e.GetPosition(LayoutRoot).Y.ToString();
}
And i dont remember such event MouseHover - maybe MouseEnter?
I am making simple paint application in which a line would be drawn whenever someone holds down the mouse button and drags(exactly like in windows paint).
However i am having a hard time finding the suitable event handler for this. MouseDown is simply not working and MouseClick is only jotting down dots whenever i press a mouse down.
Need help in this matter.
Thanks.
Handle MouseDown and set a boolean variable to true. Handle MouseMove and, if the variable is set to true and the mouse's movement is above your desired treshold, operate. Handle MouseUp and set that variable to false.
Example:
bool _mousePressed;
private void OnMouseDown(object sender, MouseEventArgs e)
{
_mousePressed = true;
}
private void OnMouseMove(object sender, MouseEventArgs e)
{
if (_mousePressed)
{
//Operate
}
}
private void OnMouseUp(object sender, MouseEventArgs e)
{
_mousePressed = false;
}