PDA

View Full Version : Textbox Callback



martin
16-06-2009, 12:15
Hello Thinbasic Freaks :lol:

What's wrong with the following script? I get an error about CBMSG (not defined or misspelled keyword). CBMSG is also used in the Thinbasic textbox-example and works correctly with that script. So what am I doing wrong?


'Textbox Callback test
uses "ui"

begin const
%textbox1=1
end const

dim WindowMain AS DWORD

function TBMAIN()
DIALOG New 0, "Main Window",-1,-1, 110, 50, %WS_CLIPCHILDREN or %WS_OVERLAPPEDWINDOW, 0 To WindowMain

CONTROL ADD TEXTBOX, WindowMain,%textbox1, "", 5,25,100,10,,,textbox1proc

'---Show dialog
dim dlgCounter as long

Dialog Show Modeless WindowMain Call WindowMainProc
Do
Dialog DoEvents 0 To dlgCounter
Loop While dlgCounter
end function

callback function textbox1proc() as long
select case cbmsg
case %WM_COMMAND
select case CBCTLMSG
case %EN_SETFOCUS
case %EN_KILLFOCUS
case %EN_CHANGE
case %EN_UPDATE
CONTROL GET TEXT WindowMain, %textbox1 TO Txt
msgbox 0,"You typed: " & Txt
case %WM_NOTIFY
end select
end select
end function

CALLBACK FUNCTION WindowMainProc() AS LONG 'Callback for dialog
SELECT CASE CBMSG 'Test for messages
CASE %WM_INITDIALOG
CASE %WM_SIZING
CASE %WM_COMMAND
case %WM_NOTIFY
CASE %WM_CLOSE
END SELECT
END FUNCTION


Best regards,

Martin

ErosOlmi
16-06-2009, 12:23
You was almost there ;) (but I also need to add more checking in parsing :unguee: )

Change from:

CONTROL ADD TEXTBOX, WindowMain,%textbox1, "", 5,25,100,10,,, textbox1proc
to

CONTROL ADD TEXTBOX, WindowMain,%textbox1, "", 5,25,100,10,,, CALL textbox1proc

Also add a local variable named "TXT" in function "textbox1proc"

Ciao
Eros

martin
16-06-2009, 12:36
OUCH I looked over that for hours. Thanks for helping me out!

ErosOlmi
16-06-2009, 12:43
You can also just write something:

CONTROL ADD TEXTBOX, WindowMain,%textbox1, "", 5,25,100,10, call textbox1proc

because style and ExStyle are both optional.

thinBasic uses the keyword CALL to determine if the next token is to be considered a callback function name or not.

Ciao
Eros

martin
16-06-2009, 14:17
Okay, that clear. But now let's say I want to show a msgbox "thanks for your input" when the user press ENTER key or leaves the textbox with TAB or mouse. For leaving textbox with the mouse i guess i must use %EN_KILLFOCUS. But how to code ENTER and TAB in the Textbox callback? I love Thinbasic but i find those %-paramaters a little bit complicated and confusing :-S

ErosOlmi
16-06-2009, 15:00
I love Thinbasic but i find those %-paramaters a little bit complicated and confusing :-S


Martin,

thinBasic follows what are know Windows SDK programming. It is more or less the same things you would do if programming in C or C++ without using any "wrapper".
It may sounds complex at first but if you will get it, than you will find familiar with many other low level programming.

Trapping ENTER or ESC or TAB key is not so easy and all depends on how a windows is showed on screen: MODAL or MODELESS

MODAL windows were thought to be used for dialog boxes where there is usually an OK (%IDOK) or CANCEL (%IDCANCEL) buttons: ENTER correspond to pressing the OK button while ESC corresponds to pressing CANCEL button.
When showing a window in MODAL way, windows automatically trap those keys and pass the corresponding messages to the textbox having the focus and than to the dialog (if the textbox callback return zero).
So if you transform your window to MODAL you will get all you need. See attached example
MODELESS windows were instead intended for complex window where the programmer has full responsibility to subclass or superclass controls. In MODELESS way, Windows does not handle ENTER or TAB or ESC key trap but you have to apply a subclass procedure that will handle the events before they are trapped by the OS. Unfortunately thinBasic cannot pass a script function to the OS telling you want to subclass a control. But in future maybe I will be able to add also this functionality


So, if you need to trap ENTER or TAB or ESC keys in one or more windows, use MODAL way like in the attached example.

Ciao
Eros

martin
16-06-2009, 16:41
Okay your explanation and example made it more understandable to me now. But I'm pretty sure I need some more help with this in the future, be prepared ;-)

martin
18-06-2009, 10:20
Another question, as promised ;)
Sometimes I ask to myself: Am I the coder of my script or is it Eros? :lol:

Okay, I have a listview and I want to do something when the user press CTRL X or CTRL Y:

1. If CTRL X is pressed, the script must only respond if a row of the listview is selected
2. The script must always respond to CTRL Y (can pressed by user whenever the listview has focus or not)

I played a lot with %WM_KEYDOWN, GetWindowMultiKeyState, and GetWindowKeyState but the results are not good :fie:

Martin