Liberty BASIC Community Forum
« Graphicbox code reduction ... HELP!!! »

Welcome Guest. Please Login or Register.
Nov 20th, 2017, 06:00am


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1 2 3 4  Notify Send Topic Print
 veryhotthread  Author  Topic: Graphicbox code reduction ... HELP!!!  (Read 1568 times)
BeeTrap
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 21
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #30 on: Apr 20th, 2017, 08:08am »

I apologize to all for getting into "Turtleman's" question. Since this thread is not "active" I would like to thank Rod and Turtleman. Per your Reply #16, I did rewrite my program using the 2-DIM array. I was worried that adding the extra code to "handle" this array would actually add to the size of my program. But, once completed I compared the original to the new version, with ALL Remarks " ' lines " removed and went from 30KB to 26KB. Also the code just looks better. So I thank Turtleman for asking the question that got me started and Rod for pointing this out to me.
User IP Logged

Turtleman
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 187
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #31 on: Apr 21st, 2017, 05:23am »

I want to publicly thank Rob for all the help he's provided over the years and particularly the past couple of months on this board and via emails. His suggestions and examples have been nothing short of tremendous and are most appreciated. smiley

Not wanting to take further advantage of his generosity, I'm posting my latest roadblock in case anyone else can offer suggestions. In particular, I'm trying to reduce the amount of redundant code and possibly even program correctly for a change. For example, I'm using the following code to read the contents of a dozen textboxes having consecutively numbered variables:

#tab3.w1, "!contents? w1"
#tab3.w2, "!contents? w2"
and so on to
#tab3.w12, "!contents? w12"

My feeble attempts at assigning a handle to an array and using FOR / NEXT so far haven't worked and I could sorely use an example showing how to go about it. Likewise, I suspect there's a more efficient way of writing the contents of the textboxes to a file than listing each line separately:

print #5, w1
print #5, w2
and so on to
print #5, w12

I've been frequenting this novice section for years and doubt I'll ever evolve into a more advanced programmer, but I'm trying. Thanks for everyone's kind assistance!
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is online

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5562
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #32 on: Apr 21st, 2017, 07:09am »

This should be all you need.

Code:
for t= 1 to 12
    'create the #handle variable
    h$="#tab3.w"+str$(t)
    'get the textbox contents using the handle variable
    'save those contents to intermediate variable w
    #h$ "!contents? w"
    'now store it in the array
    w(t)=w
next

'open the file
'printing from the array is simpler than the textboxes
for t = 1 to 12
    #5 w(t)
next
 
User IP Logged

Turtleman
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 187
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #33 on: Apr 21st, 2017, 09:21am »

Thanks so much. I was very close on the reading the contents of the textboxes and had exactly the same code for writing; but as we all know, "close" doesn't count. I now see that a large portion of my program's nearly 17K lines of code can be drastically streamlined, which is almost like starting over. As Rob has educated me, I need to stop using static variables in favor of arrays.

Back later right now I have a tremendous amount of code to clean up! Thanks again!!!!
User IP Logged

Turtleman
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 187
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #34 on: May 2nd, 2017, 1:03pm »

Me again! I've received some terrific advice on this board and via PM's, but am reluctant to pester my "benefactor" further. So I'll throw this out to the board in case someone has a working example I might use.

Basically, I have a column of a dozen textboxes and need the selected textbox to change from white to a colored background on a mouse click. Rod was kind enough to furnish such an example, but for various reasons it isn't fitting into my application. No textbox should be initially selected, while clicking a textbox will change its color, permit a new value (numeric) to be entered, stored, and later retrieved. Clicking the box a second time (or another box or a command) should return it to its white initial state.

I know textbox colors have to be designated before the window is open, but that multiple textboxes may be hidden and made visible that share the same coordinates. Right now, however, I just need a simple way of detecting when a particular box has been selected. Thanks for any suggestions.
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is online

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5562
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #35 on: May 3rd, 2017, 01:45am »

