简体   繁体   中英

Problem while resizing a form - c# winforms

I have a form that contains (among other things) a chat option. I have 2 richtextboxes and a button for sending the messages.

表格

I want that when I resize the form, the messages box will get bigger (or smaller) and that the input box and the button will move down (or up), so they will stay under the messages box.

Here is the code I wrote:

    private void MainForm_SizeChanged(object sender, EventArgs e)
    {
        Rectangle screenRectangle = RectangleToScreen(ClientRectangle);

        int titleHeight = screenRectangle.Top - Top;

        int chatCurrentHeight = richTextBox2.Height;
        int chatNewHeight=Height-titleHeight - richTextBox1.Height - button1.Height - 100;
        richTextBox2.Height = chatNewHeight;
        int heightDiffernce = chatNewHeight - chatCurrentHeight;

        richTextBox1.Location = new Point(richTextBox1.Location.X, richTextBox1.Location.Y+heightDiffernce);
        button1.Location = new Point(button1.Location.X, button1.Location.Y+heightDiffernce);
    }

***richtextbox1 is the input box, and richtextbox2 is the messages box.

It works, but it breaks when the window gets very small (after the size of the messages box is almost 0) and when I minimize the window: The input box and the button disappear.

How can I fix it?

I agree with Idle_Mind. Use a tablelayoutpanel.

I would use two table layout panels. 1. your main container 2. one for your messages and input

Then, use the 'percent' property of the table layout panel on the row which contains your 'messages' control.

Perhaps, 80% for your messages, 20%, for your input. Then, dock both richtextboxes so they stretch grow and shrink.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM