Unable to Highlight Text in a TextBox - c#

I have a winforms app that initially displays a window with two file dialog boxes, one folder dialog box, and one textbox with some default text in it. I want the default text to be highlighted. I've tried everything I have found with no luck.
Any advice is appreciated.
Regards.

Make sure HideSelection is false on your textbox and use the select method to select the text:
textBox1.HideSelection = false;
textBox1.Select(0, textBox1.Text.Length);

You can use SelectionStart and SelectionLenght
textbox.SelectionStart = 0;
textbox.SelectionLength = textbox.Text.Length;

Related

How to change the panel visibility with richtextbox input

For some reason, this one will not work:
if (richTextBox2.Text == "settings")
panel5.Visible = true;
The other inputs work like "close", inputting close will close the form.
What i want to happen is when "settings" is typed into the text box, i want the panel to become visible.
You could try comparing in this way:
if (richTextBox2.Text.ToUpper() == "SETTINGS")
panel5.Visible = true;
Other way to try, is debug "if" line, and check the content of Text property. Maybe, richTextBox2.Text contains some hidden characters.
I hope this could help you.

How to set cursor after the text in a textbox on TextChange Event

I have a textbox txtZipCode. On the TextChange event, i am populating data in other two textboxes txtState and txtCity. What i want to do is, i want to set the cursor after the Text of txtState.
I am doing txtState.Focus()
but, the cursor is displaying on the left side of the text...i want to set the cursor after the text.
Any Suggestions ?
Thanks in Advance
Try This,
txtState.Attributes["onfocus"] = "javascript:this.setSelectionRange(this.value.length,this.value.length);";
This should help after focus:
txtState.SelectionStart = txtState.Text.Length -1;
txtState.SelectionLength = 0;

How can I select all the text within a Windows Forms textbox?

I want to select all the text that is with in text box.
I've tried this using the code below:
textBoxResults.SelectionStart = 0;
textBoxResults.SelectionLength = textBoxResults.Text.Length;
Source: I got this code from here http://msdn.microsoft.com/en-us/library/vstudio/hk09zy8f(v=vs.100).aspx
but for some reason it doesn't seem to work - meaning, no text gets selected.
You can use the built in method for this purpose.
textBoxResults.SelectAll();
textBoxResults.Focus(); //you need to call this to show selection if it doesn't has focus
You can also try the following which might solve you problem:
textBoxResults.SelectAll();
This works well with multi-lined textbox.
This method enables you to select all text within the control.
public void CopyAllMyText()
{
// Determine if any text is selected in the TextBox control.
if(textBox1.SelectionLength == 0)
// Select all text in the text box.
textBox1.SelectAll();
// Copy the contents of the control to the Clipboard.
textBox1.Copy();
}
Check this link for more info. http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.selectall.aspx

how to get and set current cursor position of WPF textbox

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();

Multiline text as the button label in Windows Forms

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";

Categories