April 16, 2024, 10:05:59 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Beginners questions

Started by Hobo, September 12, 2008, 06:14:39 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

mrainey

Hobo,

Here's a program I wrote with IBasic Standard back in my beginner days (I'm now an "advanced beginner").  It uses a timer to trap the Enter key and works with multiple edit boxes.  It doesn't use the Tab key.  Maybe you can get some ideas from it.


                                   Mike
Software For Metalworking
http://closetolerancesoftware.com

LarryMc

It's doing what you are telling it to do.
Let's examine your code step by step:1 CASE @IDTIMER
  2IF (Enter(13) <> 0) & (focus=2)
3 inputa$ = GETCONTROLTEXT (win,2)
4 SETFOCUS win,3
5 focus=3
6 sw1=0
7ENDIF
  8IF (Enter(13) <> 0) & (focus=3)
9 inputb$ = GETCONTROLTEXT (win,3)
10 focus=4
11ENDIF
12IF (Enter(13) <> 0) & (focus=4)
13 result=VAL(inputa$)+VAL(inputb$)
14 SETCONTROLTEXT win,4,STR$(result)
15ENDIF

Each time the timer fires the handler is entered at step 1.
You have already set focus=2 and let's assume Enter(13)<> 0 is TRUE
At step 5 focus is set to 3.
So, when the if at step 8 is evaluated it is a true statement; Enter(13) <> 0 & focus=4
Step 10 sets focus=4
when the if at step 12 is evaluated it is a true statement; Enter(13) <> 0 & focus=4
so steps 13 and 14 are executed.

Like I said, it is doing exactly what you are telling it to do.

Larry

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

LarryMc

substitute this for what's in the timer portion: CASE @IDTIMER
  IF (Enter(13) <> 0)
'clear the buffer
do:until Enter(13) = 0
select focus
case 2
inputa$ = GETCONTROLTEXT (win,2)
SETFOCUS win,3
focus=3
sw1=0
case 3
inputb$ = GETCONTROLTEXT (win,3)
focus=4
'case 4
result=VAL(inputa$)+VAL(inputb$)
SETCONTROLTEXT win,4,STR$(result)
endselect
ENDIF

This will fix the "fall through" problem.
But you've got a ways to go in order to be able to correct an entry without having to start over from scratch.

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

aurelCB

I play with my example and find solution with TAB key .Without starttimer.
I try with ENTER key but without success.
See code:

'TAB button traped for edit box in window with menu
def win:window
DECLARE "user32",Enter alias GetAsyncKeyState(vKey:int),int
def run:int
def CurrentID:int
def ebox1$,ebox2$,ebox3$:string
def OK_ctr,Cancel_ctr,Close_ctr:int
const BN_CLICKED = 0
const BS_NOTIFY = &H4000
const BN_SETFOCUS = 6

WINDOW win,0,0,450,250,@minbox,0,"Enter button traping",handler
SetWindowColor win,rgb(221,222,231)
Menu win,"T,&File,0,0","I,&Exit,0,1","I,&Exit2,0,2"
'1 enable tabing ,0 disable tab
Enabletabs win,1
'starttimer win,100,1

control win,"E,,10,10,80,24,@tabstop,10"
setfont win,"MS Sans Serif",8,400,0,10
control win,"E,,110,10,80,24,@tabstop,20"
setfont win,"MS Sans Serif",8,400,0,20
control win,"E,,210,10,80,24,@tabstop,30"
setfont win,"MS Sans Serif",8,400,0,30
'if you need tabed  buttons then must use constants------------------
control win,"B,OK,23,55,70,20,@tabstop|0x50010000|BS_NOTIFY,40"
control win,"B,Cancel,120,55,70,20,@tabstop|0x50010000|BS_NOTIFY,50"
control win,"B,Close,70,90,70,20,@tabstop|0x50010000|BS_NOTIFY,60"

SETFOCUS win ,10

run=1
Waituntil run=0
closewindow win
end

SUB handler
Select @CLASS
case @idclosewindow
run=0
case @idcreate
centerwindow win