There are two fundamental problems to overcome. The first is that a textbox can't change color, the second is that a textbox has no "change" event so you cannot easily know when a user makes a change.

Leave aside the fact that you now know how to address a textbox with a handle variable, that is just a convenience that reduces the volume of code.

The two solutions I have shown are firstly to move an appropriately coloured textbox over the desired textbox and use that to take input. Secondly to know where the user is making changes you need to poll and check which textbox has current focus. Using that trick you can know if the focus has changed. If so close the input and move it to the new position.

So the code we have shared does everything you desired. Other folks may have alternatives.

Some example code for others to tinker with.

Code:
nomainwin
norows=10
nocols=3
'create an array to hold the textbox handle names
dim tbn$(norows,nocols)
'create an array to hold the actual textbox handle (a numeric pointer)
dim tbh(norows,nocols)
'create an array to hold the textbox current values
dim tbv(norows,nocols)
'fill those values
tbv(1,1)=5
tbv(2,1)=10
tbv(3,1)=15
tbv(4,1)=20
tbv(5,1)=25
tbv(6,1)=30
tbv(7,1)=35
tbv(8,1)=40
tbv(9,1)=45
tbv(10,1)=22
tbv(1,2)=2
tbv(2,2)=3
tbv(3,2)=4
tbv(4,2)=5
tbv(5,2)=6
tbv(6,2)=6
tbv(7,2)=7
tbv(8,2)=8
tbv(9,2)=9
tbv(10,2)=1
tbv(1,3)=1
tbv(2,3)=1
tbv(3,3)=1
tbv(4,3)=2
tbv(5,3)=3
tbv(6,3)=4
tbv(7,3)=5
tbv(8,3)=6
tbv(9,3)=7
tbv(10,3)=8

'fill in the names
for r=1 to norows
for c=1 to nocols
    tbn$(r,c)="#main.tb"+right$("00"+str$(r),2)+right$("00"+str$(c),2)
    'print tbn$(r,c)
next
next

'set the foreground and background colors
'these apply to the window and all controls
BackgroundColor$ = "buttonface"
ForegroundColor$ = "black"
WindowWidth = 550
WindowHeight = 410
'create the special colored textboxes that will move about
'override the BackgroundColor$ for individual textbox
TextboxColor$ = "red"
textbox #main.tbr, -30,-20,40,20
TextboxColor$ = "green"
textbox #main.tbg, -30,-20,40,20
TextboxColor$ = "cyan"
textbox #main.tbc, -30,-20,40,20

'create the grid of static display textboxes
TextboxColor$ = "white"
textbox #main.tb0101, 30, 20, 40,20
textbox #main.tb0201, 30, 40, 40,20
textbox #main.tb0301, 30, 60, 40,20
textbox #main.tb0401, 30, 80, 40,20
textbox #main.tb0501, 30, 100,40,20
textbox #main.tb0601, 30, 120, 40,20
textbox #main.tb0701, 30, 140, 40,20
textbox #main.tb0801, 30, 160, 40,20
textbox #main.tb0901, 30, 180, 40,20
textbox #main.tb1001, 30, 200, 40,20

textbox #main.tb0102, 80, 20, 40,20
textbox #main.tb0202, 80, 40, 40,20
textbox #main.tb0302, 80, 60, 40,20
textbox #main.tb0402, 80, 80, 40,20
textbox #main.tb0502, 80, 100,40,20
textbox #main.tb0602, 80, 120, 40,20
textbox #main.tb0702, 80, 140, 40,20
textbox #main.tb0802, 80, 160, 40,20
textbox #main.tb0902, 80, 180, 40,20
textbox #main.tb1002, 80, 200, 40,20

