April 30, 2024, 04:50:10 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Check what Edit Control Box curser is

Started by Hobo, April 11, 2011, 03:24:16 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Hobo

Hello all. I am working with Edit Controls. I would like to perform different operations by checking what Edit Control Box has the curser. Is there a way to check what Edit Control Box the curse is? In other word I would like to use Select statement. If curser in Edit Control “A” do this, if curser in Edit Control “B” do this and so on.  Am I making any sense or should I upload a sample? Any help would be appreciated.

Hobo

LarryMc

Look in the help file under edit controls/notification messages for @ENSETFOCUS
use a global variable to hold the control id of the control with the current focus.

LarryMc
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Hobo

I hope my code not too long for posting. If yes, please let me know what is acceptable and what not.

Thanks Larry. I did looked up the Help file. The problem is I am still at beginner level and I do not know how to apply your suggested @ENSETFOCUS.
I understand to assign global variable for Edit Control, but I still don't know how to put in work with @ENSETFOCUS. The truth is examples works for me the best.
My problem is around the "CASE @IDtimer". there are two subs. One calculate shaft, the other calculate hub. Shaft calculation is fine. what I would like is to get cursor position and then pick to go the right sub.

If it is too stupid or/and complicated I'll figure out something else.


DECLARE "user32",SetWindowPos(Hwnd:int,HwndAfter:int,x:int,y:int,cx:int,cy:int,flags:int)
DECLARE "user32",Enter alias GetAsyncKeyState(vKey:int),int
DECLARE "user32",GetForegroundWindow(),INT

SETID "WM_NCLButtonDown",161
SETID "HTCaption",2

DEF win:WINDOW

DEF sdia$,skey$,hdia$,hkey$:STRING

DEF focus:INT
DEF MyWin:INT

WINDOW win,856,763,393,85,@nocaption|@size,0,"Simple Window",main
SETWINDOWCOLOR win,RGB(255,255,0)
STARTTIMER win,10,1
ENABLETABS win,1
SETPRECISION 4
focus = 2

'Draw text
CONTROL win,"T,Dia,60,2,38,15,0,1"
SETFONT win, "MS Sans Serif",9,400,0,1
SETCONTROLCOLOR win,1,rgb(0,0,0),RGB(255,255,0)
CONTROL win,"T,Key,130,2,38,15,0,2"
SETFONT win, "MS Sans Serif",9,400,0,2
SETCONTROLCOLOR win,2,rgb(0,0,0),RGB(255,255,0)
CONTROL win,"T,Shaft,5,18,25,15,0,3"
SETFONT win, "MS Sans Serif",9,400,0,3
SETCONTROLCOLOR win,3,rgb(0,0,0),RGB(255,255,0)
CONTROL win,"T,Hub,5,42,25,15,0,4"
SETFONT win, "MS Sans Serif",9,400,0,4
SETCONTROLCOLOR win,4,rgb(0,0,0),RGB(255,255,0)
CONTROL win,"T,Distance,203,2,45,15,0,5"
SETFONT win, "MS Sans Serif",9,400,0,5
SETCONTROLCOLOR win,5,rgb(0,0,0),RGB(255,255,0)
CONTROL win,"T,=,180,18,10,15,0,6"
SETFONT win, "MS Sans Serif",9,400,0,6
SETCONTROLCOLOR win,6,rgb(0,0,0),RGB(255,255,0)
CONTROL win,"T,=,180,42,10,15,0,7"
SETFONT win, "MS Sans Serif",9,400,0,7
SETCONTROLCOLOR win,7,rgb(0,0,0),RGB(255,255,0)

'Draw editing box
CONTROL win,"E,shaft-dia,40,16,60,20,@tabstop|@cteditright,11"
SETFONT win, "MS Sans Serif", 9, 400,0,11
CONTROL win,"E,key-size,110,16,60,20,@tabstop|@cteditright,12"
SETFONT win, "MS Sans Serif", 9, 400,0,12
CONTROL win,"E,hub-dia,40,40,60,20,@tabstop|@cteditright,13"
SETFONT win, "MS Sans Serif", 9, 400,0,13
CONTROL win,"E,key-size,110,40,60,20,@tabstop|@cteditright,14"
SETFONT win, "MS Sans Serif", 9, 400,0,14
CONTROL win,"E,shaft-distance,195,16,60,20,@tabstop|@cteditright,15"
SETFONT win, "MS Sans Serif", 9, 400,0,15
CONTROL win,"E,hub-distance,195,40,60,20,@tabstop|@cteditright,16"
SETFONT win, "MS Sans Serif", 9, 400,0,16
SETFOCUS win, 11

