How to grow TextBox in WinForm? - winforms

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.
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

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.

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.

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.

Related

C# Winforms: GroupBox not resizing

I have two GroupBoxes on the left side of one of my TabControls, call them GroupBox A (top left), and GroupBox B (bottom left). The GroupBoxes do not resize like I would hope.
Example:
When I resize the main form that has the TabControls with my mouse, or maximize it, or move it to a screen with lower resolution, GroupBox B keeps its width / height. This causes GroupBox B to draw over GroupBox A, kind of like a 'always on top' effect.
Desired:
Would like both GroupBoxes to resize according to one another / proporitionally and fit the area they are given.
Ideas?
A tableLayoutPanel can help with this.
Add a tableLayoutPanel and size it to fit your needs.
Anchor the table to Top, Bottom, Left, and Right
Put GroupBoxA into the upper left cell of the table
Put GroupBoxB into the lower right cell of the table
For both GroupBox size them accordingly and anchor them to all 4 sides.
Now they will grow and shrink proportionately with the app.
Additionally you can add more of your controls to the table. If you need a control to span multiple rows or columns use the RowSpan/ColumnSpan property.
If your GroupBox is inside another control like a tab or something, then do as below:
In my case I had a GroupBox inside a tab and I called the below methods in the InitializeComponent() method to force the Groupbox to adjust to the tab size.
this.groupBox4.ResumeLayout(false);
this.groupBox4.PerformLayout();
this.tabPage2.ResumeLayout(false);
this.tabPage2.PerformLayout();
And If you have multiple GroupBoxes, you need set the anchoring accordingly.
I faced a similar problem, I just had used the split container for both GroupBoxes, anchored the split container at top, bottom, left an right of my main form, and both GroupBoxes too, at top, bottom, left and right of their containers.
Slightly off topic from the original question, but my issue was that my MaximumSize field of my Group Box was not set, or was too small so I could not resize the height!
Altought the question is pretty old, someone might still find it useful...
I had the same problem and found a working solution -> instead of changing the width/height of the GroupBox, change its minimum width/height in the Form's resize method
edit: fixed typo
private void Form1_Resize(object sender, EventArgs e) {
groupBox1.MinimumSize = new Size(this.Width /2, this.Height);
}

WPF - overlay text on top of datagrid row

I have a WPF datagrid where certain rows will be disabled. In addition to disabling the cell contents, I'd like to present to the user some text that overlays the row with an explanation such as "Row disabled due to ...". Basically the same effect of having a label control with a lower z-order sitting directly in front of the datagrid row's cells.
If cell text spillover was possible (like in Excel), I could create a text column of 0 width with the message, then let the message runover through the neighboring cells. But to my knowledge this type of text spillover isn't possible in the datagrid.
If your rows have fixed height, you could add a another transparent datagrid on top of the other datagrid. The transparent datagrid would have one column which would show the disabled explanation. Simple but hacky solution.
The proper way would be to use adorners.

Winforms - can't resize when adding textbox

I'm putting together a simple winforms app, which consists on 1 form and a textbox.
I want to have the textbox scale in width when the form is scaled by the user.
By default the textbox is a fixed width and anchored at the top & left. The form scales ok, but not the textbox.
If I apply left, top & right anchor to the textbox it only scales the textbox & form up and I can't shrink it.
Do I need to dynamically set the width of the textbox to that of the form?
I've been trying to recreate this for 5 minutes and it resizes fine for me, and I've only been able to find one set of combinations that reproduces the behavior you describe:
Is the textbox inside a panel with Autosize set to "true" and AutoSizeMode set to "grow only" perhaps? That's the only way I've been able to duplicate the behavior.
Here's a screenshot of the properties set this way. My textbox is also set to anchor at top, left, right. Again, this is the ONLY way I've been able to duplicate the behavior you're describing, so hopefully this helps.

Windows forms app, autoscale controls with form

I'm a newbie. Designing a form that can be resized, and I want my textboxes, labels and buttons to resize with the form, can someone tell me how to do this?
It depends on the type of layout you need. The "basic tools" you have to do that are following properties: Anchor and Dock.
Anchor
With the Anchor property you "attach" a side of an element to a side of its container. For example if you place a button in the bottom-right corner of a window and you set "Bottom, Right" as Anchor then when you'll resize the form the button will keep its relative position to that corner.
Now imagine you place a multiline text-box in the form, resize as needed (for example 4 px from top, left and right border and 128 px height) and set the Anchor property to "Left, Top, Right". When you'll resize the form that control will keep its height but it'll resize to keep its margins (so if you'll make the form wider its width will be increased).
Dock
Dock is different. With docking you "say" to the Layout Manager to use all available space in one direction. For example if you set to Left then your control will keep its width but it'll use all the available height and its location will be most left as possible.
You may have more than one control docked in a container, imagine you have 5 textbox with Top docking inside a form. They'll be stacked to the top of the form using all the width (and resizing). Another example: a Top docked control (as a banner) and a "Fill" docked control (as main content). Remember that with docking the order of controls matters (if you first place the "Fill" control it'll use ALL the available space and the "Top" dock control will overlap).
Even more
Moreover you have some layout controls too (tables and stacks). They're really easy to use and a 30 minutes of "experiments" will clarify much better than a long text.

Spreading controls to fill space as a dialog resizes

I have a dialog with column down the right side filled with buttons. The dialog is built with Windows Forms. I have a mockup at the following link:original dialog
(I would have included it but apparently i'm not allowed to use image tags)
I would like for the buttons in the right column to resize themselves to fill the remaining vertical space when the dialog resizes. It doesn't particularly matter to me whether or not buttons simply increase in size or whether the buttons remain the same size while the gaps between them increase. I'm simply want the buttons to go from the top to the bottom. (I have a mockup for this as well but apparenlty i can only include one link)
I've tried hosting the buttons in a FlowLayoutPanel but they do not increase as the dialog stretches, I only get whitespace at the bottom after I run out of buttons. I also tried a TableLayoutPanel and had the same result but I may have misused it. Does anyone have any ideas how I could accomplish this?
Thanks in advance,
Jeremy
To get you started. Use the TableLayoutPanel, set its Anchor property to top, bottom, left, and right. Set the rows and columns to percentages as needed. I suggest each control have it own cell. Note that each control in a "cell" can have its Dock and Anchor property set as needed.
You can do this with a TableLayoutPanel. Create a column for the buttons, with each button having it's own row / cell in the column. Set each row to be an even percentage for height (if there are 10 buttons, each row would be 10%), and dock the TableLayoutPanel to the right side of the screen. Then, put the buttons into their rows and set them to full docking. Then, when the dialog expands, the TableLayoutPanel will expand to fill the entire right side of the screen, each row will adjust proportionally, and each button would expand to fit the new row size.
You may have to adjust this a bit to fit your needs, especially in how it relates to the other content in the window.

Resources