Problems Dynamically Moving Controls On WinForm
-
I wrote a small test form that allows the user to click on a control and then move the control to the desired location using mouse down and mouse up events. I have placed a groupbox on the form and then am dynamically creating a combox and placing it in the group box. I can get it to move the control ok, but it does not end up in the position where the user lets up the mouse button. I am using PointToClient but it is not working. Here is the code: public Form1() { InitializeComponent(); ComboBox cb = new ComboBox(); cb.Parent = groupBox; cb.Location = new Point(60, 15); cb.MouseDown +=new MouseEventHandler(cb_MouseDown); cb.MouseUp +=new MouseEventHandler(cb_MouseUp); } private void cb_MouseDown (object sender, MouseEventArgs e ) { Cursor = Cursors.SizeAll; } private void cb_MouseUp (object sender, MouseEventArgs e) { ComboBox cb = ((ComboBox) sender); cb.Location = PointToClient (new Point(e.X, e.Y)); Cursor = Cursors.Default; }
-
I wrote a small test form that allows the user to click on a control and then move the control to the desired location using mouse down and mouse up events. I have placed a groupbox on the form and then am dynamically creating a combox and placing it in the group box. I can get it to move the control ok, but it does not end up in the position where the user lets up the mouse button. I am using PointToClient but it is not working. Here is the code: public Form1() { InitializeComponent(); ComboBox cb = new ComboBox(); cb.Parent = groupBox; cb.Location = new Point(60, 15); cb.MouseDown +=new MouseEventHandler(cb_MouseDown); cb.MouseUp +=new MouseEventHandler(cb_MouseUp); } private void cb_MouseDown (object sender, MouseEventArgs e ) { Cursor = Cursors.SizeAll; } private void cb_MouseUp (object sender, MouseEventArgs e) { ComboBox cb = ((ComboBox) sender); cb.Location = PointToClient (new Point(e.X, e.Y)); Cursor = Cursors.Default; }
it ain't that simple. First of all, mouse events provide locations relative to the top lefthand corner of the control that reports the mouse actions, so any movement you want to accomplish needs at least to do the following:
finalLocation = originalLocation - mouseDownLocation + mouseUpLocation.
Furthermore, you have some functional shortcomings: 1. you probably want to see the drag going on, which means you need MouseMove as well. 2. when the mouse leaves the container, your container no longer gets mouse events, unless you have captured the mouse first. 3. when you want context menus you need to process right-clicks; you then don't want to move the control I guess. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
I wrote a small test form that allows the user to click on a control and then move the control to the desired location using mouse down and mouse up events. I have placed a groupbox on the form and then am dynamically creating a combox and placing it in the group box. I can get it to move the control ok, but it does not end up in the position where the user lets up the mouse button. I am using PointToClient but it is not working. Here is the code: public Form1() { InitializeComponent(); ComboBox cb = new ComboBox(); cb.Parent = groupBox; cb.Location = new Point(60, 15); cb.MouseDown +=new MouseEventHandler(cb_MouseDown); cb.MouseUp +=new MouseEventHandler(cb_MouseUp); } private void cb_MouseDown (object sender, MouseEventArgs e ) { Cursor = Cursors.SizeAll; } private void cb_MouseUp (object sender, MouseEventArgs e) { ComboBox cb = ((ComboBox) sender); cb.Location = PointToClient (new Point(e.X, e.Y)); Cursor = Cursors.Default; }
Don't use location use Left and Top
private void cb\_MouseUp(object sender, MouseEventArgs e) { ComboBox box = sender as ComboBox; box.Left += e.X; box.Top += e.Y; Cursor = Cursors.Default; }
If you're moving it from one container into another, You're going to have to work out which container and then remove control from original container and add to new container. Sounds like fun :)
-
I wrote a small test form that allows the user to click on a control and then move the control to the desired location using mouse down and mouse up events. I have placed a groupbox on the form and then am dynamically creating a combox and placing it in the group box. I can get it to move the control ok, but it does not end up in the position where the user lets up the mouse button. I am using PointToClient but it is not working. Here is the code: public Form1() { InitializeComponent(); ComboBox cb = new ComboBox(); cb.Parent = groupBox; cb.Location = new Point(60, 15); cb.MouseDown +=new MouseEventHandler(cb_MouseDown); cb.MouseUp +=new MouseEventHandler(cb_MouseUp); } private void cb_MouseDown (object sender, MouseEventArgs e ) { Cursor = Cursors.SizeAll; } private void cb_MouseUp (object sender, MouseEventArgs e) { ComboBox cb = ((ComboBox) sender); cb.Location = PointToClient (new Point(e.X, e.Y)); Cursor = Cursors.Default; }
I hate tooting my own horn, but take a look at my article on the subject -> Create your Own Runtime Movable Windows Forms Controls[^].
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
I hate tooting my own horn, but take a look at my article on the subject -> Create your Own Runtime Movable Windows Forms Controls[^].
A guide to posting questions on CodeProject[^]
Dave KreskowiakSorry, I should have provided that link, I was aware of your excellent article on the subject... :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
it ain't that simple. First of all, mouse events provide locations relative to the top lefthand corner of the control that reports the mouse actions, so any movement you want to accomplish needs at least to do the following:
finalLocation = originalLocation - mouseDownLocation + mouseUpLocation.
Furthermore, you have some functional shortcomings: 1. you probably want to see the drag going on, which means you need MouseMove as well. 2. when the mouse leaves the container, your container no longer gets mouse events, unless you have captured the mouse first. 3. when you want context menus you need to process right-clicks; you then don't want to move the control I guess. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
Thanks Luc! This is what I needeed.
-
Sorry, I should have provided that link, I was aware of your excellent article on the subject... :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
No big deal. I just hate it when I see people doing that and they all start by handling the mouse event of the form itself. There's an easier way!
A guide to posting questions on CodeProject[^]
Dave Kreskowiak