'Draw close button
RECT win,360,0,20,70,rgb(0,0,0),rgb(235,235,235)
CONTROL win,"B,X,361,0,19,19,0,200"

SetWindowPos(win,-1,0,0,0,0,2|1)
MyWin = win
run=1

WAITUNTIL run = 0
CLOSEWINDOW win
END

SUB main
SELECT @class
CASE @IDcontrol
IF (@controlID=200)
run=0
      ENDIF
CASE @IDLbuttonDn
IF @mouseX >361 & @mouseX <380 & @mouseY >18
SendMessage Win,@WM_NCLButtonDown,@HTCaption,0
ENDIF
CASE @IDtimer & (MyWin = GetForegroundWindow())
IF (Enter(13) <> 0)
do:until Enter(13) = 0
ShaftCalculation()
ENDIF
IF (Enter(13) <> 0)
do:until Enter(13) = 0
HubCalculation()
ENDIF
ENDSELECT
RETURN

SUB ShaftCalculation
select focus
CASE 2
sdia$ = GETCONTROLTEXT (win,11)
SETFOCUS win,12
focus=3
CASE 3
skey$ = GETCONTROLTEXT (win,12)
IF (VAL(sdia$) <> 0) & (VAL(skey$) <>0)
result=(VAL(sdia$)/2)-(VAL(skey$)/2) : SETCONTROLTEXT win,15,STR$(result)
SETFOCUS win,11
focus=4
ELSE
SETCONTROLTEXT win,11,""
SETCONTROLTEXT win,12,""
SETCONTROLTEXT win,15,""
SETFOCUS win,11
focus=2
ENDIF
CASE 4
SETCONTROLTEXT win,11,""
SETCONTROLTEXT win,12,""
SETCONTROLTEXT win,15,""
SETFOCUS win,11
focus=2
endselect
RETURN

SUB HubCalculation
select focus
CASE 2
hdia$ = GETCONTROLTEXT (win,13)
SETFOCUS win,14
focus=3
CASE 3
hkey$ = GETCONTROLTEXT (win,14)
IF (VAL(hdia$) <> 0) & (VAL(hkey$) <>0)
result=(VAL(hdia$)/2)+(VAL(hkey$)/2) : SETCONTROLTEXT win,16,STR$(result)
SETFOCUS win,13
focus=4
ELSE
SETCONTROLTEXT win,13,""
SETCONTROLTEXT win,14,""
SETCONTROLTEXT win,16,""
SETFOCUS win,13
focus=2
ENDIF
CASE 4
SETCONTROLTEXT win,13,""
SETCONTROLTEXT win,14,""
SETCONTROLTEXT win,16,""
SETFOCUS win,13
focus=2
endselect
RETURN

aurelCB

Hi Hobo ..try now.
is this what you mean?
DECLARE "user32",SetWindowPos(Hwnd:int,HwndAfter:int,x:int,y:int,cx:int,cy:int,flags:int)
DECLARE "user32",Enter alias GetAsyncKeyState(vKey:int),int
DECLARE "user32",GetForegroundWindow(),INT

SETID "WM_NCLButtonDown",161
SETID "HTCaption",2

DEF win:WINDOW

DEF sdia$,skey$,hdia$,hkey$:STRING

DEF focus:INT
DEF MyWin:INT

WINDOW win,0,0,393,100,@nocaption|@size,0,"Simple Window",main
SETWINDOWCOLOR win,RGB(255,255,0)
STARTTIMER win,10,1
ENABLETABS win,1
SETPRECISION 4
focus = 2

'Draw text
CONTROL win,"T,Dia,60,2,38,15,0,1"
SETFONT win, "MS Sans Serif",9,400,0,1
SETCONTROLCOLOR win,1,rgb(0,0,0),RGB(255,255,0)
CONTROL win,"T,Key,130,2,38,15,0,2"
SETFONT win, "MS Sans Serif",9,400,0,2
SETCONTROLCOLOR win,2,rgb(0,0,0),RGB(255,255,0)
CONTROL win,"T,Shaft,5,18,25,15,0,3"
SETFONT win, "MS Sans Serif",9,400,0,3
SETCONTROLCOLOR win,3,rgb(0,0,0),RGB(255,255,0)
CONTROL win,"T,Hub,5,42,25,15,0,4"
SETFONT win, "MS Sans Serif",9,400,0,4
SETCONTROLCOLOR win,4,rgb(0,0,0),RGB(255,255,0)
CONTROL win,"T,Distance,203,2,45,15,0,5"
SETFONT win, "MS Sans Serif",9,400,0,5
SETCONTROLCOLOR win,5,rgb(0,0,0),RGB(255,255,0)
CONTROL win,"T,=,180,18,10,15,0,6"
SETFONT win, "MS Sans Serif",9,400,0,6
SETCONTROLCOLOR win,6,rgb(0,0,0),RGB(255,255,0)
CONTROL win,"T,=,180,42,10,15,0,7"
SETFONT win, "MS Sans Serif",9,400,0,7
SETCONTROLCOLOR win,7,rgb(0,0,0),RGB(255,255,0)