case @idcontrol
select @controlid
case 10
if Enter(9) <> 0
if (@notifycode=@ensetfocus) then currentID=10
if (@notifycode=@enkillfocus)& (run=1) then lostfocus10()
endif
case 20
if Enter(9) <> 0
if (@notifycode=@ensetfocus) then currentID=20
if (@notifycode=@enkillfocus)& (run=1) then lostfocus20()
endif

endselect
' case @idchar
               '  if @code=0x0D
' messagebox win,"Enter pressed","No entry"
' endif
Endselect
RETURN
'-------------------------------------------------------------------
SUB lostfocus10
ebox1$=getcontroltext(win,10)
move win,220,55
print win,"                                                  "

IF ebox1$ <> ""
move win,220,55
print win,"Ebox1: ",ebox1$
ELSE
messagebox win,"Edit box 1 is empty","No entry"
Endif
Return
'-------------------------------------------------------------------
SUB lostfocus20
ebox2$=getcontroltext(win,20)
move win,220,85
print win,"                                                  "

IF ebox2$ <> ""
move win,220,85
print win,"Ebox2: ",ebox2$
ELSE
messagebox win,"Edit box 2 is empty","No entry"
Endif
Return

Hobo

Mike, as a beginner you did very well. Nice piece of work.  ;)
I found the timer part, but your program is so complex I am pretty much lost in it, but never the less I will study it. It helps that you used long and meaningful words for coding.  Thanks to share it.

Also thank Larry for the tutoring and for the simple solution to fix my code.
I know it is not elegant code, but it is a start.

Hobo

aurelCB

Mike is not beginner ,he just joke :D
Are you try my example?

aurelCB

I think that next example work without starttimer.
You can enter numbers in edit1 then move caret with tab key.
Caret will be in edit2 and enter number in edit2 then press tab key again.
After this press enter and you give result in edit3.
If edit3 is not empty you can press tab key to next control wich is button.
Maby this example is not the best but work.

'TAB button traped for edit box in window with menu
def win:window
DECLARE "user32",Enter alias GetAsyncKeyState(vKey:int),int
def run:int
def CurrentID:int
def ebox1$,ebox2$,ebox3$:string
def result:float
def OK_ctr,Cancel_ctr,Close_ctr:int
const BN_CLICKED = 0
const BS_NOTIFY = &H4000
const BN_SETFOCUS = 6

WINDOW win,0,0,450,250,@minbox,0,"Enter button traping",handler
SetWindowColor win,rgb(221,222,231)
Menu win,"T,&File,0,0","I,&Exit,0,1","I,&Exit2,0,2"
'1 enable tabing ,0 disable tab
Enabletabs win,1
'starttimer win,100,1

control win,"E,,10,10,80,24,@tabstop,10"
setfont win,"MS Sans Serif",8,400,0,10
control win,"E,,110,10,80,24,@tabstop,20"
setfont win,"MS Sans Serif",8,400,0,20
control win,"E,,210,10,80,24,@tabstop,30"
setfont win,"MS Sans Serif",8,400,0,30
'if you need tabed  buttons then must use constants------------------
control win,"B,OK,23,55,70,20,@tabstop|0x50010000|BS_NOTIFY,40"
control win,"B,Cancel,120,55,70,20,@tabstop|0x50010000|BS_NOTIFY,50"
control win,"B,Close,70,90,70,20,@tabstop|0x50010000|BS_NOTIFY,60"

SETFOCUS win ,10

run=1
Waituntil run=0
closewindow win
end

SUB handler
Select @CLASS
case @idclosewindow
run=0
case @idcreate
centerwindow win

case @idcontrol
select @controlid
case 10
if Enter(9) <> 0
if (@notifycode=@ensetfocus) then currentID=10
if (@notifycode=@enkillfocus)& (run=1) then lostfocus10()
endif
case 20
if Enter(9) <> 0
if (@notifycode=@ensetfocus) then currentID=20
if (@notifycode=@enkillfocus)& (run=1) then lostfocus20()
endif
case 30
if Enter(9) <> 0
if (@notifycode=@ensetfocus) then currentID=30
if (@notifycode=@enkillfocus)& (run=1) then lostfocus30()

