时间:2019-01-17 标签:c#Richtextboxmovecarettox,ylocation

发布于 2024-11-26 01:01:56 字数 832 浏览 1 评论 0原文

可能的重复:
RichTextBox C# 设置插入符位置 winforms

我正在开发一个正在阅读的项目来自终端(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随遇而安 2024-12-03 01:01:56

我不确定这是否应该如何完成,但有一种方法:

private void SetCaretPosition(int rows, int col)
{
    int curPos = 0;
    if (richTextBox1.Lines.Length >= rows)
    {    
        for (int i = 0; i < rows - 1; ++i)
        {
           curPos += richTextBox1.Lines[i].Length + 1; //add 1 for the newline character
        }
        richTextBox1.SelectionStart = curPos + col; 
    }
    else
        richTextBox1.SelectionStart = richTextBox1.TextLength;
    richTextBox1.ScrollToCaret();             
}

编辑:

我没有意识到您还发布了编辑。您需要考虑新行字符。如果是 \n,则为 1;如果是 \r\n,则为 2

I am not sure if this is how it should be done but its 1 way:

private void SetCaretPosition(int rows, int col)
{
    int curPos = 0;
    if (richTextBox1.Lines.Length >= rows)
    {    
        for (int i = 0; i < rows - 1; ++i)
        {
           curPos += richTextBox1.Lines[i].Length + 1; //add 1 for the newline character
        }
        richTextBox1.SelectionStart = curPos + col; 
    }
    else
        richTextBox1.SelectionStart = richTextBox1.TextLength;
    richTextBox1.ScrollToCaret();             
}

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

地狱即天堂 2024-12-03 01:01:56

检查 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文