none
I have done some dynamically textboxes and dont want them to appear outside of the form. RRS feed

  • Question

  • I have done some dynamically textboxes. The problem is that when this textboxes appear. It changes a buttons location and then the button appears outside the form
    Here is my code! It dosent work right now..
                    if (CreateBtn.Location.Y + CreateBtn.Height < textBoxes.Location.Y + textBoxes.Height || CreateBtn.Location.Y + CreateBtn.Height < textBoxes2.Location.Y + textBoxes2.Height)
                    {
                        CreateBtn.Location = new Point(CreateBtn.Location.X, textBoxes.Location.Y + textBoxes.Height + 15);
                    }
                    if (Location.Y + Height < CreateBtn.Location.Y + CreateBtn.Height)
                    {
                        Height = CreateBtn.Location.Y + CreateBtn.Height + 50;
                    }

    • Moved by CoolDadTx Monday, November 18, 2019 2:47 PM Winforms related
    Saturday, November 16, 2019 8:48 AM

Answers

  • Hi Carlo,

    Thank you for posting here.

    According to your description, this is a question about the height and boundaries of the form.

    I think the reason of the error is winforms form titlebar height, which has a value of 30.

    So even if CreateBtn has disappeared, the value of CreateBtn.Location.Y + CreateBtn.Height may still be less than Height.

    I made some changes to your code, and now the code is working.

            private void Button1_Click(object sender, EventArgs e)
            {
                textBox1.Visible = true;
                if (CreateBtn.Location.Y + CreateBtn.Height < textBox1.Location.Y + textBox1.Height || CreateBtn.Location.Y + CreateBtn.Height < textBox2.Location.Y + textBox2.Height)
                {
                    CreateBtn.Location = new Point(CreateBtn.Location.X, textBox1.Location.Y + textBox1.Height + 15);
                }
                if (Height - 30 < CreateBtn.Location.Y + CreateBtn.Height)
                {
                    Height = CreateBtn.Location.Y + CreateBtn.Height + 50;
                }
            }
    

    Result: 

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Carlo Goretti Monday, November 18, 2019 9:04 AM
    Monday, November 18, 2019 5:45 AM

All replies

  • Hello,

    Try using PointToClient.

    CreateBtn.Location = this.PointToClient(new Point(CreateBtn.Location.X, textBoxes.Location.Y + textBoxes.Height + 15));


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, November 16, 2019 2:18 PM
    Moderator
  • Thanks but it didnt work.. Any other ideas? My way have worked in other projects..


    Saturday, November 16, 2019 3:46 PM
  • The problem is with the other IF case.. Any ideas there @karen?
    Sunday, November 17, 2019 3:23 PM
  • Hi Carlo,

    Thank you for posting here.

    According to your description, this is a question about the height and boundaries of the form.

    I think the reason of the error is winforms form titlebar height, which has a value of 30.

    So even if CreateBtn has disappeared, the value of CreateBtn.Location.Y + CreateBtn.Height may still be less than Height.

    I made some changes to your code, and now the code is working.

            private void Button1_Click(object sender, EventArgs e)
            {
                textBox1.Visible = true;
                if (CreateBtn.Location.Y + CreateBtn.Height < textBox1.Location.Y + textBox1.Height || CreateBtn.Location.Y + CreateBtn.Height < textBox2.Location.Y + textBox2.Height)
                {
                    CreateBtn.Location = new Point(CreateBtn.Location.X, textBox1.Location.Y + textBox1.Height + 15);
                }
                if (Height - 30 < CreateBtn.Location.Y + CreateBtn.Height)
                {
                    Height = CreateBtn.Location.Y + CreateBtn.Height + 50;
                }
            }
    

    Result: 

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Carlo Goretti Monday, November 18, 2019 9:04 AM
    Monday, November 18, 2019 5:45 AM
  • Thanks! That worked!
    Monday, November 18, 2019 9:04 AM