c#Windows窗体中元素的相对位置

时间:2013-09-08 11:19:30

标签: c# winforms relativelayout splitter

我尝试使用SpliterContainer和带按钮的控制面板创建包含2列的简单应用程序。我想在每个屏幕上看起来都很棒。这就是为什么我决定使用元素的相对位置。

我阅读了文档和不同的论坛,但我得到了一些奇怪的东西。第二列分离器根本没有出现。

Result

拜托,你能帮我找到那个问题的原因吗?

private void Form1_Load(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Maximized;

            int screenWidth = Screen.PrimaryScreen.Bounds.Width;
            int screenHeight = Screen.PrimaryScreen.Bounds.Height;

            //set form size
            this.Size = new Size(screenWidth, screenHeight);

            //set button panel size
            const double percentOfHeightPanel = 0.05;
            int heightOfPanelButton = Convert.ToInt32(screenHeight * percentOfHeightPanel);
            this.panel_button.Size = new System.Drawing.Size(screenWidth, heightOfPanelButton);
            this.panel_button.Location = new Point(0, 0);


            //set splitContainer size
            int widthOfContainer = Convert.ToInt32(0.5 * screenWidth);
            int heightOfContainers = Convert.ToInt32(screenHeight * (0.95));
            splitContainer1.Panel1.MinimumSize = new Size(widthOfContainer, heightOfContainers);
            splitContainer1.Panel2.MinimumSize = new Size(widthOfContainer, heightOfContainers);
            splitContainer1.Location = new Point(0, heightOfPanelButton);
            //this.splitContainer1.Panel2MinSize = screenWidth - widthOfContainer;

            //set textBox size                
            this.textBox1.Multiline = true;
            this.textBox1.Location = new Point(0, heightOfPanelButton);
            this.textBox1.MinimumSize = new System.Drawing.Size(widthOfContainer, heightOfContainers);

            this.textBox2.Multiline = true;
            this.textBox2.Location = new Point(widthOfContainer, heightOfPanelButton);
            this.textBox1.MinimumSize = new System.Drawing.Size(widthOfContainer, heightOfContainers);
        }

1 个答案:

答案 0 :(得分:1)

如果你想要两个有两个相同尺寸的分割面板

splitContainer1.SplitterDistance = 
                          (splitContainer1.Width - splitContainer1.SplitterWidth) / 2;

然后设置

splitContainer1.IsSplitterFixed = true;

您可以在设计时手动设置这两个属性。然后,用户将无法调整面板的大小,面板将自动调整大小以使其大小相同。

请考虑使用TableLayoutPanel

如果进一步,双方应该看起来相同,将控件放在UserControl上,并将两个实例放入两个面板,并将停靠属性设置为Fill