在多行文本框中编辑文本 [英] Edit text in multiline textbox
问题描述
我有字母,符号和数字。每个都有自己的标签。我在每个标签上都有一个点击事件,它将标签中的任何内容添加到多行文本框中。
问题是无论插入符号在哪里,当我编辑时通过删除某些内容,然后单击标签将新项目添加到文本框中,它总是将其放在最后一行的末尾而不是插入符号的位置。
我知道这是因为我告诉它通过这样的点击事件:
I have letters, symbols and numbers. Each one is in its own label. I have a click event on each label which adds whatever is in the label to a multi line textbox.
The problem is no matter where the caret is, when I edit the text by deleting something, and then click a label to add a new item to the textbox, it always puts it at the end of the last line rather than where the caret is.
I know this is because I'm telling it to through the click event like this:
private void lblCbore_MouseClick(object sender, MouseEventArgs e)
{
txtFCF.Text += lblCbore.Text;
}
我如何告诉它将我点击的项目放在插入符号的位置,而不是在最后最后一行?
我尝试了什么:
所有我已经完成的是使用多行文本框。我没想到它不能作为单行文本框工作。我搜索了谷歌,并在这里寻求答案。
How can I tell it to place the item I click where the caret is, instead of at the end of the last line?
What I have tried:
All I've done is use the multiline textbox. I didn't expect it to not work as a single line textbox. I've searched google, and on here for an answer.
推荐答案
这很简单:文本框中的Caret位置由SelectionStart属性给出:
It's pretty simple: the Caret position in a textbox is given by the SelectionStart property:
private void MyButton_Click(object sender, EventArgs e)
{
int pos = myTextBox.SelectionStart;
string str = "The new text. ";
myTextBox.Text = myTextBox.Text.Insert(pos, str);
myTextBox.SelectionStart = pos + str.Length;
}
txtFCF.Text += lblCbore.Text;
这不是你插入的方式;你明确追加。
考虑一下:
如何找到光标的位置在文本框中? C# - Stack Overflow [ ^ ]
这篇关于在多行文本框中编辑文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!