'Draw editing box
CONTROL win,"E,shaft-dia,40,16,60,20,@tabstop|@cteditright,11"
SETFONT win, "MS Sans Serif", 9, 400,0,11
CONTROL win,"E,key-size,110,16,60,20,@tabstop|@cteditright,12"
SETFONT win, "MS Sans Serif", 9, 400,0,12
CONTROL win,"E,hub-dia,40,40,60,20,@tabstop|@cteditright,13"
SETFONT win, "MS Sans Serif", 9, 400,0,13
CONTROL win,"E,key-size,110,40,60,20,@tabstop|@cteditright,14"
SETFONT win, "MS Sans Serif", 9, 400,0,14
CONTROL win,"E,shaft-distance,195,16,60,20,@tabstop|@cteditright,15"
SETFONT win, "MS Sans Serif", 9, 400,0,15
CONTROL win,"E,hub-distance,195,40,60,20,@tabstop|@cteditright,16"
SETFONT win, "MS Sans Serif", 9, 400,0,16
SETFOCUS win, 11

'Draw close button
RECT win,360,0,20,70,rgb(0,0,0),rgb(235,235,235)
CONTROL win,"B,X,361,0,19,19,0,200"

SetWindowPos(win,-1,0,0,0,0,2|1)
'MyWin = win
run=1

WAITUNTIL run = 0
CLOSEWINDOW win
END

SUB main
SELECT @class

CASE @idclosewindow
run=0

CASE @IDcontrol
IF (@controlID=200)
run=0
      ENDIF

CASE @IDLbuttonDn
IF @mouseX >361 & @mouseX <380 & @mouseY >18
SendMessage Win,@WM_NCLButtonDown,@HTCaption,0
ENDIF

CASE @IDtimer:' & (win = GetForegroundWindow())
IF win<>0
IF (Enter(13) <> 0)
do:until Enter(13) = 0
ShaftCalculation()
ENDIF
IF (Enter(13) <> 0)
do:until Enter(13) = 0
HubCalculation()
ENDIF
endif

ENDSELECT
RETURN

SUB ShaftCalculation
select focus
CASE 2
sdia$ = GETCONTROLTEXT (win,11)
SETFOCUS win,12
focus=3
' CASE 3
skey$ = GETCONTROLTEXT (win,12)
IF (VAL(sdia$) <> 0) & (VAL(skey$) <>0)
result=(VAL(sdia$)/2)-(VAL(skey$)/2) : SETCONTROLTEXT win,15,STR$(result)
SETFOCUS win,11
focus=4
ELSE
SETCONTROLTEXT win,11,""
SETCONTROLTEXT win,12,""
SETCONTROLTEXT win,15,""
SETFOCUS win,11
focus=2
ENDIF
CASE 4
SETCONTROLTEXT win,11,""
SETCONTROLTEXT win,12,""
SETCONTROLTEXT win,15,""
SETFOCUS win,11
focus=2
endselect
RETURN

SUB HubCalculation
select focus
CASE 2
hdia$ = GETCONTROLTEXT (win,13)
SETFOCUS win,14
focus=3
CASE 3
hkey$ = GETCONTROLTEXT (win,14)
IF (VAL(hdia$) <> 0) & (VAL(hkey$) <>0)
result=(VAL(hdia$)/2)+(VAL(hkey$)/2) : SETCONTROLTEXT win,16,STR$(result)
SETFOCUS win,13
focus=4
ELSE
SETCONTROLTEXT win,13,""
SETCONTROLTEXT win,14,""
SETCONTROLTEXT win,16,""
SETFOCUS win,13
focus=2
ENDIF
CASE 4
SETCONTROLTEXT win,13,""
SETCONTROLTEXT win,14,""
SETCONTROLTEXT win,16,""
SETFOCUS win,13
focus=2
endselect
RETURN

Hobo

