首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RichTextBox和Caret位置

RichTextBox和Caret位置
EN

Stack Overflow用户
提问于 2011-11-26 07:40:33
回答 1查看 3.4K关注 0票数 2

当我选择文本时,我找不到确定RTB中插入符号位置的方法。SelectionStart 不是选项

我想检测的方向选择,无论是向后还是向前。我正试图在SelectionChanged event中实现这一点。如有任何建议,将不胜感激。

编辑:

通过使用mouseDown和mouseUp事件注册鼠标运动方向(X轴)来解决这个问题。

代码:

代码语言:javascript
复制
bool IsMouseButtonPushed = false;
int selectionXPosition = 0, sDirection=0;

private void richTextBox_SelectionChanged(object sender, EventArgs e)
{
    if (sDirection==2)//forward
    {
        //dosomething
    }
}

private void richTextBox_MouseMove(object sender, MouseEventArgs e)
{
    if (IsMouseButtonPushed && (selectionXPosition - e.X) > 0)//backward
    {
        sDirection = 1;
    }
    else if (IsMouseButtonPushed && (selectionXPosition - e.X) < 0)//forward
    {
        sDirection = 2;
    }
}

private void richTextBox_MouseDown(object sender, MouseEventArgs e)
{
    IsMouseButtonPushed = true;
    selectionXPosition = e.X;
}

private void richTextBox_MouseUp(object sender, MouseEventArgs e)
{
    IsMouseButtonPushed = false;
}

还有其他的方法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-26 19:26:51

SelectionStart和SelectionLength属性在左侧选择期间发生变化,SelectionLength在右侧选择过程中发生变化。

简单解决方案:

代码语言:javascript
复制
int tempStart;
int tempLength;

private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
    if (richTextBox1.SelectionType != RichTextBoxSelectionTypes.Empty)
    {
        if (richTextBox1.SelectionStart != tempStart)
            lblSelectionDesc.Text = "Left" + "\n";
        else if( richTextBox1.SelectionLength != tempLength)
            lblSelectionDesc.Text = "Right" + "\n";
    }
    else
    {
        lblSelectionDesc.Text = "Empty" + "\n";
    }

    tempStart = richTextBox1.SelectionStart;
    tempLength = richTextBox1.SelectionLength;

    lblSelectionDesc.Text += "Start: " + richTextBox1.SelectionStart.ToString() + "\n";
    lblSelectionDesc.Text += "Length: " + richTextBox1.SelectionLength.ToString() + "\n";
}

控件:

RitchTextBox + 2xLabels

  1. ,我不知道为什么,但是即使禁用了AutoWordSelection,我的鼠标也会选择整个单词。不幸的是,对于我的解决方案,这会导致选择方向的改变。
  2. ,您可能会为此使用属性更改事件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8277027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档