【问题标题】:Gambas3 setting Cursor positionGambas3 设置光标位置
【发布时间】:2022-10-19 10:02:57
【问题描述】:

我在 Gambas3 中使用 qt5 TextEdit 来获取富文本。

请考虑代码:

    Dim cursorpos As Integer

  If Key.Code = Key.Left Or Key.Code = Key.Up Or Key.code = Key.Right Or Key.Code = Key.Down Or Key.Code = Key.Delete Or Key.Code = Key.Backspace Then
    
    cursorpos = TextEdit1.Pos ' just pick the position
    Print cursorpos
    
  Else 
    cursorpos = TextEdit1.Pos
    Print cursorpos
    TextEdit1.RichText = "<font color = \"#224444\">" & Replace(TextEdit1.Text, gb.NewLine, "<br>") & "</font>" ' this preserves the newlines, and replaces them with a <br> for the rich text
    Print "setting : ", cursorpos ' prints the correct value
    TextEdit1.Pos = cursorpos ' does not work
    Print "got : ", TextEdit1.Pos ' jumps to the end of the string
  Endif

现在,我写:

This si a line
this is a second line

我在第一行有错字。我用箭头键到达那里。我按了两次退格键,然后删除了 si 这个词。都好。现在我希望输入字符 i,光标应该停留在字符 i 之后。但是一旦 i 输入到正确的位置,光标就会跳到文本的末尾。

请帮忙。如何将光标位置保持在正确的位置?谢谢你。

【问题讨论】:

    标签: gambas


    【解决方案1】:

    你的错误是这一行......

    TextEdit1.RichText = "<font color = "#224444">" & Replace(TextEdit1.Text, gb.NewLine, "<br>") & "</font>" 
    ' this preserves the newlines, and replaces them with a <br> for the rich text
    

    实际上它删除了隐藏的原始富文本格式

    在不更改内部 RichText 格式的情况下更改内联文本颜色的最佳方法是执行以下操作...

    TextEdit1.Format.Color = Color.Red
    

    然后在该位置键入的文本将显示为红色。

    因此,您可以监视删除/退格,然后在当前位置将格式颜色设置为红色。

    gambaswiki.org/wiki/comp/gb.qt4.ext/textedit

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-14
      • 1970-01-01
      相关资源
      最近更新 更多