Hi Aurel. Thank you again so many times aided my little projects.
It was not what I had in my mind, but thank you to clean out a little bit my code.
I was heading home yesterday when suddenly hit me, why do I get the same input twice to get answer for each when I could use the same input once and get two answers. So, now the code works as I wanted. Never the less Larry brought up this @ENSETFOCUS option that is now bugs me and now I want to learn how to use it, but that is another chapter. :-)
Here is my final code. It works like a calculator. Type the number and hit enter.

DECLARE "user32",SetWindowPos(Hwnd:int,HwndAfter:int,x:int,y:int,cx:int,cy:int,flags:int)
DECLARE "user32",Enter alias GetAsyncKeyState(vKey:int),int
DECLARE "user32",GetForegroundWindow(),INT

SETID "WM_NCLButtonDown",161
SETID "HTCaption",2

DEF win:WINDOW

DEF sdia$,skey$,hdia$,hkey$:STRING

DEF focus:INT


WINDOW win,856,763,393,63,@nocaption|@size,0,"Simple Window",main
SETWINDOWCOLOR win,RGB(255,255,0)
STARTTIMER win,10,1
ENABLETABS win,1
SETPRECISION 4
focus = 1
CENTERWINDOW win

'Draw text
CONTROL win,"T,Dia,33,2,38,15,0,1"
SETFONT win, "MS Sans Serif",9,400,0,1
SETCONTROLCOLOR win,1,rgb(0,0,0),RGB(255,255,0)
CONTROL win,"T,Key,100,2,38,15,0,2"
SETFONT win, "MS Sans Serif",9,400,0,2
SETCONTROLCOLOR win,2,rgb(0,0,0),RGB(255,255,0)
CONTROL win,"T,Shaft,183,2,25,15,0,3"
SETFONT win, "MS Sans Serif",9,400,0,3
SETCONTROLCOLOR win,3,rgb(0,0,0),RGB(255,255,0)
CONTROL win,"T,Hub,255,2,25,15,0,4"
SETFONT win, "MS Sans Serif",9,400,0,4
SETCONTROLCOLOR win,4,rgb(0,0,0),RGB(255,255,0)
CONTROL win,"T,=,150,18,10,15,0,5"
SETFONT win, "MS Sans Serif",9,400,0,5
SETCONTROLCOLOR win,5,rgb(0,0,0),RGB(255,255,0)

'Draw editing box
CONTROL win,"E,shaft-dia,10,16,60,20,@tabstop|@cteditright,11"
SETFONT win, "MS Sans Serif", 9, 400,0,11
CONTROL win,"E,key-size,80,16,60,20,@tabstop|@cteditright,12"
SETFONT win, "MS Sans Serif", 9, 400,0,12
CONTROL win,"E,shaft-distance,165,16,60,20,@tabstop|@cteditright,13"
SETFONT win, "MS Sans Serif", 9, 400,0,13
CONTROL win,"E,hub-distance,235,16,60,20,@tabstop|@cteditright,14"
SETFONT win, "MS Sans Serif", 9, 400,0,14
SETFOCUS win, 11

'Draw close button
RECT win,360,0,20,60,rgb(0,0,0),rgb(235,235,235)
CONTROL win,"B,X,361,0,19,19,0,200"

SetWindowPos(win,-1,0,0,0,0,2|1)
run=1

WAITUNTIL run = 0
CLOSEWINDOW win
END

SUB main
SELECT @class
CASE @IDcontrol
IF (@controlID=200)
run=0
      ENDIF
CASE @IDLbuttonDn
IF @mouseX >361 & @mouseX <380 & @mouseY >18
SendMessage Win,@WM_NCLButtonDown,@HTCaption,0
ENDIF
CASE @IDtimer
IF (Enter(13) <> 0)
do:until Enter(13) = 0
Calculation()
ENDIF
ENDSELECT
RETURN

SUB Calculation
SELECT focus
CASE 1
CONTROLCMD win,11,@EDSetSelection,0,-1
sdia$ = GETCONTROLTEXT (win,11)
SETFOCUS win,12
focus=2
CASE 2
CONTROLCMD win,12,@EDSetSelection,0,-1
skey$ = GETCONTROLTEXT (win,12)
IF (VAL(sdia$) <> 0) & (VAL(skey$) <>0)
result=(VAL(sdia$)/2)-(VAL(skey$)/2) : SETCONTROLTEXT win,13,STR$(result)
result=(VAL(sdia$)/2)+(VAL(skey$)/2) : SETCONTROLTEXT win,14,STR$(result)
SETFOCUS win,11
focus=1
ELSE
SETCONTROLTEXT win,11,""
SETCONTROLTEXT win,12,""
SETCONTROLTEXT win,13,""
SETCONTROLTEXT win,14,""
SETFOCUS win,11
focus=1
ENDIF
ENDSELECT
RETURN

