Dennis
Full Member
Old but still active
Posts: 147
|
Post by Dennis on Oct 20, 2021 12:10:11 GMT -5
Hi I have a situation where I am capturing text in a textbox and want to display the number of characters entered so far. To make it clearer, I have a textbox and I want to display how many characters have been entered so far as a seperate statictext. Is it possible to do this?
I have defined the textbox as multiline as follows:
Stylebits #RiskID.LongDescrip, _WS_VSCROLL OR _ES_MULTILINE, _ES_AUTOHSCROLL, 0, 0
textbox #RiskID.LongDescrip, 146, 345, 265, 80
I have considered using inkey$ and SCAN but inkey$ only works in a grapicbox or window and to be honest, I cannot quite understand how SCAN works.
Regards
|
|
|
Post by Walt Decker on Oct 20, 2021 12:27:07 GMT -5
If you are not adverse to using API you can use a timer and use the User32.dll function GetWindowTextLengthA(). Example: Timer 100, [GET.LEN] [TIMER.SET] WAIT [GET.LEN] Hndl = HWND(#YOR.LSTBOX) CALLDLL "User32", "GetWindowTextLengthA", Hndl AS ULONG, TextLen AS LONG
PRINT #YOR.STATCTRL, STR$(TextLen)
GOTO [TIMER.SET] Why do you have ES_AUTOHSCROLL. If you drop that the text will wrap.
I am not certain how SCAN works, but I think it may be either a Sleep(0) or PeekMessageA()
|
|
Dennis
Full Member
Old but still active
Posts: 147
|
Post by Dennis on Oct 20, 2021 13:28:59 GMT -5
Thanks Walt It is wrapping but will try without the Horizontal Scroll. I think it was a remnant from a concept test that I did. I was hoping to not have to use an API but will play with what you have posted. Thanks
|
|
Dennis
Full Member
Old but still active
Posts: 147
|
Post by Dennis on Oct 20, 2021 13:50:20 GMT -5
Thanks Walt - it works! Will use it and wait to see if someone comes up with an "API-less" solution
|
|
|
Post by Walt Decker on Oct 20, 2021 13:53:48 GMT -5
About the only thing an LB text box understands in native LB is "print #handle.ext, "!contents? varName$"" Even the text editor does not allow you the get the number of characters straight-away. With the text editor you can get the number of lines that are delimited by a CRLF and the number of characters per line. Then you have to multiply the # of lines by the characters per line to get the total character count. I think GetWindowTextLengthA() does not work with a text editor. It does with a text box. Is the text scrolling when it reaches the right side of the control or only when "ENTER" is pressed?
Your reply was slightly before mine. Glad it works for you.
PS: My bad. I mis-read your STYLEBITS. Your are REMOVING AUTOHSCROLL. That is why it is scrolling. I read it as including AUTOHSCROLL.
|
|
Dennis
Full Member
Old but still active
Posts: 147
|
Post by Dennis on Oct 20, 2021 15:26:25 GMT -5
Hi Walt Yes it scrolls without having to press ENTER. Your idea works well with a single multiline textbox but I cannot get it to work when a window has more than 1 multiline textboxes. It works OK with the first but not with the subsequent ones. I can enter data into all of them but only the first updates its character counter. The other textboxes' counters remain dormant... I have tried using a switch of sorts to identify which control has focus but LB does not have something like a "GETFOCUS" function to let you know which control has focus at a point. I will use your routine when I have a single multiline textbox but will have to reconsider what I do about multi textboxes. I did remember to set the Hndl to point to whatever textbox had focus. The previous BASIC that I am converting from had a GETFOCUS and it allowed inkey$ to be trapped in a textbox. It also allowed an optional "event handler" for a control. Using these featues, you could consider each control individually. If this was the case, I could set up your solution for every textbox individually. Hope this makes sense Perhaps I am having a geriatric moment!
|
|
|
Post by Walt Decker on Oct 20, 2021 15:41:57 GMT -5
Give me a bit. I will see what I can work up for you. I have a solution in mind.
|
|
|
Dennis
Full Member
Old but still active
Posts: 147
|
Post by Dennis on Oct 20, 2021 16:35:46 GMT -5
Its past midnight here (Greece) so going to call it a day... Thanks for your assistance - much appreciated!
|
|
|
Post by Walt Decker on Oct 20, 2021 16:41:55 GMT -5
Hope you are not in bed. If so, it will be here in the AM, your time. And you are welcome.
' ES.MULTILINE = HEXDEC("&H0004") ES.AUTOVSCROLL = HEXDEC("&H0040") ES.AUTOHSCROLL = HEXDEC("&H0080") ES.NOHIDESEL = HEXDEC("&H0100") WS.VSCROLL = HEXDEC("&H00200000") WS.GROUP = HEXDEC("&H00020000") WS.TABSTOP = HEXDEC("&H00010000")
OPEN "User32.Dll" FOR DLL AS #USER DIM EdtCtrl(2)
CurEdt = 0 Hndl = 0
WindowWidth = 600 WindowHeight= 400
Style = WS.MULTILINE OR WS.AUTOVSCROLL OR WS.NOHIDESEL OR WS.GROUP _ OR WS.TABSTOP Nstyle = WS.MULTILINE OR WS.AUTOVSCROLL OR WS.NOHIDESEL OR WS.TABSTOP STYLEBITS #BOX.EDL, Style, ES.AUTOHSCROLL, 0, 0 STYLEBITS #BOX.EDM, Nstyle, ES.AUTOHSCROLL, 0, 0 STYLEBITS #BOX.EDR, Nstyle, ES.AUTOHSCROLL, 0, 0 TEXTBOX #BOX.EDL, 5, 5, 150, 80 TEXTBOX #BOX.EDM, 160, 5, 150, 80 TEXTBOX #BOX.EDR, 315, 5, 150, 80
OPEN "TXT BOXES" FOR WINDOW AS #BOX
EdtCtrl(0) = HWND(#BOX.EDL) EdtCtrl(1) = HWND(#BOX.EDM) EdtCtrl(2) = HWND(#BOX.EDR)
PRINT #BOX.EDL, "!setfocus"
TIMER 100, [GET.LEN] [TIMER.SET] WAIT
'----------------------------------------------------- '-----------------------------------------------------
[GET.LEN] CurFocus = 0 TxtLen = 0 Found = -1
Hndl = EdtCtrl(CurEdt) CALLDLL #USER, "GetFocus", CurFocus AS ULONG [RE.DO] IF CurFocus = Hndl THEN CALLDLL #USER, "GetWindowTextLengthA", Hndl AS ULONG, TxtLen AS LONG IF TxtLen < 1 THEN GOTO [TIMER.SET] PRINT TxtLen '<--- your static control here ELSE FOR I = 0 TO 2 IF CurFocus = EdtCtrl(I) THEN Found = I EXIT FOR END IF NEXT I IF Found > -1 THEN Hndl = EdtCtrl(Found) CurEdt = Found GOTO [RE.DO] ELSE PRINT "ERROR" END END IF END IF
GOTO [TIMER.SET] [END.GET.LEN] '
By the way, with a little more code it can be made to capture the "ENTER" key and scroll down to the next line instead of having to press CTRL + ENTER to accomplish that.
|
|
|
Post by Brandon Parker on Oct 20, 2021 21:25:28 GMT -5
Here is a quick example using only Liberty BASIC native commands if you are interested. At least I think this is what you are after. An API-less implementation ... NoMainWin
WindowWidth = 330 WindowHeight = 175
StyleBits #Test.txtTest1, _WS_VSCROLL Or _ES_MULTILINE, _ES_AUTOHSCROLL, 0, 0 TextBox #Test.txtTest1, 5, 5, 150, 100 StaticText #Test.stcTest1, "0", 5, 115, 150, 25 StyleBits #Test.txtTest2, _WS_VSCROLL Or _ES_MULTILINE, _ES_AUTOHSCROLL, 0, 0 TextBox #Test.txtTest2, 160, 5, 150, 100 StaticText #Test.stcTest2, "0", 160, 115, 150, 25 Open "Test" For Window As #Test #Test "TrapClose quit" Timer 50, [checkTxtBox] Wait
[checkTxtBox] Timer 0 #Test.txtTest1 "!Contents? testText$" #Test.stcTest1 "TextBox 1 Len is ";Len(testText$) testText$ = "" #Test.txtTest2 "!Contents? testText$" #Test.stcTest2 "TextBox 2 Len is ";Len(testText$) Timer 50, [checkTxtBox] Wait
Sub quit handle$ Timer 0 Close #handle$ End End Sub {:0) Brandon Parker
|
|
Dennis
Full Member
Old but still active
Posts: 147
|
Post by Dennis on Oct 20, 2021 23:48:46 GMT -5
Up again and working Wow!! Thanks Walt and Brandon - both solutions make sense AND taught me lots about using TIMER. Since I was looking for an API-less solution, I will go with Brandon's but I first need to play with it a bit to see how it works in a window with mixed controls. However, I like what Walt has offered because it helps to solve another issue that of which control has focus. I am going to use both in two different situations. Incidentally Brandon, your code has a small error - you are using testText$ for both textboxes whereas the second "!contents" and display should use testText2$. I am sure both solutions are something that others can use. Thanks for both of your assistance - much appreciated. Now to embed them in my code...
|
|
|
Post by tsh73 on Oct 21, 2021 0:23:00 GMT -5
nope testText$ in
#Test.txtTest2 "!Contents? testText$" is just ordinary variable being reused. You could call it dummy$, whatever.
|
|
Dennis
Full Member
Old but still active
Posts: 147
|
Post by Dennis on Oct 21, 2021 0:30:41 GMT -5
Hi Anatoly
Run the code and you will see what happens. The wrong length is being shown for the second textbox... :-)
You are looking for each textbox to have it's own length counter and not both the same.
|
|
Dennis
Full Member
Old but still active
Posts: 147
|
Post by Dennis on Oct 21, 2021 0:58:55 GMT -5
Thanks guys - embedded and all works as needed!
|
|