Have an instruction like this:
this.toolStripStatusLabel1.Text =
this.toolStripStatusLabel1.Text + "; OS=" +
System.Environment.OSVersion.ToString();
If I concatenate OS with something that is more than 10 characters text is blank instead of being truncated. Is this a Bug ?
Check your font size
Ensure that the autosize is set to true
As suggested by Henk, check width of the label
If you pause your program (place a breakpoint right after this happening), what does it say the text is? Empty, or is it still the complete string?
Btw, you can use += instead of repeating the name of the label.Text after the =-sign.
Related
I have a simple StatusStrip with one ToolStripStatusLabel in it. Text in label can be quite long, so I have prefered to display it cutted.
I have set ToolStripStatusLabel properties: Spring=true and TextAlign=MiddleLeft. I didn't want to set StatusStrip's property LayoutStyle = ToolStripLayoutStyle.Flow, because with ToolStripLayoutStyle.Flow the Text will be overridden with triangle(for resizing).
The Text property is set directly after InitializeComonents() and is displayed as expected - cutted.
If I do minimize(to taskbar) and then restore the window, the text will not be displayed at all. If I make window wider I can see the text, and if I bring the window to the initial size I still can see the text, cutted as expected.
I have and will post my solution, but I would ask you, whether you have any elegant one?
The solution I have found is quite simple. Just handle the window's restore event(there is no such event, but there is a workaround:
Is there an event raised in C# when a window is restored?) and reset the text in ToolStripStatusLabel:
var txt = tslabel.Text;
tslabel.Text = " ";
tslabel.Text = txt;
I have a label, let's call it lblText.
Here's what's happening. I set the text:
lblText.text = "Some label stuff here."
And then it displays as .Some label stuff here. I tried adding a space (Some label stuff here.), a non-breaking space, and even an underscore(Some label stuff here._), but instead it just displayed .Some label stuff here or _.Some label stuff here. This is all showing on the same line.
I tested this with an event, which executes MessageBox.Show(lblText.Text.Substring(0,1)). This is showing that string starts with S. I'm at a total loss why this is happening, and it makes even less sense to me that adding an underscore after the period places it before at the start. Anyone have any hints?
I'm using VS 2005 (it's legacy code).
You have the 'RightToLeft' property turned on in your label. Turn it off and the full stop will be in the right place.
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.righttoleft%28v=vs.110%29.aspx
In regards to single-line textboxes (Multiline property is set to false), is it possible to scroll to the end of the line when the text length exceeds the horizontal size of the box?
I have tried various solutions that work for multi-line boxes, but none of them have worked thus far.
Very similar questions have been asked by several individuals in the past, but it has always regarded Multi-Line textboxes. The questions/solutions that I have come across on SO are the following:
Scroll to bottom of C# TextBox
How do I automatically scroll to the bottom of a multiline text box?
Right now I have the following code (which seemingly does not work):
PathText.Text = "";
PathText.AppendText(BrowseDialog.SelectedPath);
PathText.SelectionStart = PathText.TextLength;
PathText.ScrollToCaret();
PathText.Refresh();
PathText is the textbox in use, and BrowseDialog is a FileDialog.
Any suggestions are greatly appreciated.
You could do something like this:
PathText.Focus();
PathText.Select(PathText.Text.Length, 0);
textBox1.Select(textBox1.Text.Length, 0);
// call focus
textBox1.Focus();
OR
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();
textBox1.Focus();
I want to get the current cursor position from a WPF TextBox. If a TextBox contains text abhishek and cursor is blinking after abhi then i want that index, so that later after clearing the TextBox programmatically and assigning some other or same text programmatically I want to make the cursor blink just after 4 characters.
I have tried get cursor position like this,
_tempFuncName = txtFunctionName.Text;
_cursorPosition = txtFunctionName.SelectionStart;
_selectionLength = txtFunctionName.SelectionLength;
And set back at some later stage from other event like this,
txtFunctionName.Text = _tempFuncName;
txtFunctionName.SelectionStart = _cursorPosition;
txtFunctionName.SelectionLength = _selectionLength;
Here underscore variables are page level variables.
This code is not working. Is there some other approach?
You can play with caretindex property of a text box
//You can set this property on some event
NumberOfDigits.CaretIndex = textbox.Text.Length;
You just need to add one line to set focus on textbox otherwise everything is working fine.
txtFunctionName.Text = _tempFuncName;
txtFunctionName.SelectionStart = _cursorPosition;
txtFunctionName.SelectionLength = _selectionLength ;
txtFunctionName.Focus();
txtFunctionName.Text = _tempFuncName;
txtFunctionName.SelectionStart = _cursorPosition;
txtFunctionName.SelectionLength = _selectionLength ;
these statements are sufficient enough to do the req thing. i was making mistake in choosing event to write code. Thanks everyone.
For me, setting the focus only didn't help, but scrolling to the caret did.
txt_logArea.Select(txt_logArea.Text.Length, 0);
txt_logArea.ScrollToCaret();
Basically, I am creating a button in an oval shape. But my button label is too long to display in one line, so I wanted to split it into multiple lines so that the oval button looks good.
How do I enable word wrap on a button?
If you want to set a button's label to multi-line text inside the VS designer, you can click on the "down arrow" at the right of the property field and then you are able to enter multiple lines of text.
I tried this in VS 2015.
Set the label text on form load and add Environment.Newline as the newline string, like this:
btnOK.Text = "OK" + Environment.NewLine + "true";
Just add a newline in the text at the place where it should split.
Try to add "\n" to button's Text property in the places you want to wrap.
There are two options:
If you are creating a custom control, then place a label control on it with the Autosize = true option. And adjust its size as per the buttons size.
Add a new line wherever you want (a bit crude).
You can create custom Button with one additional property (say, Label) which converts "\n" occurrence into "real" newline (because VS designer cannot do it already 10 years):
public string Label
{
get { return (string.IsNullOrEmpty(Text) ? Text : Text.Replace("\n", #"\n")); }
set {
Text = (string.IsNullOrEmpty(value) ? value : value.Replace(#"\n", "\n"));
}
}
Once you created such class, your SuperButton will be visible in Toolbox at Project page, so you don't loose visual way of design.
You just need to insert a line break (i.e. \n) in the button text.
Example:
Button1.AutoSize = true;
Button1.Text = "This is \n The Button Text";