Disable action on mouse move C#

Asked

Viewed 304 times

0

I have a problem in my code, I have some actions on mouse moves in C# and would like to disable them. Whenever I drag the mouse to the listbox of my form, it already does the action, I would like to leave this on the mouse click, but I’m having difficulties, follow the code in case someone can help I appreciate.

private void lstColunasLayout2_MouseMove(object sender, MouseEventArgs e) {
  int index = lstColunasLayoutSecundario.IndexFromPoint(new Point(e.X, e.Y));
  ListBox lb = sender as ListBox;
  if (index >= 0 && lb_item != null) {
    lb.SelectedIndex = index;
    AtualizaRelacaoColunas(lb);
    lb_item = null;
  }
}
  • 1

    Just to clarify, you want that same code in the listbox click event?

  • It doesn’t have to be exactly the same, I would just like to know how to transpose this code to the click.

  • 1

    You can accept an answer if you have satisfied your problem :)

1 answer

1

Remove the association between the event MouseMove listbox and the method lstColunasLayout2_MouseMove.

To do so, delete the following line from your code:

    this.lstColunasLayout2.MouseMove += new System.Windows.Forms.MouseEventHandler(this.lstColunasLayout2_MouseMove);

If you used Visual Studio to create the form, this line of code will probably be inside the file "[Nomeform].Designer.Cs"

Another way to do this, if you’re using Visual Studio, would be:

  • Open Form
  • Selecting the Listview
  • In the Properties window, click the events button (button with the radius icon)
  • Delete content from Mousemove event
  • Solved friend, stopped performing the action when drag, transfer and changed the code inside the click, and now just click to work. Grateful.

  • Transferring code from one event to another will work but it would be interesting to disable the event that you will no longer use to prevent code degradation or possible errors in the future.

  • 1

    If you have solved the problem, please tick as the correct answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.