Z
Z
ZmeuSnake2016-06-26 06:42:54
Programming
ZmeuSnake, 2016-06-26 06:42:54

How to reduce system load when working with Cursor in MouseMove on dataGridView?

WinForms project, it contains a form with a dataGridView on which the MouseMove event hangs. The form is expanded to full screen. There is also a working timer.
Here I determine the coordinates of the cell over which the mouse cursor is located:

private Point A1;
int cellX;
int cellY;
private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
        {
            cellX = e.X / dataGridView1.Columns[0].Width;
            cellY = e.Y / dataGridView1.RowTemplate.Height;
            this.Text = "cellX: " + cellX + "  cellY: " + cellY;
        }

The load on the processor is minimal.
But in addition to defining the cell, I need the cursor to be able to move only line by line. I'm trying to add the following code to my method:
A1.X = dataGridView1.Left + e.X;
A1.Y = 200 + dataGridView1.RowTemplate.Height/2;
Cursor.Position = A1; //как я понял, тормоза из-за этой строчки.

At the same time, I achieved what I wanted and the cursor moves along one of the rows, but when you hover over the dataGridView, the load on one of the cores in the task manager grows to 100%, and the timer stops until the cursor leaves the table.
Are there any less expensive ways to solve the problem, or can I somehow optimize my method?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
ZmeuSnake, 2016-06-26
@ZmeuSnake

The problem was partially solved by placing the problematic line in if:
if (kk == 3)
{
Cursor.Position = A1;
kk = 0;
}
kk++;

M
maaGames, 2016-06-26
@maaGames

Don't use A1 and write directly to Position. I don’t know C#, but the extra temporary object definitely doesn’t add speed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question