How to make an object stay in the same position relative to another object

Yairk_kaufmann 1 Reputation point
2021-03-10T11:52:03.073+00:00

I have a text box and I want to leave it in the same place relative to the image

76353-ezgif-4-180b3dd786a2.gif

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,466 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 71,616 Reputation points
    2021-03-10T12:37:52.263+00:00

    One of the ways is with a FlowLayoutPanel

    A test with
    Anchor : Bottom, Right
    and
    Margin 50; 50; for the TextBox
    =>

    76246-flowlayoutpanel.gif

    1 person found this answer helpful.
    0 comments No comments

  2. Castorix31 71,616 Reputation points
    2021-03-10T13:57:05.557+00:00

    You can also override WM_SIZING .
    Random test =>

    private void MoveSize()
    {
        // Resize/Move controls as you want     
        pictureBox1.Left = (this.ClientSize.Width - pictureBox1.Width) / 2;
        pictureBox1.Top = (this.ClientSize.Height - pictureBox1.Height) / 2;
        // textBox1 relative to pictureBox1
        textBox1.Left = pictureBox1.Left + 100;
        textBox1.Top = pictureBox1.Bottom + 20;
    }
    
    public const int WM_SIZING = 0x0214;
    
    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        if (m.Msg == WM_SIZING)
        {
            MoveSize();
            m.Result = (IntPtr)1;
        }
    }
    
    1 person found this answer helpful.
    0 comments No comments