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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inserting into textbox from button event

Status
Not open for further replies.

duotwr

Programmer
Feb 8, 2002
6
GB
Hi,

what I'm wanting to set up is a textbox and a button, so that a user can select some text in the textbox and press a button to insert tags around it. The main problem is that I can't seem to use the selStart and selLength properties of the txtbox because as soon as the button is pressed, the focus has already left the textbox. If anyone knows an event sequence outline that would let me maintain these variables then could they hepl me out.

Ta

Keith
 
Doesn't seem to work. I reckon when you reset the focus to the Textbox using textbox.setFocus it highlights everything in the text box and therefore selStart = 0 and selLength = everything.

I found one bit of info on the net (subsequently have lost it though) that suggested that the function associated with the button might be able to read the selStart and selLength properties before the tesctbox.focus is lost, although which button event this would be attached to I don't know.

Any other ideas would be helpful.

Keith
 
Here you go. I think this may be what you are looking for. Please let me know if it is.

Private num As Integer
Private strt As Integer
Private Sub Command1_Click()
Text1.SetFocus
Text1.SelStart = strt
Text1.SelLength = num
End Sub

Private Sub Text1_LostFocus()
strt = Text1.SelStart
num = Text1.SelLength
End Sub


If you do not understand any of this, please let me know. Thanks.
 
forgot to tell you...
all you need to run the above code is a form with a textbox and a command button
 
Hiya

yeah, thanks. This is just about working, the only weird thing is that at the first press of the button the textbox doesn't set the variables onthe lost focus event, but I'll sort that out.

Thanks a lot

Keith
 
Aargh.

No, not working properly. For some reason the first time I type something in the text box and go to click the button, the loseFocus event doesn't read the correct values of selStart and selLength, it takes them as being zero even if something is selected. Any way of ensuring that these values are read correctly?

Keith
 
let me see if I understand your problem. You want to type some text into a text box. Then you want to highlight some or all of the text in the box? Then you want to click the command button and affect the text in the box? The code I gave you worked on my machine. When I highlighted some or all of the text in my text box, and clicked the button, the text flashed unhighlighted, and then immediately rehighlighted the exact same text. Did you put the first 2 lines of code in the "Declarations" section? Also, you could try changing the "Private" to "Public" on those lines. Keep me informed.
 
the way I checked my code was to set a breakpoint in the code, and step through--hovering the mouse over the variables. VB will show you what value is stored in them.
 
Hiya,

yeah, been trying this. The problem actually seemed to be on the lose focus event, it didn't seem to be setting the selStart and selLength values to the two variables, but only the first time I tried it with writing text. If I retried highlighting, then it worked fine. If I then deleted all the text, typed some more in, highlighted and pressed the button, it didn't! So don't know what's happening, the code for the button is fine, 'cos I debugged stepping through it, bt the variables were not getting set in the first place. Actually now I think, the scope should have kept the previous values, so in actual fact when the lose focus event takes place it must be actually setting the two variables to zero. Very strange.

If you've any ideas on why the textbox (rather than the button & it's code) should be behaving this way then could you let me know.

Thanks a lot

Keith
 
do you have the following lines in the "declarations" section? I cannot reproduce the problems you are having. If you could paste your code on here, I'll look at it. Otherwise, if you put your email address on here, I'll attach a copy of my program and send it to you, if you think it would help. I don't know why it would be setting the values to zero. It should not be.

Private num As Integer
Private strt As Integer
 
Oh yeah, sorry, the declerations of the two variables are up at the declerations section. I can't really email you code for this, and I'm finishing work soon-ish. I'll probbaly leave this for the moment. THe problem is that the first time I type text into the textbox, if I then try to click on the command button then the values are not set by the textbox's onLoseFocus event.

Anyway, will have to leave this I reckon, 'cos time's pressing for today. If you've got any other ideas then let me know. Thanks for the help, if I find out what the problem has been then I'll let you know.

Thanks

Keith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top