endif

endselect
'----------------------------------------------------------------
case @idkeydown
If (@code=0x0D)
  setprecision 4
ebox1$ = getcontroltext(win,10)
ebox2$ = getcontroltext(win,20)
result=val(ebox1$)+val(ebox2$)
    setcontroltext win,30,""
setcontroltext win,30,str$(result)
Enabletabs win,1
Endif

Endselect
RETURN
'-------------------------------------------------------------------
SUB lostfocus10
ebox1$=getcontroltext(win,10)
move win,220,55
print win,"                                                  "

IF ebox1$ <> ""
move win,220,55
print win,"Ebox1: ",ebox1$
ELSE
messagebox win,"Edit box 1 is empty","No entry"
Endif
Return
'-------------------------------------------------------------------
SUB lostfocus20
ebox2$=getcontroltext(win,20)
move win,220,85
print win,"                                                  "

IF ebox2$ <> ""
move win,220,85
print win,"Ebox2: ",ebox2$
ELSE
messagebox win,"Edit box 2 is empty","No entry"
Endif
Return
'-------------------------------------------------------------------
SUB lostfocus30
ebox3$=getcontroltext(win,30)
'move win,220,85
'print win,"                                                  "

IF ebox3$ <> ""
messagebox win,"Edit box 3 is not empty","No empty"
Enabletabs win,1
setfocus win,40
ELSE
Enabletabs win,0
setfocus win
Endif
Return

LarryMc

Zlatko
I think he is wanting to use the ENTER key and not the TAB key.
Using the ENTER key allows everything to be done with the right hand ( the numeric keys and enter key are on the right side of the keyboard.

Using the TAB key requires using the left hand.

And using the ENTER key makes it work more like a calculator.

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

Hobo

Mike’s program also formats the entered number. Like if I enter 10 the program formats to 10.0000. That is very neat. Also I noticed, he used @notifycode=@ensetfocus and so Graham.

This is so exiting.  8)

Yes, Zlatko’s sample works fine with TAB. No doubt about that, and I could use that for my program, but ENTER key is more natural and more elegant.

This is way beyond what I wanted to accomplish.   ;D

Thanks to all
Hobo

Hobo

I think this works as far as pressing ENTER key. I am sure it is crude, but works.

To make it neater I tied to use USING to format my input, but I could not make it to format my input variable to 3 decimal places. Any suggestion?

Anyway I am going to use this demo experiment to apply my â€Ã...“realâ€Ã, working project.

Thanks again
Hobo   

aurelCB

Hi Hobo ....
What to say,very well for beginner ;)
You only forget define DEF result:FLOAT and your result will be with 3 decimal places.
zlatko

Hobo

I thought I am done, but my program has a strange behavior and I have no idea why.  :o

When I am in the middle of entering numbers into the EDITBOX and then I leave the program. In other words, I put it in sleep mode, or inactive position,  to work with, for example MSWord, but any programs would do and then whenever I use ENTER key my program wakes up and response to the ENTER key input. It should not do that, should it?

Here is my sample program to show if somebody interested and could say something about it.

Cheers  :)

Hobo


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

DEF win:WINDOW
DEF inputa$,inputb$,inputc$,result$:STRING
DEF focus:INT

WINDOW win,0,0,200,300,@MINBOX,0," CURVE Wiz",Handler
SETWINDOWCOLOR win,RGB(255,255,0)

SETPRECISION 3
CENTERWINDOW win
ENABLETABS win,1
STARTTIMER win,10,1
focus=2

'Draw Buttons'
CONTROL win,"B,Add2Num,5,5,60,20,0,1"
SETFONT win, "MS Sans Serif", 9, 400,0,1

'Add edit fields
CONTROL win,"E,inputX,120,40,70,20,@tabstop|@cteditright,2"
showwindow win, @swhide,2
CONTROL win,"E,inputY,120,65,70,20,@tabstop|@cteditright,3"
showwindow win, @swhide,3
CONTROL win,"E,output,120,115,70,20,@tabstop|@cteditright,4"
showwindow win, @swhide,4
CONTROL win,"T,Sum =,75,115,30,20,0,5"
showwindow win, @swhide,5
SETFONT win, "MS Sans Serif", 9, 400,0,5
SETCONTROLCOLOR win,5,0,rgb(255,255,0)
LINE win,100,100,190,100,RGB(255,255,0)


