Reputation: 147
In WPF it has the default support for the vertical alignment for the TextBox control. but in Windows Forms there is no way to set the vertical alignment of the TextBox control.
In My case, I have using the multi-line text box and the text is need to be displayed on the bottom of the TextBox and it need to be maintain the alignment while typing on it.
I have tried to get the line count of the entered text and try to calculate the bounds of the text box based on the length of the text. but the line count is not get properly when editing the text with word wrap.
Can any one help me on this to maintain the vertical alignment of the TextBox while editing?
I have tried to change the location of the textbox using the suggestion given in that thread. when editing the text. When I try to edit the text the bounds is not updated properly and some part of the text is hidden in the text box. I have calculated the bounds based on the font size and text width.
Size textSize = TextRenderer.MeasureText(TextBoxText, this.textBoxControl.Font);
int textBoxTop = this.textBoxControl.Bounds.Top;
int nol = (textSize.Width > this.textBoxControl.Width) ? ((textSize.Width) / this.textBoxControl.Width) + 1 : 1;
{
if (nol > n)
{
n = nol;
rect1 = this.textBoxControl.Bounds;
if (top + (height - nol * textSize.Height) > top)
{
rect1.Y = top + (height - nol * textSize.Height);
rect1.Height = nol * textSize.Height;
this.textBoxControl.Bounds = rect1;
}
else
{
this.textBoxControl.Bounds = rect1;
}
}
else if (nol < n)
{
n = nol;
rect1 = this.textBoxControl.Bounds;
if (rect1.Y + nol * textSize.Height < top + height)
{
rect1.Y += textSize.Height - this.textBoxControl.Margin.Top;
rect1.Height -= textSize.Height;
//this.textBoxControl.Bounds = rect1;
}
if (nol == 1)
{
rect1.Y = top + height - textSize.Height;
rect1.Height = textSize.Height;
//this.textBoxControl.Bounds = rect1;
}
this.textBoxControl.Bounds = rect1;
}
}
Its working fine while editing the text, but in some cases the nol
Line count is calculated wrongly. How can I get the actual line count of the textbox including the wrapped lines.?
Upvotes: 1
Views: 4879
Reputation:
I have created a control that has a TextBox
docked to the bottom of a panel that looks kinda like a TextBox
:
// Make sure you have this.
using System.Linq;
public class BottomAlignTextBox : Panel
{
public BottomAlignTextBox()
{
this.BackColor = Color.White;
this.BorderStyle = (Application.RenderWithVisualStyles) ? BorderStyle.FixedSingle : BorderStyle.Fixed3D;
this.Size = new Size(200, 200);
this.Padding = new Padding(5, 0, 4, 2);
bottomAlignTextBox.Dock = DockStyle.Bottom;
bottomAlignTextBox.Multiline = true;
bottomAlignTextBox.WordWrap = true;
bottomAlignTextBox.AcceptsReturn = true;
bottomAlignTextBox.BorderStyle = BorderStyle.None;
bottomAlignTextBox.Height = 20;
bottomAlignTextBox.TextChanged += delegate
{
if (bottomAlignTextBox.Height < this.Height - 20)
{
if (TextRenderer.MeasureText(bottomAlignTextBox.Text, bottomAlignTextBox.Font).Width > bottomAlignTextBox.Width + 6)
{
string longestLine = bottomAlignTextBox.Lines.OrderByDescending(s => TextRenderer.MeasureText(s, bottomAlignTextBox.Font).Width).First();
bottomAlignTextBox.Text = bottomAlignTextBox.Text.Replace(longestLine, longestLine.Substring(0, longestLine.Length - 1) + Environment.NewLine + longestLine[longestLine.Length - 1]);
bottomAlignTextBox.Height += 19;
bottomAlignTextBox.SelectionStart = bottomAlignTextBox.Text.Length + 2;
bottomAlignTextBox.SelectionLength = 0;
}
}
};
this.Controls.Add(bottomAlignTextBox);
this.Click += delegate { bottomAlignTextBox.Focus(); };
}
public new string Text
{
get { return bottomAlignTextBox.Text; }
set { bottomAlignTextBox.Text = value; }
}
private TextBox bottomAlignTextBox = new TextBox();
}
Upvotes: 1
Reputation: 920
I am not sure if that is entirely possible, but I think you can wrap the control with a Panel control or some sort and Dock it to the bottom of the wrapping Panel control? If the size needs to be dynamic, playing around the Anchor properties should work as well.
Upvotes: 0