|
-
Sep 8th, 2007, 10:09 AM
#1
Thread Starter
Addicted Member
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?
-
Sep 8th, 2007, 10:35 AM
#2
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.
-
Sep 8th, 2007, 09:35 PM
#3
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|