'Add Checkbox
CONTROL win,"C,,25,150,20,20,0x50000003,6"
SETCONTROLCOLOR win,6,rgb(0,0,0),RGB(255,255,0)
CONTROL win,"T,Click to Stay on Top,20,180,150,20,0,12"
SETCONTROLCOLOR win,12,0,rgb(255,255,0)

run =  1
WAITUNTIL win=0
END

Handler:
SELECT @CLASS

CASE @IDCLOSEWINDOW
      CLOSEWINDOW win
   
CASE @IDCONTROL
IF @CONTROLID=1
addnumber()
ENDIF
IF (@CONTROLID=6)
mark =  GETSTATE (win, 6)
IF mark =1
SetWindowPos(win,-1,0,0,0,0,2|1)
ELSE
SetWindowPos(win,-2,0,0,0,0,2|1)
ENDIF
ENDIF
CASE @IDTIMER
  IF (Enter(13) <> 0)
do:until Enter(13) = 0
add2numberoperation()
ENDIF
ENDSELECT
RETURN

SUB addnumber
LINE win,100,100,190,100
showwindow win, @swrestore,2
showwindow win, @swrestore,3
showwindow win, @swrestore,4
SETCONTROLTEXT win,2,""
SETCONTROLTEXT win,3,""
SETCONTROLTEXT win,4,""
SETFOCUS win,2
focus=2

RETURN

SUB add2numberoperation
select focus

case 2
inputa$ = GETCONTROLTEXT (win,2)
SETFOCUS win,3
focus=3
case 3
inputb$ = GETCONTROLTEXT (win,3)
result=VAL(inputa$)+VAL(inputb$)
SETCONTROLTEXT win,4,STR$(result)
SETFOCUS win,1
focus=4
case 4
SETCONTROLTEXT win,2,""
SETCONTROLTEXT win,3,""
SETCONTROLTEXT win,4,""
SETFOCUS win,2
focus=2

endselect
RETURN

Johnny

Try this solution... Check that the foreground window is indeed the window from your own program.

Cheers!


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

DEF win:WINDOW
DEF inputa$,inputb$,inputc$,result$:STRING
DEF focus:INT
DEF MyWin:INT

WINDOW win,0,0,200,300,@MINBOX,0," CURVE Wiz",Handler
SETWINDOWCOLOR win,RGB(255,255,0)

SETPRECISION 3
CENTERWINDOW win
ENABLETABS win,1
STARTTIMER win,10,1
focus=2

'Draw Buttons'
CONTROL win,"B,Add2Num,5,5,60,20,0,1"
SETFONT win, "MS Sans Serif", 9, 400,0,1

'Add edit fields
CONTROL win,"E,inputX,120,40,70,20,@tabstop|@cteditright,2"
showwindow win, @swhide,2
CONTROL win,"E,inputY,120,65,70,20,@tabstop|@cteditright,3"
showwindow win, @swhide,3
CONTROL win,"E,output,120,115,70,20,@tabstop|@cteditright,4"
showwindow win, @swhide,4
CONTROL win,"T,Sum =,75,115,30,20,0,5"
showwindow win, @swhide,5
SETFONT win, "MS Sans Serif", 9, 400,0,5
SETCONTROLCOLOR win,5,0,rgb(255,255,0)
LINE win,100,100,190,100,RGB(255,255,0)


'Add Checkbox
CONTROL win,"C,,25,150,20,20,0x50000003,6"
SETCONTROLCOLOR win,6,rgb(0,0,0),RGB(255,255,0)
CONTROL win,"T,Click to Stay on Top,20,180,150,20,0,12"
SETCONTROLCOLOR win,12,0,rgb(255,255,0)

MyWin = win
run =  1
WAITUNTIL win=0
END

Handler:
SELECT @CLASS

CASE @IDCLOSEWINDOW
      CLOSEWINDOW win
   