textbox #main.tb0103, 130, 20, 40,20
textbox #main.tb0203, 130, 40, 40,20
textbox #main.tb0303, 130, 60, 40,20
textbox #main.tb0403, 130, 80, 40,20
textbox #main.tb0503, 130, 100,40,20
textbox #main.tb0603, 130, 120, 40,20
textbox #main.tb0703, 130, 140, 40,20
textbox #main.tb0803, 130, 160, 40,20
textbox #main.tb0903, 130, 180, 40,20
textbox #main.tb1003, 130, 200, 40,20

button #main.button,"Win", [clicked],UL,30, 240,40, 20
button #main.button,"Lose", [clicked],UL,30, 260,40, 20
button #main.button,"Draw", [clicked],UL,30, 280,40, 20
open "untitled" for window as #main
#main "trapclose [quit]"
'find all the textbox handles that can be selected
'and display their values

for r=1 to norows
for c=1 to nocols
    'hwnd() function cannot use an array
    h$=tbn$(r,c)
    tbh(r,c)= HWND(#h$)
    'print tbh(r,c)
    #h$ tbv(r,c)
next
next

'set focus on tb0101
newr=1
newc=1
newx=30
h$=tbn$(newr,newc)
i$="#main.tbc"
oldi$=i$
#i$ tbv(newr,newc)
gosub [placeinput]

'start polling checkhandle to see where the user is clicking
timer 50, [checkhandle]
wait

[checkhandle]
'check which textbox has focus now
found=0
CallDLL #user32, "GetFocus", handle as long
for r= 1 to norows
    for c= 1 to nocols
        if tbh(r,c)=handle then
            newr=r
            newc=c
            if newc=1 then newx=30
            if newc=2 then newx=80
            if newc=3 then newx=130

            'create handle variable for clicked textbox
            h$=tbn$(newr,newc)
            'decide which input textbox to use
            i$="#main.tbc"
            found=1
            exit for '#h$ #i$ and newr,newc and newx set
        end if
    next
    if found then exit for
next
if newr<>oldr or newc<>oldc then
    gosub [removeinput]
    gosub [placeinput]
end if
wait

[removeinput]
'hide input and show static display box
#oldi$ "!contents? v$"
tbv(oldr,oldc)=val(v$)
#oldi$ "!hide"
#oldh$ tbv(oldr,oldc)
#oldh$ "!show"
return

[placeinput]
'place the input textbox and hide original
#h$ "!hide"
#i$ "!locate ";newx;" ";newr*20;" 40 20"
#i$ tbv(newr,newc)
#i$ "!show"
#main "refresh"
#i$ "!setfocus"
oldh$=h$
oldi$=i$
oldr=newr
oldc=newc
return



[quit]
timer 0
close #main
end

 
« Last Edit: May 3rd, 2017, 02:00am by Rod » User IP Logged

tsh73
Board Moderator

member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1686
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #36 on: May 3rd, 2017, 04:29am »

I would suggest using drawn grid in a graphicbox instead of set of textbox, ant put single different-colored textbox over it to edit stuff
(much like grid demo).
The only problrm it will not support "exit on second click" behaviour.
User IP Logged

damned Dog in the Manger
Turtleman
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 187
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #37 on: May 3rd, 2017, 04:38am »

tsh73: Thanks for the suggestion, but I'm not familiar with the grid demo. Can you supply a link to the code or description? Thanks!
User IP Logged

tsh73
Board Moderator

member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1686
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #38 on: May 3rd, 2017, 08:17am »

I meant this one (has to search)
Data Grid / Spreadsheet
but if you search for "grid" on Code board you'll find others too.
« Last Edit: May 3rd, 2017, 08:17am by tsh73 » User IP Logged

damned Dog in the Manger
Rod
Global Moderator
ImageImageImageImageImage


member is online

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5562
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #39 on: May 4th, 2017, 06:21am »

To make it appear that the grid is inactive till clicked you can hold the focus on an off screen textbox.


Code:
nomainwin
norows=10
nocols=3
'create an array to hold the textbox handle names
dim tbn$(norows,nocols)
'create an array to hold the actual textbox handle (a numeric pointer)
dim tbh(norows,nocols)
'create an array to hold the textbox current values
dim tbv(norows,nocols)
'fill those values
tbv(1,1)=5
tbv(2,1)=10
tbv(3,1)=15
tbv(4,1)=20
tbv(5,1)=25
tbv(6,1)=30
tbv(7,1)=35
tbv(8,1)=40
tbv(9,1)=45
tbv(10,1)=22
tbv(1,2)=2
tbv(2,2)=3
tbv(3,2)=4
tbv(4,2)=5
tbv(5,2)=6
tbv(6,2)=6
tbv(7,2)=7
tbv(8,2)=8
tbv(9,2)=9
tbv(10,2)=1
tbv(1,3)=1
tbv(2,3)=1
tbv(3,3)=1
tbv(4,3)=2
tbv(5,3)=3
tbv(6,3)=4
tbv(7,3)=5
tbv(8,3)=6
tbv(9,3)=7
tbv(10,3)=8

'fill in the names
'the off screen tb
tbn$(0,0)="#main.tb0000"
for r=1 to norows
for c=1 to nocols
    tbn$(r,c)="#main.tb"+right$("00"+str$(r),2)+right$("00"+str$(c),2)
    'print tbn$(r,c)
next
next

'set the foreground and background colors
'these apply to the window and all controls
BackgroundColor$ = "buttonface"
ForegroundColor$ = "black"
WindowWidth = 550
WindowHeight = 410
'create the special colored textboxes that will move about
'override the BackgroundColor$ for individual textbox
TextboxColor$ = "red"
textbox #main.tbr, -30,-20,40,20
TextboxColor$ = "green"
textbox #main.tbg, -30,-20,40,20
TextboxColor$ = "cyan"
textbox #main.tbc, -30,-20,40,20

'create off screen textbox
TextboxColor$ = "white"
textbox #main.tb0000, -100,-100,40,20

'create the grid of static display textboxes
textbox #main.tb0101, 30, 20, 40,20
textbox #main.tb0201, 30, 40, 40,20
textbox #main.tb0301, 30, 60, 40,20
textbox #main.tb0401, 30, 80, 40,20
textbox #main.tb0501, 30, 100,40,20
textbox #main.tb0601, 30, 120, 40,20
textbox #main.tb0701, 30, 140, 40,20
textbox #main.tb0801, 30, 160, 40,20
textbox #main.tb0901, 30, 180, 40,20
textbox #main.tb1001, 30, 200, 40,20

textbox #main.tb0102, 80, 20, 40,20
textbox #main.tb0202, 80, 40, 40,20
textbox #main.tb0302, 80, 60, 40,20
textbox #main.tb0402, 80, 80, 40,20
textbox #main.tb0502, 80, 100,40,20
textbox #main.tb0602, 80, 120, 40,20
textbox #main.tb0702, 80, 140, 40,20
textbox #main.tb0802, 80, 160, 40,20
textbox #main.tb0902, 80, 180, 40,20
textbox #main.tb1002, 80, 200, 40,20

textbox #main.tb0103, 130, 20, 40,20
textbox #main.tb0203, 130, 40, 40,20
textbox #main.tb0303, 130, 60, 40,20
textbox #main.tb0403, 130, 80, 40,20
textbox #main.tb0503, 130, 100,40,20
textbox #main.tb0603, 130, 120, 40,20
textbox #main.tb0703, 130, 140, 40,20
textbox #main.tb0803, 130, 160, 40,20
textbox #main.tb0903, 130, 180, 40,20
textbox #main.tb1003, 130, 200, 40,20

button #main.win,"Win", [clicked],UL,30, 240,40, 20
button #main.los,"Lose", [clicked],UL,30, 260,40, 20
button #main.draw,"Draw", [clicked],UL,30, 280,40, 20
open "untitled" for window as #main
#main "trapclose [quit]"
'find all the textbox handles that can be selected
'and display their values
'get off screen tb handle
h$="#main.tb0000"
r=0
c=0
tbh(r,c)= HWND(#h$)

for r=1 to norows
for c=1 to nocols
    'hwnd() function cannot use an array
    h$=tbn$(r,c)
    tbh(r,c)= HWND(#h$)
    'print tbh(r,c)
    #h$ tbv(r,c)
next
next

'set focus on tb0000 off screen
newr=0
newc=0
newx=-100
h$=tbn$(newr,newc)
i$="#main.tb0000"
oldi$=i$
#i$ tbv(newr,newc)
gosub [placeinput]

'start polling checkhandle to see where the user is clicking
timer 50, [checkhandle]
wait

[checkhandle]
'check which textbox has focus now
found=0
CallDLL #user32, "GetFocus", handle as long
for r= 1 to norows
    for c= 1 to nocols
        if tbh(r,c)=handle then
            newr=r
            newc=c
            if newc=1 then newx=30
            if newc=2 then newx=80
            if newc=3 then newx=130

            'create handle variable for clicked textbox
            h$=tbn$(newr,newc)
            'decide which input textbox to use
            i$="#main.tbc"
            found=1
            exit for '#h$ #i$ and newr,newc and newx set
        end if
    next
    if found then exit for
next
if newr<>oldr or newc<>oldc then
    gosub [removeinput]
    gosub [placeinput]
end if
wait

[removeinput]
'hide input and show static display box
#oldi$ "!contents? v$"
tbv(oldr,oldc)=val(v$)
#oldi$ "!hide"
#oldh$ tbv(oldr,oldc)
#oldh$ "!show"
return

[placeinput]
'place the input textbox and hide original
#h$ "!hide"
#i$ "!locate ";newx;" ";newr*20;" 40 20"
#i$ tbv(newr,newc)
#i$ "!show"
#main "refresh"
#i$ "!setfocus"
oldh$=h$
oldi$=i$
oldr=newr
oldc=newc
return



[quit]
timer 0
close #main
end

 
« Last Edit: May 4th, 2017, 06:21am by Rod » User IP Logged

Turtleman
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 187
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #40 on: May 4th, 2017, 06:37am »

Thanks Rob. I have no idea why this code works, while my attempts to initially focus to an off screen textbox didn't permit clicking the first textbox until another was clicked first. Maybe I'll figure it out over the next few days. (SSD should arrive today, so computer upgrading will likely get in the way.)
User IP Logged

Turtleman
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 187
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #41 on: May 4th, 2017, 3:14pm »

Making progress! Went to a single column with additional rows and changed a lot of designators to mesh with the rest of my program, but it's looking hopeful. I'm now trying to figure out how to keep a newly entered value in the selected textbox without having to first select another textbox. Toggling the selected box back to white would be good, but that's currently resetting the new value to zero. Working on it!
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is online

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5562
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #42 on: May 5th, 2017, 02:49am »

Why does it matter that the textbox remains colored? It is the textbox that has current focus and the one the user can edit. To stop editing the user will press another control or button like stop editing or exit or move on. Then you can stop the polling and display all textboxes white.
User IP Logged

Turtleman
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 187
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #43 on: May 5th, 2017, 02:57am »

It probably doesn't matter, and is just a personal preference not to display cyan if editing is complete. Besides, the active textboxes will turn yellow when stepping through the sequences. I can already stop the polling and return to white with a button and should be able to make it toggle. Right now I just need to store and display new values.
User IP Logged

Turtleman
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 187
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #44 on: May 5th, 2017, 03:39am »

To be a little more specific, once a textbox has been selected (colored cyan), it needs to accept a new typed in value that will remain once the textbox has been deselected.
User IP Logged

Pages: 1 2 3 4  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls