Moving/Resizing components in a form

Dear Experts,

I am looking for a nice way to move and resize components (like a button for example) in a Form.

Any ideas on how to do that in C#?

Sergio Lobo
SergioLoboAsked:
Who is Participating?
 
YurichConnect With a Mentor Commented:
that's an interesting task...

ok, in the scenario below, when you click your button and hold and then move you mouse, it will move along with your mouse and as soon as you release you mouse (mouse_up) it'll settle on the new location:

bool drag = false;
Point point;
private void button1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
      drag = true;
}

private void button1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
      drag = false;
}

private void button1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
      if( drag )
      {
            Point newPoint = PointToClient( Control.MousePosition );

            int xDiff = point.X - newPoint.X;
            int yDiff = point.Y - newPoint.Y;

            button1.Location = new Point( button1.Location.X - xDiff, button1.Location.Y - yDiff );

            point = newPoint;
      }
}

I'm not quite sure about resizing though... do we want ot have this border around the button or how would you resize it at all?? As a matter of fact I got no idea how it was done in Delphi...

regards
0
 
YurichCommented:
I'm not sure about your "nice ways" :)

but what about these ways:

button1.Location = new Point( x, y );

for moving - button1 of course can be anything else, x and y are new coordinates; or more practical:

button1.Location = new Point( button1.Location.X + 10; button1.Location.Y + 20 );

again 10 and 20 can be anything but in this case we don't need to know where we're moving, just how far we're

button1.Size = new Sysem.Drawing.Size( width, height );

where width and height are new dimensions, or again:

button1.Size = new System.Drawing.Size( button1.Size.Width + 20, button1.Size.Height. + 20 );

again we don't need to know how big we're making our button, just how bigger

regards
0
 
SergioLoboAuthor Commented:
Hello Yurich,

I believe I was not clear in my original question. I need to move and resize components with a mouse. For example, imagine that you have a form with just one button. What I would like to have is the possibility to click on this button AND not to fire the OnClick event. Instead, I would be able to drag the button to another position in the form, or resize it. Something very similar to what Delphi IDE allows us to do with visual components.

Kind regards,
Sergio Lobo
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
YurichCommented:
so you basically want your controls to behave on your click as they're in the design mode? But you will want to fire On_Click sooner or later otherwise what's the point of a button, right?
0
 
YurichCommented:
oops,

add this line into your Mouse_Down:

point = PointToClient( Control.MousePosition );
0
 
YurichCommented:
... and following the logic we have a resizing soluction for button2:
when mouse down and dragged, the size will change accordingly:

bool resize = false;
Point point2;
private void button2_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
      resize = true;
      point2 = PointToClient( Control.MousePosition );
}

private void button2_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
      resize = false;            
}

private void button2_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
      if( resize )
      {
            Point newPoint = PointToClient( Control.MousePosition );
            int xDiff = point2.X - newPoint.X;
            int yDiff = point2.Y - newPoint.Y;

            button2.Size = new System.Drawing.Size(
                  button2.Width - xDiff, button2.Height - yDiff );

            point2 = newPoint;
      }      
}

regards,
0
 
YurichCommented:
i really think i've done a good job on that one... the question is answered in full and all coding is provided.

regards,
yurich
0
 
SergioLoboAuthor Commented:
I entirely agree with you Yurich!
Sorry I took so long to acknowledge your work.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.