Results 1 to 3 of 3

Thread: Two cursors?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2001
    Posts
    205

    Two cursors?

    I am trying to write an app that creates a merge document. So I have a text box for writing the document text. And I want to have a list of data fields that can be inserted. But I'm stuck on how to maintain the cursor position in the text box while the user selects a field from a dropdown list. I suppose I could use a context menu but the list is really long and I wanted a better way of displaying the list - like a set of combo boxes. When I click on the combo box to select a field, the text box no longer has focus and the cursor position of where the field will be inserted is lost. Any suggestions?

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Two cursors?

    You can set a variable to the current position of the caret on the TextBox.Leave event... So when the user selects something to insert, you know where to insert in the text.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Two cursors?

    No, the cursor position is NOT lost. The position of the caret in the TextBox is stored in its SelectionStart property. The value of that property doesn't change just because the control loses focus. Try this:

    1. Create a new WinForms project.
    2. Add a TextBox and a Button to the form.
    3. Run the project.
    4. Type some text into the TextBox.
    5. Click in the middle of the text to set the caret position.
    6. Press the Tab key. Note that there is no indication of where the caret is in the TextBox while it doesn't have focus.
    7. Press the Tab key again. Note that the caret is exactly where you left it.

    How do you suppose that happened? Because the SelectionStart property didn't change just because the control lost focus. Just because you couldn't see where the caret was, doesn't mean that the control had forgotten.

    Now, the SelectionLength property is also related here. When you select text in the TextBox the SelectionStart property contains the index of the first selected character and the SelectionLength contains the number of characters selected. If no text is selected then the SelectionStart is the position of the caret and the SelectionLength is zero.

    If you set the SelectedText property of a TextBox then any text currently selected will be replaced with the new value. If no text is selected then the new value is inserted at the position indicated by the SelectionStart property. Calling the TextBox's Paste method will have a similar effect, only the new value is whatever text is currently on the clipboard.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width