I have a standard WinForms TextBox and I want to insert text at the cursor's position in the text. How can I get the cursor's position?
Thanks
I have a standard WinForms TextBox and I want to insert text at the cursor's position in the text. How can I get the cursor's position?
Thanks
Regardless of whether any text is selected, the SelectionStart property represents the index into the text where you caret sits. So you can use String.Insert to inject some text, like this:
myTextBox.Text = myTextBox.Text.Insert(myTextBox.SelectionStart, "Hello world");
You want to check the SelectionStart
property of the TextBox
.
James, it's pretty inefficient that you need to replace the whole string when you only want to insert some text at the cursor position.
A better solution would be:
textBoxSt1.SelectedText = ComboBoxWildCard.SelectedItem.ToString();
When you have nothing selected, that will insert the new text at the cursor position. If you have something selected, this will replace your selected text with the text you want to insert.
I found this solution from the eggheadcafe site.
All you have to do is this:
Double click the item (button, label, whatever) that will insert text into the document at the cursor. Then type this in:
richTextBox.SelectedText = "whatevertextyouwantinserted";
Here is my working realization, alowing to type only digits, with restoring last valid typing text position:
Xaml:
<TextBox
Name="myTextBox"
TextChanged="OnMyTextBoxTyping" />
Code behind:
private void OnMyTextBoxTyping(object sender, EventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(myTextBox.Text, @"^[0-9]+$"))
{
var currentPosition = myTextBox.SelectionStart;
myTextBox.Text = new string(myTextBox.Text.Where(c => (char.IsDigit(c))).ToArray());
myTextBox.SelectionStart = currentPosition > 0 ? currentPosition - 1 : currentPosition;
}
}
You have to keep the SelectionStart
property in a variable, and then when you press the button, shift the focus back to the TextBox. Then set the SelectionStart
property to the one in the variable.
On what event would you suggest I record the variable? Leave?
Currently I have:
private void comboBoxWildCard_SelectedIndexChanged(object sender, EventArgs e)
{
textBoxSt1.Focus();
textBoxSt1.Text.Insert(intCursorPos, comboBoxWildCard.SelectedItem.ToString());
}
private void textBoxSt1_Leave(object sender, EventArgs e)
{
intCursorPos = textBoxSt1.SelectionStart;
}
Recording on the Leave event is working but the text doesn't get inserted, am I missing something?
UPDATE: I needed textBoxSt1.Text =
textBoxSt1.Text = textBoxSt1.Text.Insert(intCursorPos, comboBoxWildCard.SelectedItem.ToString());
Thanks all.
Thanks
To get the caret position when you click the mouse within the text of a TextBox
, use the TextBox
MouseDown
event. Create a point using X and Y properties of the MouseEventArgs
. The TextBox
has a method called GetCharIndexFromPosition(point)
. Pass it the point and it returns the caret position. This works if you are using the mouse to determine where you want to insert the new text.
int cursorPosition = textBox1.SelectionStart;
//it will extract your current cursor position where ever it is
//textBox1 is name of your text box. you can use one
//which is being used by you in your form
© 2022 - 2024 — McMap. All rights reserved.