Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insertion point in textbox 1

Status
Not open for further replies.

ChipsIT

Programmer
Aug 13, 2003
16
0
0
GB
Hi

I have a form with a textbox attached to a memo field and several buttons which insert specific text into the textbox. (Imagine a simple HTML Editor on an Access form). When I click a button, the textbox loses the focus so I write the insertion point to a variable in the textbox_Exit event (have also tried Keypress and Keyup events) using text1.selstart so that when a button is clicked, the app knows where to insert the appropriate text into the textbox.

The trouble is, text1.selstart returns 0 at all times unless the textbox is clicked. For example, if I type text anywhere within the content of the textbox then click a button, the variable is 0, if I click at position n within the textbox, the variable is set to n.

I'm wondering if I am looking in the wrong place. The question is, how do I record the current cursor position in a textbox_Exit event. It sounds simple to me, but I must be simpler!

Thanks for any help.
Steve
 
Hi Steve,

Would it help to write your code in the
TextBox1_LostFocus() Event ?

Regards

John
 
Hi John

I've tried writing the code in Exit, LostFocus, KeyPress and KeyUp but the basic problem exists in all. There seems to only be an insertion point when the textbox is clicked. If I click at a point within the textbox the insertion point is correct, but if I type anything following the click the insertion point = 0. The cursor position and the insertion point are not the same thing, but there isn't a property to give me the cursor position.

I set a variable in any of the events and put this in another textbox so I can see what value I have when I click any button, so the only code I am using is

intCursor = Me.txtInput.SelStart
Me.txtPos = intCursor

I am thinking that I am "barking up the wrong tree" and there's a better way of setting the cursor position variable... but I don't know it!

Steve
 
So as not to leave this thread hanging, I think the answer to my question lies in using a toolbar rather than buttons as the control doesn't lose the focus when you click a toolbar button.

However, I opted to go the ActiveX route and use a Rich Textbox (richtx32.ocx) which gives me far more functionality even though it obviously turns out a little more complex.

Steve
 
Hi Steve,

I don't recall seeing this thread when it was raised and there is no easy answer to it. I THINK you could keep track of the insertion point with a form-level variable and a combination of change, keypress and mousedown events but it wouldn't be easy and would take a fair amount of testing to get right.

Anyway, the reason for my post is just to thank you both for making the effort yourself to solve your problem and for returning and posting your resolution. That is worth a star - it so often doesn't happen.

Enjoy,
Tony
 
My first star! No problems Tony. I find it frustrating myself to locate a thread you are looking for only to find it hanging with no answers.

I believe the answer here is Rich Textbox and have completed what I was doing with that. I haven't answered the question on why the insertion point seems to be zero when entering data to a textbox via the keyboard, but I have a much better word processor-like interface now anyway.

Steve
 
I know that you already have another solution for this, but just in case you are still interested, I think I've got what you want to work!

In a UserForm with a TextBox1 and a CommandButton1:

Code:
Option Explicit
Dim InsertPosition As Double

Private Sub CommandButton1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
InsertPosition = TextBox1.SelStart
End Sub

Private Sub CommandButton1_Click()
TextBox1.SetFocus
TextBox1.Text = Left(TextBox1.Text, InsertPosition) & CommandButton1.Caption _
    & Right(TextBox1.Text, Len(TextBox1.Text) - InsertPosition)
TextBox1.SelStart = InStr(1, TextBox1.Text, CommandButton1.Caption) _
    + Len(CommandButton1.Caption)
End Sub

I hope you find this interesting or even helpful!



Peace! [peace]

Mike

Never say Never!!!
Nothing is impossible!!!
 
Mike

The Exit event should be on the textbox. You also need to set InsertPosition on the KeyUp event as if you type into the text field, the Insertion Point is set to zero. I'm not going to spend time comparing this to my code, but I got the following seemingly working using SendKeys rather than replacing the whole text string...

Code:
Dim InsertPosition As Double

Private Sub CommandButton1_Click()
TextBox1.SetFocus
TextBox1.SelStart = InsertPosition
SendKeys CommandButton1.Caption

End Sub

Private Sub TextBox1_Exit(Cancel As Integer)
InsertPosition = TextBox1.SelStart
End Sub

Private Sub TextBox1_KeyUp(KeyCode As Integer, Shift As Integer)
InsertPosition = TextBox1.SelStart
End Sub

Acutally I may compare later when I have time cos it's so simple I can't believe it was a problem!

Anyway, now we have a satisfactory conclusion to the thread. :)

Cheers
Steve
 
The way the code is written works just fine, it insert the Caption from CommandButton1 to the last position where the cursor was located in TextBox1 and places the cursor at the end of the "inserted" text.

I didn't suggest SendKeys, because I didn't know how long the text was that you wanted to insert.

You just have to create a new dummy UserForm with a TextBox and a CommandButton (you don't even have to rename them) to be able to test this.



Peace! [peace]

Mike

Never say Never!!!
Nothing is impossible!!!
 
My intention is not to criticise the code of people who try to assist Mike. With a little tinkering I got it working and it lead me to the conclusion that fitted my original problem. Thanks for your input.

Steve
 
i don't know if you are still interested, but I was able to overcome this problem by using the following steps.

dim curplace as variant
1. On textbox lost focus
curplace = me.textbox.selstart

2. On click of button
after buttons action... me.textbox.setfocus

3. On textbox get focus
me.textbox.selstart = curplace

I hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top