简体   繁体   English

调整表单大小时出现问题 - c#winforms

[英]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. 我有2个richtextbox和一个用于发送消息的按钮。

表格

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. *** richtextbox1是输入框,richtextbox2是消息框。

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. 它可以工作,但是当窗口变得非常小时(在消息框的大小几乎为0之后)以及当我最小化窗口时它会中断:输入框和按钮消失。

How can I fix it? 我该如何解决?

I agree with Idle_Mind. 我同意Idle_Mind。 Use a tablelayoutpanel. 使用tablelayoutpanel。

I would use two table layout panels. 我会使用两个表布局面板。 1. your main container 2. one for your messages and input 1.你的主要容器2.一个用于你的消息和输入

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. 也许,80%的消息,20%,您的输入。 Then, dock both richtextboxes so they stretch grow and shrink. 然后,停靠两个richtextbox,以便它们伸展和缩小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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