Binding a TextBox's Height to Parent via Code - c#

I have a TextBox that I'm creating, via code, to insert into a Control in a WPF interface. The Control can be dynamically resized via click-and-drag code, and I want the TextBox's height and width to match the Control's. The end result is that I have a TextBox that I can dynamically resize.
The box is going to be placed in this Border:
<Border Name="borInner" Margin="0,0,2,0" HorizontalAlignment="Center" VerticalAlignment="Center" />
I'm trying to make the textbox fill the area of the border. To do so, I'm assuming 2 things have to happen.
I have to set Horizontal and Vertical Alignments on borInner to Stretch
The TextBox must also have those properties.
Unfortunately, when both of these things are set, the TextBox matches the Width of the overall control, but not the height. It just sits centered vertically still. Does the Height value of a TextBox control just not follow Stretch? If I set a height manually, it will work, but I'd rather not have to manually update the size of the textbox during the mousemove if I can help it, as it's pretty slow to calculate what the new dimensions should be constantly.
For more detail on the Control the TextBox is being inserted into: It has a property I've made called bool FitContentToSize, which makes this code happen on the Control's creation
borInner.VerticalAlignment = VerticalAlignment.Stretch;
borInner.HorizontalAlignment = HorizontalAlignment.Stretch;
I then create the textbox
element = new TextBox() {
AcceptsReturn = true,
TextWrapping = TextWrapping.Wrap,
VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
HorizontalScrollBarVisibility = ScrollBarVisibility.Auto
};
and add that TextBox to borInner.
What am I missing, or does anyone have a decent idea for a workaround?

Try setting the height of the textbox to Double.NaN and then setting the margin to 0 all around.

Make sure your inner border (borInner) is not inside something like a StackPanel, or anything else that limits the height of its children to the bare minimum. If this is the case, consider using something like a Grid, or DockPanel.

Related

Label Text not displayed due to less width of its container

I have a label in panel. The width of text in the label is more than its container panel. Because of that the text in the label is not coming completely.
I have tried this.label1.Dock = DockStyle.Top; and this.label1.Dock = DockStyle.Fill; but both aren't working. Is there any way to solve this problem?
The label is in a TableLayoutPanel which is in panel. And I want to show the text completely in the first row only. Making AutoSize true of panel is causing other data to move from their position. Which shouldn't happen.
When hosting controls in TableLayoutPanel, you can to set ColumnSpan for your controls.
Column spanning is often useful for positioning a control that is
considerably wider than its peers.
Select the LinkLabel at designer and in properties set ColumnSpan to 3. Also set AutoSize property of it to true:
For more information see:
How to: Span Rows and Columns in a TableLayoutPanel Control

Change ScrollBar width in ScrollViewer programatically

When I create a new ScrollViewer, I need to modify the size of the ScrollBars in the ScrollViewer (change the VerticalScroll width and the HorizontalScroll height), programatically.
I tried the following based in a solution found in SO, without success:
public static ScrollViewer CreateScrollViewer()
{
ScrollViewer result = new ScrollViewer();
// I need to set the scroll width and height here
// I tried the following with no success
result.Resources.Add(SystemParameters.VerticalScrollBarWidth, 100);
return result;
}
I saw some solutions to change them, but all are XAML based. I need to do it in runtime, in pure C#. How could I do this?
You can access the ScrollBars from the ScrollViewer's ControlTemplate. You can find out how to do that from the How to: Find ControlTemplate-Generated Elements page on MSDN and you can find details of the default ControlTemplate in the ScrollViewer Styles and Templates page on MSDN, but in short, try this:
ScrollViewer scrollViewer = new ScrollViewer();
scrollViewer.ApplyTemplate();
ScrollBar scrollBar =
(ScrollBar)scrollViewer.Template.FindName("PART_VerticalScrollBar", scrollViewer);
You can do the same for the horizontal ScrollBar which is named PART_HorizontalScrollBar.

How to reserve space in Win Form TableLayoutPanel control

I have a question about WinForm TableLayoutPanel control. For example, if I hide the control in the column. it will change the size.
Is there a way to not paint the control in a tablelayoutpanel with column autosize so that the column will still have the control size?
Using Control.Visible = false will make the column width 0.
I need something like hidden in WPF Grid.
You can place a Panel in the TLP and place the control on the panel. Set the panel's background color to the same as the TLP, and it will be invisible. Then hide the control but leave the panel.
This should work fine if your control is a fixed size. If the control size varies, it might be a bit more tricky to do this. You will need to vary the size of the Panel too. Setting it to AutoSize = true and AutoSizeMode = GrowOnly might work.

How to fix this issue that some of the text of the label has been hidden by textbox?

The text of the label is written programmatically:
public Form1()
{
InitializeComponent();
label.Text = data from database;
}
You could set the Dock property each of the controls. Depending on your layout you can set each of them to DockStyle.Left and set the AutoSize property of the label to true. If you can't dock them as is, you can put them inside of a panel and dock inside of the panel. When inside of the panel you can also take advantage of the Fill style of docking (which would also work outside of the panel, but depending on the rest of the controls in your layout it could screw them up. Inside of a panel you can set the label to DockStyle.Left and the TextBox to DockStyle.Fill (to take up the rest of the space)
Set the label's MaximumSize.Width property so it cannot overlap the TextBox. If you don't have enough space vertically then also set the MaximumSize.Height property. You then should also consider setting AutoEllipsis to True so that it is obvious to the user that the text got truncated, a tooltip shows the full text.
An easy way to determine the proper values for MaximumSize is to temporarily turn AutoSize off. Adjust the label size to the maximum size that doesn't overlap anything. Copy/paste the Size into AutoSize. Or leave it off.

change the height of statusStrip

I have a simple windows form with a statusStrip in VS2010, and no matter what I tried, the height of statusStrip does not change, what is proper way of changing the height??
thanks
I just changed the StatusStrip size without problems...
Create a new Form.
Create a StatusStrip.
Set its property "Dock" to "None".
Set its property "Autosize" to "False".
Set its property "Size" with Height = 'the height you need' (I have put it to 100).
If you want, now you can Dock to "Bottom" again.
Add a ProgressBar: it will be 100 Height.
Let me know if this works for you
None of the mentioned approaches worked. Eventually this is the working solution for us:
First, use a TableLayoutPanel with a row at 100% and bottom row with absolute height of 24 (or any desired height you want).
Second, drag and drop the ToolStrip control into the bottom row, and setting the Dock property to Fill. This forces the ToolStrip control to fill the bottom cell of the TableLayoutPanel. Now you have a toolstrip with height 24.

Categories