0
Reply

Some Problems with the event handler

eternauta109 eternauta109

eternauta109 eternauta109

May 6 2017 4:26 PM
267
Hi guys,
 
I would to like build a user control, called Cluster, with a little simple feauture:
 
This must to change color if happen a mouse move on with the left click buttun pressed.
 
My achivment is to change color to some closed cluster when i go over it with the mouse while the left click is pressed. 
 
So my problem is that when i leave a control and my left buttun is pressed, i can't intercept the new cluster and not to be abble to opertate on this one, because the event mousedown its still alive. I hope to have explained it in some way.
 
i build all my cluster in the with this code:
 
  1. private void Form1_Load(object sender, EventArgs e)  
  2.         {  
  3.             int app = 0;  
  4.             for (int i = 0; i < panel1.Right; i += 15)  
  5.             {  
  6.                 Cluster cluster = new Cluster();  
  7.                 app += 30;  
  8.                 cluster.Left = app;  
  9.                 cluster.Name = "cluster" + i;  
  10.                 panel1.Controls.Add(cluster);  
  11.             }  
  12.         }  
 and this is my user control Cluster:
 
  1. public Cluster()  
  2.         {  
  3.             InitializeComponent();  
  4.         }  
  5.   
  6.          
  7.         private void Cluster_Load(object sender, EventArgs e)  
  8.         {  
  9.             this.BackColor = Color.Gray;  
  10.         }  
  11.   
  12.           
  13.         private void Cluster_MouseDown(object sender, MouseEventArgs e)  
  14.         {  
  15.             switch (Control.MouseButtons)  
  16.             {  
  17.                 case MouseButtons.Left:                   
  18.                     this.BackColor = Color.Green;  
  19.                     break;  
  20.   
  21.                 case MouseButtons.Right:  
  22.                     this.BackColor = Color.Gray;  
  23.                     break;  
  24.             }  
  25.         }  
  26.   
  27.           
  28.         private void Cluster_MouseMove(object sender, MouseEventArgs e)  
  29.          {  
  30.   
  31.              if (e.X < this.Width)  
  32.              {  
  33.                  (Application.OpenForms["Form1"]).Controls["txtProva"].Text = Convert.ToString(e.X);  
  34.              }  
  35.              else  
  36.              {  
  37.                  this.MouseDown - null// i'll hope to lose the handler with this command  
  38.                  this.MouseDown -= Cluster_MouseDown;  
  39.              }  
  40.              switch (Control.MouseButtons)  
  41.              {  
  42.                  case MouseButtons.Left:  
  43.                      this.BackColor = Color.Green;  
  44.                        
  45.                      break;  
  46.   
  47.                  case MouseButtons.Right:  
  48.                      this.BackColor = Color.Gray;  
  49.                      break;  
  50.              }  
  51.        
  52.         }  
 I really sorry for my English, thank you every body to read this post.
 
Et.