Eric
Eric

Reputation: 1232

How to grow TextBox in WinForm?

In the WinForm I am working on, I have two TextBox widgets and one button between the text boxes. When the form starts, and the winform is at its minimum size, the TextBoxes look as expected. No gaps between the items where they come close to the button there in the center. This is as seen in the top graphic.

However, when the user maximizes the winform, the TextBoxes move to the correct position but do not expand to keep close to the button in the center. Textbox A is anchored to the left and Textbox B is anchored to the right. So the alignment is correct. This is as seen in the bottom graphic.

TextBox example

Now, the question is how to make text box A grow to the right so that it comes close to the button in the middle and text box B grow to the left so that it also comes close to the button?

Thanks! Eric

Upvotes: 2

Views: 1064

Answers (3)

Fabio
Fabio

Reputation: 32445

Use TablelayoutPanel control. From MSDN

Create TableLayoutPanel with one row and three columns
First column: SizeType: percent, 50%
Second column: SizeType: absolute, 75(button width)
Third column: SizeType: percent, 50%

Then put textboxes and button in the columns.
Set for textboxes and button property .Dock = Fill
Size of the textboxes will be automatically changed with size of the columns in the TableLayoutPanel

Set Dock property of the TableLayouPanel to Fill or Bottom or Top. Then TableLayoutPanel will change width when width of the form is changed.

Small form

Large form

Upvotes: 2

György Kőszeg
György Kőszeg

Reputation: 18023

With simple anchors you cannot achieve this. But you can use a TableLayoutPanel:

  • Place a TableLayoutPanel on the form and set Dock to Fill (or Top/Bottom)
  • Make 3 columns.
    • Column 1: Percent, 50%
    • Column 2: Absolute, set the desired size of the Button (eg. 100px)
    • Column 3: Percent, 50%
  • Drop TextBoxA into Column1, and set Anchor Left and Right
  • Drop the Button into Column2, and clear the Anchors
  • Drop TextBoxB into Column3, and set Anchor Left and Right

Without a TableLayoutPanel, you can make the following:

  • TextBoxA: Anchored Left and Right
  • Button and TextBoxB: Anchored Right So only TextBoxA will grow.

Upvotes: 3

Kunal Khatri
Kunal Khatri

Reputation: 471

you can use the Anchor Property for Button and textbox -

Button A - Anchor - left and Right Button B - Anchor - left and Right

you can also, set textbox anchor property as per your needs and control the location of all these three controls.

Upvotes: 0

Related Questions