CASE @IDCONTROL
IF @CONTROLID=1
addnumber()
ENDIF
IF (@CONTROLID=6)
mark =  GETSTATE (win, 6)
IF mark =1
SetWindowPos(win,-1,0,0,0,0,2|1)
ELSE
SetWindowPos(win,-2,0,0,0,0,2|1)
ENDIF
ENDIF
CASE @IDTIMER
  IF (Enter(13) <> 0) & (MyWin = GetForegroundWindow())
do:until Enter(13) = 0
add2numberoperation()
ENDIF
ENDSELECT
RETURN

SUB addnumber
LINE win,100,100,190,100
showwindow win, @swrestore,2
showwindow win, @swrestore,3
showwindow win, @swrestore,4
SETCONTROLTEXT win,2,""
SETCONTROLTEXT win,3,""
SETCONTROLTEXT win,4,""
SETFOCUS win,2
focus=2

RETURN

SUB add2numberoperation
select focus

case 2
inputa$ = GETCONTROLTEXT (win,2)
SETFOCUS win,3
focus=3
case 3
inputb$ = GETCONTROLTEXT (win,3)
result=VAL(inputa$)+VAL(inputb$)
SETCONTROLTEXT win,4,STR$(result)
SETFOCUS win,1
focus=4
case 4
SETCONTROLTEXT win,2,""
SETCONTROLTEXT win,3,""
SETCONTROLTEXT win,4,""
SETFOCUS win,2
focus=2

endselect
RETURN

aurelCB


Hobo

Wowâ€Ã,¦thank you Johnny. I was afraid nobody could help me at this time. It was elegant how you patched my code.  You really made my day. Thanks again.  :)

Regards,

Hobo

Hobo

I got this idea. If you work in an office like me and notes, little scribbles with information written down on it, lay all around and when I need one of those I can’t find it. So why not make a little â€Ã...“NoteBarâ€Ã, that can pop up with a click and have all there?

So, I did exactly that, but I wonder if I could use string variables for the menu labels? So it would be more organized and not a â€Ã...“mileâ€Ã, long. And my other question. Is there a way to break a line and continue at the next line?

BTW, you can move the NoteBar by clicking just under the bar

Cheers,

Hobo

aurelCB

This example is weird for me.
First when i press menu item Length window close?
And where you find this and what is this? -
   CASE @IDLbuttonDn
       Select @HitWindow
             Case Win
             SendMessage Win,@WM_NCLButtonDown,@HTCaption,0
      endselect

Like i see message is @WM_NCLButtonDown??
And what is purpose of :
CASE @IDCONTROL
      STOPTIMER win,1
         CLOSEWINDOW win
???

aurelCB

By the way Note program allready exists.
Try in attachment.

Hobo

The note program you have sent me works and very nice. Thanks Aurel. :)

Yes, the menu items are not working yet. I have not made my mind how to do the rest.
The code â€Ã,¦
   
CASE @IDLbuttonDn
       Select @HitWindow
             Case Win
             SendMessage Win,@WM_NCLButtonDown,@HTCaption,0
      endselect

I found it in Michael Rainey’s â€Ã...“ME Weights 2.1â€Ã, program. I don’t fully understand either, but something to do moving the window. Once I used @NoCaption statement in my program, I was not able to move the window around the screen anymore. Adding this line of command allowed the program move again.

Errâ€Ã,¦maybe it is a silly idea after all.

Hobo

aurelCB

Yes hobo you right i forget.
Try this:
'part of mike program
def win:window
' DRAG WINDOW WITH NO TITLE BAR
SetID "WM_NCLButtonDown",161
SetID "HTCaption",2
Window win,0,0,400,300,@nocaption|@border,0,"no cap",main
CONTROL win,"B,CloseWin,225,7,70,20,0x50000000,1"

waituntil win=0
end

sub main
Select @class
case @idclosewindow
closewindow win

case @idcontrol
If @controlid=1
closewindow win
Endif

Case @IDLbuttondn       
IF win
SendMessage Win,@WM_NCLButtonDown,@HTCaption,0
ENDIF     
Endselect
return

Hobo