none
LitDev Extension Can't Set Cursor Position RRS feed

  • Question

  • I'm trying to set the cursor position in a textbox. I would think the following code would work using LitDev's extension:

    TextBox=Controls.AddTextBox(0,0)
    LDFocus.SetFocus(TextBox)
    LDControls.SetCursorPosition(TextBox,5)

    This doesn't do it though. Any thoughts on what I'm doing wrong or how to get it to work? I actually want to do this in a richtextbox.  I tried it and didn't work on it either. I looked at LitDev's samples and didn't see an example for doing this. Also, can't get the LDControls.SetCursorToEnd to work either.

    Thanks,


    JR


    Tuesday, June 24, 2014 3:04 PM
    Answerer

Answers

All replies

  • The SetCursorPosition is within the TextBox - we need some text in it to see the effect.

    Below the cursor is positioned after character 5, i.e. after o.  Then after 1 sec to the end of the text.

    tb = Controls.AddTextBox(0,0)
    Controls.SetTextBoxText(tb,"Hello World")
    LDFocus.SetFocus(tb)
    LDControls.SetCursorPosition(tb,5)
    Program.Delay(1000)
    LDControls.SetCursorToEnd(tb)

    Tuesday, June 24, 2014 5:30 PM
    Moderator
  • LitDev,

    That works differently than I thought that it did. Meaning you have to have text on the line to move the cursor.

    So, how would I do the following? Using wordpad I can create columns in a richtextbox by using tabs and then setting column titles for each column.  I can save that in a file and bring it into SB into a richtextbox. What I want to be able to do then is with the program running on the next line down from the column titles enter text in a column and then tab to the next column and enter text. Since I don't have any text in these lines to begin with how can I move the cursor to get it positioned at the start of the next column?

    Thanks,


    JR

    Tuesday, June 24, 2014 7:16 PM
    Answerer
  • JR,

    Not sure I follow exactly, but I might start something like this for a textbox.

    tb = Controls.AddMultiLineTextBox(0,0)
    Controls.SetSize(tb,500,500)
    Controls.TextTyped = OnTextTyped
    
    Sub OnTextTyped
      txt = Controls.GetTextBoxText(tb)
      lastCharCode = Text.GetCharacterCode(Text.GetSubText(txt,Text.GetLength(txt),1))
      If (lastCharCode = 10) Then 'Return
        Controls.SetTextBoxText(tb,txt+Text.GetCharacter(9)) 'Add a tab
        LDControls.SetCursorToEnd(tb) 'Position cursor after the tab
      EndIf
    EndSub

    Tuesday, June 24, 2014 7:37 PM
    Moderator
  • LitDev,

    Sorry, I think I made it too confusing. Please, let me try again.

    Let's say at the top of a multiline textbox I have:

    Column1                   Column2            Column3

    On the next line down from the column titles I want to put data in column 1 and then tab to column 2 enter data then tab to column 3 and enter data. I was thinking I could do that with LDControls.SetCursor Position, but I can't do that because the function requires that data be in the line which this line doesn't have data to start with.

    If your cursor position function worked this way it would be easy to do by just using the tab key to start the function and move the cursor.


    JR

    Tuesday, June 24, 2014 8:04 PM
    Answerer
  • The tab button usually tries to jump from one control to the next - this is the usual behavior if you press tab while in a Windows textbox (it moves to the next control).  So using tab within a textbox will be tricky.

    How about this, we get cursor positioned after a new tab if press tab.  We could add logic to enter the right number of tabs to align collumns perhaps.

    All this might be easier in TextWindow where the cursor can be positioned exactly.

    tb = Controls.AddMultiLineTextBox(0,0)
    Controls.SetSize(tb,500,500)
    tabChar = Text.GetCharacter(9)
    returnChar = Text.GetCharacter(10)
    Controls.SetTextBoxText(tb,"Collumn1"+tabChar+"Collumn2"+tabChar+"Collumn3"+returnChar)
    LDControls.SetCursorToEnd(tb)
    
    GraphicsWindow.KeyDown = OnKeyDown
    
    Sub OnKeyDown
      key = GraphicsWindow.LastKey
      If (key = "Tab") Then
        AddTab()
      EndIf
    EndSub
    
    Sub AddTab
      txt = Controls.GetTextBoxText(tb)
      Controls.SetTextBoxText(tb,txt+tabChar) 'Add a tab
      LDControls.SetCursorToEnd(tb) 'Position cursor after the tab
    EndSub
    Tuesday, June 24, 2014 8:24 PM
    Moderator
  • Hello LitDev,

    here's german xml for LitDev v 109.

    Tuesday, June 24, 2014 8:27 PM
    Answerer
  • Thanks
    Tuesday, June 24, 2014 8:35 PM
    Moderator
  • JR, here's another idea using fixed width font to relate tabs and characters (8 char to a tab)  to help alignment

    GraphicsWindow.FontName = "Consolas"
    tb = Controls.AddMultiLineTextBox(0,0)
    Controls.SetSize(tb,500,500)
    tabChar = Text.GetCharacter(9)
    returnChar = Text.GetCharacter(10)
    Controls.SetTextBoxText(tb,"Collumn1"+tabChar+"Collumn2"+tabChar+"Collumn3"+returnChar)
    LDControls.SetCursorToEnd(tb)
    
    GraphicsWindow.KeyDown = OnKeyDown
    
    Sub OnKeyDown
      key = GraphicsWindow.LastKey
      If (key = "Tab" And LDFocus.IsFocus(tb)) Then
        AddTab()
      EndIf
    EndSub
    
    Sub AddTab
      txt = Controls.GetTextBoxText(tb)
      numChar = 0
      For i = Text.GetLength(txt) To 1 Step -1
        numChar = numChar+1
        char = Text.GetSubText(txt,i,1)
        If (char = tabChar Or char = returnChar) Then
          i = 1
        EndIf
      EndFor
      txt = txt+tabChar 'Add a tab
      If (numChar <= 8) Then
        txt = txt+tabChar 'Add a tab
      EndIf
      Controls.SetTextBoxText(tb,txt)
      LDControls.SetCursorToEnd(tb) 'Position cursor after the tab
    EndSub

    Tuesday, June 24, 2014 8:55 PM
    Moderator
  • LitDev,

    Got it! That works. Didn't realize a tab would work in a textbox, but it does! 

    Thanks,


    JR



    Wednesday, June 25, 2014 12:28 AM
    Answerer
  • There is actually a switch that can be applied for textboxes to allow tab to be used in the text (as opposed to tabbing between controlds - default).

    I will add this option in next extension release, it will be LDControls.TextBoxTab(tb,"True").

    Wednesday, June 25, 2014 8:53 AM
    Moderator
  • LitDev,

    Great! I think doing that will make it easier. I always get caught up in what the tab and return keys will do on the graphics window and with controls. I noticed on your example above if I add another textbox that the tab key will tab the cursor to the second control instead of staying in the first and tabbing through the columns. With your fix I can control how it works.

    Thanks for the extension and your excellent support,


    JR


    Wednesday, June 25, 2014 11:30 AM
    Answerer
  • Uploaded version with LDControls.TextBoxTab.
    Wednesday, June 25, 2014 5:37 PM
    Moderator
  • Seems a general update wednesday today,

    after PDN 4.0 and NPP 6.6.7., here's LitDev 110 (german xml)


    Wednesday, June 25, 2014 8:20 PM
    Answerer
  • LitDev,

    Tested! It's neat. Works great.

    Thanks,


    JR

    Wednesday, June 25, 2014 8:26 PM
    Answerer
  • Thanks again Pappa for De xml - only a small mod this time, but it takes less than a minute to compile, create html, add code and source to zips and do the uploads - all pretty much automated.

    Wednesday, June 25, 2014 8:46 PM
    Moderator
  • Just as I supposed it in a programmer's life and try the same for all recurring tasks. :-)
    Wednesday, June 25, 2014 9:03 PM
    Answerer