Excel like Selection?
-
I might have asked this before, but I never got any good answer. How do I implement an excel like selection? I have a collection of many controls arranged in lines and rows, much like excel cells. The user i supposed to select them like cells in Excel, but it just won't work. The controls' MouseDown and MouseEnter event apparently cause the control to own the mouse and make it impossible to fire events on any other control as long as the mouse button is held down. I also got this code on msdn, but it's utter rubbish. It will cause a single control to flicker between selected and unselected madly, it's not possible to select multiple controls and the control isn't drawn while you hold the mouse over it.
Point start;
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
start = e.Location;
}private void pictureBox\_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { Size selSize=new Size(e.X - start.X, e.Y - start.Y); Rectangle selRect = new Rectangle(start, selSize); selRect.Offset(((Control)sender).Location); foreach (Control ctl in this.flowLayoutPanel1.Controls) { if (ctl.Bounds.IntersectsWith(selRect)) { // Here goes your code for doing whatever you need to "select" // the picture box... ctl.BackColor = Color.Yellow; } else { // Here goes your code for doing whatever you need to "deselect" // the picture box... ctl.BackColor = Color.Gray; } } } }
So, does anyone have a solution or at least a hint? Tanks a lot!
-
I might have asked this before, but I never got any good answer. How do I implement an excel like selection? I have a collection of many controls arranged in lines and rows, much like excel cells. The user i supposed to select them like cells in Excel, but it just won't work. The controls' MouseDown and MouseEnter event apparently cause the control to own the mouse and make it impossible to fire events on any other control as long as the mouse button is held down. I also got this code on msdn, but it's utter rubbish. It will cause a single control to flicker between selected and unselected madly, it's not possible to select multiple controls and the control isn't drawn while you hold the mouse over it.
Point start;
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
start = e.Location;
}private void pictureBox\_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { Size selSize=new Size(e.X - start.X, e.Y - start.Y); Rectangle selRect = new Rectangle(start, selSize); selRect.Offset(((Control)sender).Location); foreach (Control ctl in this.flowLayoutPanel1.Controls) { if (ctl.Bounds.IntersectsWith(selRect)) { // Here goes your code for doing whatever you need to "select" // the picture box... ctl.BackColor = Color.Yellow; } else { // Here goes your code for doing whatever you need to "deselect" // the picture box... ctl.BackColor = Color.Gray; } } } }
So, does anyone have a solution or at least a hint? Tanks a lot!
Why is your mouse move firing for movement only in a picture box ? This code looks pretty logical to me.
Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp