Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c#/winforms: How can two controls dynamically share available space?

Tags:

c#

winforms

How would I setup my controls for the following situation?:

I have a parent-container for example a GroupBox.

Inside this parent-container I have two similar controls, like for example ListBoxes, next to each other. They both have the same size, so the border between the two of them is exactly in the middle of the GroupBox.

Now when the GroupBox is resized, I want the ListBoxes to also be resized, but the two should always be at the same size than the other one. So also the border between the two of them stays in the middle of the GroupBox.

So, how would I set up the properties for these three controls to achieve my desired behaviours?

like image 867
clamp Avatar asked May 18 '09 12:05

clamp


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


2 Answers

You need another container. The TableLayoutPanel is the best solution. Use 1 row and 2 columns and dock (Dock = Fill) it in the group box. The width of both columns should be set to 50%. Next you can add your controls in the individual cells and dock them (Dock = Fill)

like image 179
Dries Van Hansewijck Avatar answered Sep 30 '22 13:09

Dries Van Hansewijck


Perhaps a SplitContainer with the two-halves set evenly and IsSplitterFixed set to true (to stop the user moving it):

[STAThread]
static void Main() {
    Application.EnableVisualStyles();
    Application.Run(new Form {
        Controls = { new SplitContainer {
            Width = 200,
            IsSplitterFixed = true,
            SplitterDistance = 100,
            SplitterWidth = 1,
            Dock = DockStyle.Fill,
            Panel1 = { Controls = {
                    new ListBox {
                        IntegralHeight = false,
                        Dock = DockStyle.Fill,
                        BackColor = Color.Blue,
                        Items = {"abc","def","ghi"}
                    }
                }
            }, Panel2 = { Controls = {
                    new ListBox {
                        Dock = DockStyle.Fill,
                        BackColor = Color.Red,
                        IntegralHeight = false,
                        Items = {"jkl","mno","pqr"}
                    }
                }
            }
        }}
    });        
}
like image 25
Marc Gravell Avatar answered Sep 30 '22 14:09

Marc Gravell