时间:2019-01-17 标签:c#Richtextboxmovecarettox,ylocation
我正在开发一个正在阅读的项目来自终端(tn3270)界面,该界面的插入符位置列为 X,Y,我想将其传递到显示完整终端界面的 Richtextbox。我尝试通过多种方式在谷歌上查找,但我似乎只能找到如何获取 Richtextbox 中插入符的 x,y 位置。
我正在寻找一个可以的功能
private void SetCaretLocation(iX,iY);
:编辑:
private void SetCaretPos(int iX,int iY)
{
int iLen = 0;
int iRow = 0;
foreach (string str in richTextBox1.lines)
{
iRow++;
iLen += str.Length;
if (iRow == iX)
break;
}
iLen += iY;
richTextBox1.SelectionStart = iLen;
}
我似乎正在接近一些东西。但位置似乎不正确匹配。
Possible Duplicate:
RichTextBox C# Set caret location winforms
I am working on a project where I am reading from a terminal(tn3270) interface which has a caret postion listed as X,Y which I want to pass on to my richtextbox that is displaying the full terminal interface. I have tried looking on google multiple ways, but all I can seem to find is how to GET the x,y location of the caret in the richtextbox.
I am looking to have a function that can be
private void SetCaretLocation(iX,iY);
:EDIT:
private void SetCaretPos(int iX,int iY)
{
int iLen = 0;
int iRow = 0;
foreach (string str in richTextBox1.lines)
{
iRow++;
iLen += str.Length;
if (iRow == iX)
break;
}
iLen += iY;
richTextBox1.SelectionStart = iLen;
}
I seem to be getting some what close. But the position does not seem to match up correctly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定这是否应该如何完成,但有一种方法:
编辑:
我没有意识到您还发布了编辑。您需要考虑新行字符。如果是 \n,则为 1;如果是 \r\n,则为 2
I am not sure if this is how it should be done but its 1 way:
Edit:
I didn't realize you had also posted an edit. You need to consider the new line character. 1 if its \n, 2 if its \r\n
检查 SelectionStart 方法
您可以使用 RichTextBox 控件的 SelectionStart 属性来“获取或设置文本框中选定文本的起点”。
http://msdn.microsoft.com/en-us /library/system.windows.forms.richtextbox.aspx
代码示例在这里:
http://msdn.microsoft.com/en-us /library/system.windows.forms.textboxbase.selectionstart.aspx
check the SelectionStart method
You can use the SelectionStart property of your RichTextBox control to "Gets or sets the starting point of text selected in the text box."
http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.aspx
The code example is here:
http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.selectionstart.aspx