How to move Multiple Controls at a time at runtime in C#

csharp India
  • 14 years ago

    Hi,

     

    I am working on C# windows application.

    In my application we want to move the multiple controls on the form at a time at runtime.

    I am able to move single control at runtime.

    How to move Multiple controls at runtime?

    Let me know if any details required?

  • 14 years ago
    Hi,

    Plz let me know how to move at least single control on the form at runtime.

    Plz help me.









  • 14 years ago
    I never use things like this
    can u plz try this

    http://www.developerfusion.co.uk/show/2692/1/
    check this also: http://www.developerfusion.co.uk/forums/thread/139444/#139444





  • 14 years ago
    Hi,
     Let me know how to move a single control at run time.

    Suppose at the run time, on the form I have one Text box control and I want to change the position of the Text box at run time only. i.e. by using mouse I'll drag the Text box and drop it on the form where I want.(but at run time).

    plz give me some idea or sample of code.

    Thanx












  • 14 years ago
    Hi Sandhya,

    This code is working for me, try and let me know
    put a text box with name as textBox1 and proceed.

    bool Dragging;
            int mousex;
            int mousey;

            private void textBox1_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    Dragging = true;
                    mousex = -e.X;
                    mousey = -e.Y;
                    int clipleft = this.PointToClient(MousePosition).X - textBox1.Location.X;
                    int cliptop = this.PointToClient(MousePosition).Y - textBox1.Location.Y;
                    int clipwidth = this.ClientSize.Width - (textBox1.Width - clipleft);
                    int clipheight = this.ClientSize.Height - (textBox1.Height - cliptop);
                    Cursor.Clip = this.RectangleToScreen(new Rectangle(clipleft, cliptop, clipwidth, clipheight));
                    textBox1.Invalidate();
                }
            }

            private void textBox1_MouseMove(object sender, MouseEventArgs e)
            {
                if (Dragging)
                {
                    Point MPosition = new Point();
                    MPosition = this.PointToClient(MousePosition);
                    MPosition.Offset(mousex, mousey);
                    textBox1.Location = MPosition;
                }
            }

            private void textBox1_MouseUp(object sender, MouseEventArgs e)
            {
                if (Dragging)
                {
                    Dragging = false;
                    Cursor.Clip = System.Drawing.Rectangle.Empty;
                    textBox1.Invalidate();
                }
            }      

    Thanks and Regards
    Amjath
















































  • 14 years ago

    Can you put all the controls you want to move into a container, such as a group box and move that?

     

  • 14 years ago
    So You are looking to create and editing invierment like Vishual Studio?
  • 13 years ago

    Hi Dear, The code which you have given is working fine for single textbox. What about multiple textbox with different ID?

    Here is my code

    public bool Dragging;

    int mousex;

    int mousey;public Form2()

    {

    InitializeComponent();

    foreach (Control ctl in this.Controls)

    {

    if (ctl.GetType() == typeof(Button))

    {

     

    Button btn = (Button)ctl;

     

    btn.MouseDown +=
    new MouseEventHandler(buttonN_MouseDown);

    btn.MouseMove += new MouseEventHandler(buttonN_MouseMove);

    btn.MouseUp += new MouseEventHandler(buttonN_MouseUp);

    }

    }

    }

    private void buttonN_MouseDown(object sender, MouseEventArgs e)

    {

    Button btn = (Button)sender;if (e.Button == MouseButtons.Left)

    {

    Dragging =
    true;

    mousex = -e.X;

    mousey = -e.Y;

    int clipleft = this.PointToClient(MousePosition).X - btn.Location.X;

    int cliptop = this.PointToClient(MousePosition).Y - btn.Location.Y;

    int clipwidth = this.ClientSize.Width - (btn.Width - clipleft);

    int clipheight = this.ClientSize.Height - (btn.Height - cliptop);

    Cursor.Clip = this.RectangleToScreen(new Rectangle(clipleft, cliptop, clipwidth, clipheight));

    //Cursor.Clip = this.RectangleToScreen(new Rectangle(52, 254, clipwidth, clipheight));

    btn.Invalidate();

    }

    }

    private void buttonN_MouseMove(object sender, MouseEventArgs e)

    {

    Button btn = (Button)sender;if (Dragging)

    {

    Point MPosition = new Point();

    MPosition = this.PointToClient(MousePosition);

    MPosition.Offset(mousex, mousey);

    foreach (Control ctl in this.Controls)

    {

    if (ctl.GetType() == typeof(Button))

    {

    btn.Location = MPosition;

    }

    }

    }

    }

    private void buttonN_MouseUp(object sender, MouseEventArgs e)

    {

    Button btn = (Button)sender;if (Dragging)

    {

    Dragging = false;

    Cursor.Clip = System.Drawing.Rectangle.Empty;

    btn.Invalidate();

    }

    }

     

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Memory is like an orgasm. It's a lot better if you don't have to fake it.” - Seymour Cray