GWS

April 12, 2011, 01:13:49 PM #5 Last Edit: April 12, 2011, 01:19:10 PM by GWS
Hi guys,

Here's another version with a few tricks.


DEF win:WINDOW
DEF Focus:int
DEF result:float
DEF sdia$,skey$,hdia$,hkey$:STRING
DEF caption[5],a$:string

declare "user32",SetWindowPos(win:window,ins:int,x:int,y:int,wid:int,hgt:int,flags:int),int

setid "SWP_NOMOVE",2
setid "SWP_NOSIZE",1
setid "HWND_TOPMOST",-1
setid "WM_NCLBUTTONDOWN",0xA1
setid "HTCAPTION",2

WINDOW win,856,763,393,63,@nocaption|@size,0,"Simple Window",main
SETWINDOWCOLOR win,RGB(255,255,0)
ENABLETABS win,1

flags = @SWP_NOMOVE|@SWP_NOSIZE
SetWindowPos(win,@HWND_TOPMOST,0,0,0,0,flags)

caption[1] = "Diam","Key","Shaft","Hub"

' Draw close button
CONTROL win,"B,X,369,0,19,19,0,200"

' Draw edit boxes
for i = 1 to 4
CONTROL win,"E,,10+90*(i-1),20,60,20,@tabstop|@cteditright,i"
SETFONT win, "MS Sans Serif", 9, 400,0,i
next i

' Show caption text
for i = 1 to 4
CONTROL win,"T," + caption[i] + ",20+90*(i-1),4,38,15,@cteditcenter,i+4"
SETFONT win, "MS Sans Serif",9,400,0,i+4
SETCONTROLCOLOR win,i+4,rgb(0,0,0),RGB(255,255,0)
next i

a$ = "="
setfont win, "MS Sans Serif",12,600
move win,170,20
print win,a$

setfocus win,1
CENTERWINDOW win

run=1

WAITUNTIL run = 0
CLOSEWINDOW win
END

SUB main
SELECT @class

case @IDcontrol
select @CONTROLID
case 200
run=0
    case 1 :'Diam
if @NOTIFYCODE = @ENSETFOCUS
Focus = 1 :'note edit box 1 now has the focus
endif
case 2 :'Key
if @NOTIFYCODE = @ENSETFOCUS
Focus = 2 :'note edit box 2 now has the focus
endif
endselect
  case @IDLBUTTONDN
' Move the Window by dragging it ..
     SendMessage win, @WM_NCLBUTTONDOWN,@HTCAPTION,0
case @IDMENUPICK
' check for Enter key pressed ..
if @code = @IDOK
select Focus
case 1
sdia$ = GETCONTROLTEXT (win,1)
SETFOCUS win,2
case 2
skey$ = GETCONTROLTEXT (win,2)
IF (VAL(sdia$) <> 0) & (VAL(skey$) <> 0)
result=(VAL(sdia$)/2)-(VAL(skey$)/2) : SETCONTROLTEXT win,3,STR$(result)
result=(VAL(sdia$)/2)+(VAL(skey$)/2) : SETCONTROLTEXT win,4,STR$(result)
SETFOCUS win,1
ELSE
SETCONTROLTEXT win,1,""
SETCONTROLTEXT win,2,""
SETCONTROLTEXT win,3,""
SETCONTROLTEXT win,4,""
SETFOCUS win,1
ENDIF
endselect
endif
ENDSELECT
RETURN


This version uses an array for the caption strings, which then makes it possible to set up the edit boxes within a 'For' loop.  The only tricky bit is working out the 'x' co-ordinates to lay out the boxes.

It detects the 'Enter' key being pressed by using a magic method of watching for the message @IDMENUPICK with an @code value of @IDOK.

Pretty amazing since the window doesn't have a menu -  :o - but it works OK.

Hope you find it interesting ..

all the best, :)

Graham
Tomorrow may be too late ..

Hobo

This is very good Graham.

I hardly ever use “For” loop, so especially like how you have setup the Controls.  Elegant way to setup their IDs.
I also believe I am getting how the @NOTIFYCODE = @ENSETFOCUS works.
The @code = @IDOK kind of replacing the @IDtimer if I am correct. And the way you drag the window is cool. :-)

Anyway much to learn, much I appreciate your time and teaching.

Hobo