I am developing a Windows Form Application. I want to Align the Text to Center or say to Right of Title Bar of Form. How can I do it ??
It can be done with custom form - you will have to create your own title bar. See V4Vendettas comment;
Other approach (link) - is to create your own handler for form resize and insert there followong code. It will add appropriate amount of spaces from the left size of text. However you will have to add form.Refresh() and call that method in form.Load; also your window will have "..." as a text in task bar.
private void UpdateTextPosition()
{
Graphics g = this.CreateGraphics();
Double startingPoint = (this.Width / 2) - (g.MeasureString(this.Text.Trim(), this.Font).Width / 2);
Double widthOfASpace = g.MeasureString(" ", this.Font).Width;
String tmp = " ";
Double tmpWidth = 0;
while ((tmpWidth + widthOfASpace) < startingPoint)
{
tmp += " ";
tmpWidth += widthOfASpace;
}
this.Text = tmp + this.Text.Trim();
}
If it's a text that's inside a control (Like a label), you can edit the property named "TextAlign" to accomplish what you want inside Windows Form Designer.
Or programmatically,
Label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
If you're talking about aligning the text of the title bar, there is no way to do this through Winforms. You will have to use something like this: http://www.codeproject.com/Articles/93959/WinForm-Extended
Inside Windows Form Designer. Make the changes like this.lable1.AutoSize = false
;
Then:
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
Graphics g = this.CreateGraphics();
double fw = this.Width; // form width
double tw = g.MeasureString(this.Text.Trim(), this.Font).Width; \\ text width
double rp = (fw - tw) / 2;
int tt = Convert.ToInt32(rp);
string st = " ";
st = st.PadRight(tt / 3);
this.Text = st + this.Text.Trim();