MouseHover does not fire when the mouse is down

I am working on a WordSearch voice program (also called WordFind) where you need to find specific words in a mass of letters. I am using C # WinForms.

My problem is when I want to click and hold 1 letter ( Label ) and then drag it to other letters to change their ForeColor . I tried searching the Internet, but to no avail.

Here is what I have:

 foreach (Letter a in game.GetLetters()) { this.Controls.Add(a); a.MouseDown += (s, e2) => { isDown = true; a.ForeColor = Color.Yellow; }; a.MouseUp += (s, e2) => { isDown = false; }; a.MouseHover += (s, e2) => { if (isDown) a.ForeColor = Color.Yellow; }; } 

However, the MouseHover event never fires if the mouse is not held. Also no luck changing MouseHover with MouseEnter . So, I saved the MouseDown and MouseUp and tried to use MouseHover in the form itself:

 private void frmMain_MouseHover(object sender, MouseEventArgs e) { if (isDown) { foreach (Letter l in game.GetLetters()) if (l.ClientRectangle.Contains(l.PointToClient(Control.MousePosition))) l.ForeColor = Color.Purple; } } 

This event also does not work, and I do not understand why he is not shooting and what alternative solutions are. Any advice is appreciated.

+5
source share
2 answers

You can use drag and drop events.

  • Set the AllowDrop property for each control whose purpose is to crash.
  • Handle the MouseDown event for each control that starts with it, and in the call to the DoDragDrop handler events of this control and set the data that you want to drag.
  • Handle the DragEnetr event of each drag target and set e.Effect to determine if drops are allowed or not. Here you can check if removal is allowed, change the color of the back to the desired color.
  • DragLeave knob to reset back color.
  • Hanlde DragDrop and use the GetData method if e.Data to retrieve data and perform actions on deletion.

walkthrough

Example

I have 3 buttons, button1 and button2, and button3 and button2 are the target of the fall. In the code below, I will check if the text that appears on button 2 is the text of button 1, I changed the color of button 2 to green and then to red. also, if you drag the mouse from button 2, I will set the default color back. If you drop, I changed the text of button2 and set the text of button1:

 //Start drag for button 2 private void button1_MouseDown(object sender, MouseEventArgs e) { this.button1.DoDragDrop(this.button1.Text, DragDropEffects.Copy); } //Start drag for button 3 private void button3_MouseDown(object sender, MouseEventArgs e) { this.button3.DoDragDrop(this.button3.Text, DragDropEffects.Copy); } //Check if drop is allowed and change back color private void button2_DragEnter(object sender, DragEventArgs e) { if(e.Data.GetData(DataFormats.Text).ToString()== button1.Text) { e.Effect = DragDropEffects.Copy; this.button2.BackColor = Color.Green; } else { e.Effect = DragDropEffects.None; this.button2.BackColor = Color.Red; } } //Perform drop actions private void button2_DragDrop(object sender, DragEventArgs e) { this.button2.Text = e.Data.GetData(DataFormats.Text).ToString(); } //Reset back color here private void button2_DragLeave(object sender, EventArgs e) { this.button2.BackColor = SystemColors.Control; } 
+3
source

You are looking for various drag and drop events:

https://msdn.microsoft.com/en-us/library/system.windows.forms.control.dragenter(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/ system.windows.forms.control.dragover (v = vs. 110) .aspx

etc...

The problem you are working with is that you are using the wrong events for what you are trying to execute.

0
source

Source: https://habr.com/ru/post/1237792/


All Articles