Liberty BASIC Community Forum
« Grid control, spreadsheet »

Welcome Guest. Please Login or Register.
Mar 26th, 2017, 1:50pm


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


« Previous Topic | Next Topic »
Pages: 1 2  Notify Send Topic Print
 hotthread  Author  Topic: Grid control, spreadsheet  (Read 492 times)
cundo
Guru
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 602
thumbup Grid control, spreadsheet
« Thread started on: Jul 17th, 2016, 10:09am »

Not sure how this is called actually. One textbox, it moves around.
The first Code:
' Super simple grid control by cundo. Copyright 2016

nomainwin
    WindowWidth = 640 : WindowHeight = 480
    UpperLeftX = INT((DisplayWidth-WindowWidth)/2)
    UpperLeftY = INT((DisplayHeight-WindowHeight)/2)
    global TXTBOXWIDTH,TXTBOXHEIGHT
    TXTBOXWIDTH = 50
    TXTBOXHEIGHT = 25
    c = 10 : r = 10
    Dim ar$( c , r )
    Stylebits #main.tbx, _ES_LEFT, _WS_BORDER , 0, 0
    textbox #main.tbx, -2222, -2222, TXTBOXWIDTH, TXTBOXHEIGHT

    stylebits #main.gbx ,0,_WS_BORDER,0,0
    graphicbox #main.gbx, 0, 0, 800, 600

    ' set a hidden default button, to use it when the user presses the Enter key.
    stylebits #main.default ,_BS_DEFPUSHBUTTON,0,0,0
    button #main.default,"ok",[changes],ul,-1111,-1111,10,10

    Open "Grid" for window_nf as #main

    #main "trapclose [quit]"
    #main.gbx "down;fill buttonface;flush"
    #main.gbx "when leftButtonDown [lbd]"
    '#main.gbx "when characterInput [tabKey]"
    #main.tbx "!font courier_new 9"

    ' create grid with columns=c and rows=r
    a$ = gridControl$( "#main.gbx", c, r , 1 )



    wait

[tabKey] ' not yet
    if left$(Inkey$, 2) = chr$( _VK_TAB ) Then
        prev.getpos$=getpos$
        getX = cellx : getY = celly
        getX = getX + TXTBOXWIDTH
        if getX > TXTBOXWIDTH*c then getX = TXTBOXWIDTH : getY = getY + TXTBOXHEIGHT
        getpos$ = gridControl$( "#main.gbx", getX, getY , 2 )
        if prev.getpos$=getpos$ then getX=0 : getY=0
        cellx = getX : celly = getY
    end if
    wait

[lbd]

    'get the value of grid
    b$ = gridControl$( "#main.gbx", MouseX, MouseY , 2 )
    #main.gbx "color black;backcolor buttonface"
    #main.gbx "place 4 ";r*TXTBOXHEIGHT+TXTBOXHEIGHT*2
    #main.gbx "\Current cell value : ";  b$ ; space$(50)
    cellx = MouseX : celly = MouseY
    wait


[changes]
    'set the value of grid
    c$ = gridControl$( "#main.gbx", cellx, celly , 3 )
    #main.gbx "color red;backcolor buttonface"
    #main.gbx "place 4 ";r*TXTBOXHEIGHT+TXTBOXHEIGHT*2
    #main.gbx "\Setting cell value : "; c$ ; space$(50)
    #main.gbx "setfocus"
    wait

[quit]
    close #main : end


Function gridControl$( gfxh$, cx, cy, type )
' type=1 create, type=2 get value, type=3 change value
'cx=cx-1 : cy = cy-1
    #gfxh$ "backcolor buttonface"
    #gfxh$ "font Courier_new 7 16 bold"
 if type=1 then ' fill array, create grid
 global maxx , maxy '!!!!!!!!!!!!!!!!
    maxx = cx : maxy = cy
    for x= 0 to maxx
     for y = 0 to maxy
        ar$(x,y)= str$(int(rnd(1)*TXTBOXWIDTH+.5)) ' random values for testing
        #gfxh$ "place "; x*TXTBOXWIDTH ; " "; y*TXTBOXHEIGHT
        #gfxh$ "color black"
        #gfxh$ "boxfilled "; x*TXTBOXWIDTH+TXTBOXWIDTH ; " "; y*TXTBOXHEIGHT+TXTBOXHEIGHT
        ' some initial values
        #gfxh$ "place ";x*TXTBOXWIDTH+4;" ";y*TXTBOXHEIGHT+15
        #gfxh$ "color black"
        #gfxh$ "|";ar$(x,y);
    next y : next x
    #gfxh$ "flush"
    #gfxh$ "setfocus"
 end if

 if type=2 then
    oldx = x : oldy = y
    x = int(cx/TXTBOXWIDTH)*TXTBOXWIDTH
    y = int(cy/TXTBOXHEIGHT)*TXTBOXHEIGHT
    idx= int(x/TXTBOXWIDTH) : idy= int(y/TXTBOXHEIGHT)
    if idx>maxx or idy>maxy then exit Function

        #main.tbx "!locate ";x;" ";y;" ";TXTBOXWIDTH;" ";TXTBOXHEIGHT
        #main.tbx ar$( idx , idy)
        #main.tbx "!selectall"
        mainHandle$ = word$( gfxh$,1,"." )
        #mainHandle$ "refresh"
        gridControl$ =  idx;"; ";idy;" = ";ar$( idx , idy)
 end if
 if type=3 then
    oldx = x : oldy = y
    x = int(cx/TXTBOXWIDTH)*TXTBOXWIDTH
    y = int(cy/TXTBOXHEIGHT)*TXTBOXHEIGHT
    idx= int(x/TXTBOXWIDTH) : idy= int(y/TXTBOXHEIGHT)
 '   if idx>c-1 or idy>r-1 then exit Function
        #main.tbx "!contents? cont$"
        ar$( idx , idy ) = cont$
        #main.tbx "!locate -1111 -1111 10 10" ' far away
        #main "refresh"
         #gfxh$ "place "; idx*TXTBOXWIDTH+1 ; " "; idy*TXTBOXHEIGHT+1
         #gfxh$ "color cyan"
         #gfxh$ "boxfilled "; idx*TXTBOXWIDTH+TXTBOXWIDTH-1 ; " "; idy*TXTBOXHEIGHT+TXTBOXHEIGHT-1
         #gfxh$ "stringWidth? cont$ sw"
        if (7+sw*7)>TXTBOXWIDTH then cont$= mid$( cont$, 1 , int(TXTBOXWIDTH/7 - .5) )
         #gfxh$ "place ";idx*TXTBOXWIDTH+4;" ";idy*TXTBOXHEIGHT+15
         #gfxh$ "color black "
         #gfxh$ "|";cont$;
         gridControl$  = idx;"; ";idy;" = "; ar$( idx , idy )
         #gfxh$ "setfocus"
 end if
End Function
 

I like more this one Code:
' Super simple grid control by cundo. Copyright 2016
nomainwin
    WindowWidth = 800 : WindowHeight = 600
    UpperLeftX = INT((DisplayWidth-WindowWidth)/2)
    UpperLeftY = INT((DisplayHeight-WindowHeight)/2)
    global TXTBOXWIDTH,TXTBOXHEIGHT, FlushSegID
    TXTBOXWIDTH = 80
    TXTBOXHEIGHT = 40
    c = 5 : r = 7 ' change these values: columns and rows

    Dim ar$( c , r )

    Stylebits #main.tbx, _ES_LEFT, _WS_BORDER , 0, 0
    textbox #main.tbx, -2222, -2222, TXTBOXWIDTH, TXTBOXHEIGHT

    stylebits #main.gbx ,0,_WS_BORDER,0,0
    graphicbox #main.gbx, 0, 0, WindowWidth, WindowHeight

    ' place a default button, to use it when the user presses the Enter key.
    stylebits #main.default ,_BS_DEFPUSHBUTTON,0,0,0
    button #main.default,"ok",[changes],ul,-1111,-1111,10,10

    Open "Grid" for window as #main

    #main "trapclose [quit]"
    #main.gbx "down;fill buttonface;flush"
    #main.gbx "when leftButtonDown [lbd]"
    ' create grid with columns=c and rows=r
    a$ = gridControl$( "#main.gbx", c, r , 1 )
    wait

[tabKey] ' not yet
    if left$(Inkey$, 2) = chr$( _VK_TAB ) Then
        prev.getpos$=getpos$
        getX = cellx : getY = celly
        getX = getX + TXTBOXWIDTH
        if getX > TXTBOXWIDTH*c then getX = TXTBOXWIDTH : getY = getY + TXTBOXHEIGHT
        getpos$ = gridControl$( "#main.gbx", getX, getY , 2 )
        if prev.getpos$=getpos$ then getX=0 : getY=0
        cellx = getX : celly = getY
    end if
    wait
[lbd]
    'get the value of grid
    b$ = gridControl$( "#main.gbx", MouseX, MouseY , 2 )
    cellx = MouseX : celly = MouseY ' to use with the [changes] code
    text$ ="color black;|Current cell value : ";  b$ ; space$(50)
    goto [drawText]
    wait
[changes]
    'set the value of grid
    c$ = gridControl$( "#main.gbx", cellx, celly , 3 )
    text$ ="color red;|New cell value : "; c$ ; space$(50)
[drawText]
    #main.gbx "color red;backcolor buttonface"
    #main.gbx "place 4 ";r*TXTBOXHEIGHT+TXTBOXHEIGHT*2
    #main.gbx text$
    wait
[quit]
    close #main : end

Function gridControl$( gfxh$, cx, cy, type )
' type=1 create, type=2 get value, type=3 change value
    fontW = 10 : fontH = int(TXTBOXHEIGHT/2 ) + 1
 #gfxh$ "font Courier_new "; fontW; " "; fontH; " bold"
 #main.tbx "!font courier_new "; fontW; " "; fontH; " bold"

 select case type  ' fill array, create grid
 case 1
    global maxx , maxy '!!!!!!!!!!!!!!!!
    maxx = cx : maxy = cy

    for x= 0 to maxx
     for y = 0 to maxy

        ar$(x,y)= str$(int(rnd(1)*TXTBOXWIDTH+.5)) ' random values for testing
        if x=0 or y=0 then
                #gfxh$ "backcolor 255 220 150"
            else
                #gfxh$ "backcolor buttonface"
        end if
        if x=0 then
            ar$(x,y)= ""; cnt1
            cnt1 = cnt1 +1
        end if
        if y = 0 then
            cnt2 = cnt2+1
            ar$(x,y)= upper$(mid$(" abcdefghijklmnopqrstuvwxyz",cnt2,1))
        end if
        #gfxh$ "place "; x*TXTBOXWIDTH ; " "; y*TXTBOXHEIGHT
        #gfxh$ "color black"
        #gfxh$ "boxfilled "; x*TXTBOXWIDTH+TXTBOXWIDTH ; " "; y*TXTBOXHEIGHT+TXTBOXHEIGHT
        ' some initial values
        #gfxh$ "place ";x*TXTBOXWIDTH+fontW;" ";y*TXTBOXHEIGHT+fontH
        #gfxh$ "color black"
        #gfxh$ "|";ar$(x,y);
    next y : next x
    #gfxh$ "setfocus"

 case 2
    oldx = x : oldy = y
    x = int(cx/TXTBOXWIDTH)*TXTBOXWIDTH
    y = int(cy/TXTBOXHEIGHT)*TXTBOXHEIGHT
    idx= int(x/TXTBOXWIDTH) : idy= int(y/TXTBOXHEIGHT)
    if idx <=0 or idy <=0 then exit Function
    if idx > maxx or idy > maxy then exit Function

        #main.tbx "!locate ";x;" ";y;" ";TXTBOXWIDTH;" ";TXTBOXHEIGHT
        #main.tbx ar$( idx , idy)
        #main.tbx "!selectall"
        mainHandle$ = word$( gfxh$,1,"." )
        #mainHandle$ "refresh"
        gridControl$ =  idx;"; ";idy;" = ";ar$( idx , idy)
 case 3
    #gfxh$ "redraw"
    oldx = x : oldy = y
    x = int(cx/TXTBOXWIDTH)*TXTBOXWIDTH
    y = int(cy/TXTBOXHEIGHT)*TXTBOXHEIGHT
    idx= int(x/TXTBOXWIDTH) : idy= int(y/TXTBOXHEIGHT)
        #main.tbx "!contents? cont$"
        ar$( idx , idy ) = cont$
        #main.tbx "!locate -1111 -1111 10 10" ' far away
        #main "refresh"

        ' colored box around the last change
         #gfxh$ "place "; idx*TXTBOXWIDTH ; " "; idy*TXTBOXHEIGHT+1
         #gfxh$ "color red"
         #gfxh$ "boxFilled "; idx*TXTBOXWIDTH+TXTBOXWIDTH ; " "; idy*TXTBOXHEIGHT+TXTBOXHEIGHT-1
         #gfxh$ "discard"

        ' cut out the string
        do
            if limit=0 then  limit=len(cont$)
            scan
           ' print "limit",limit,"TXTBOXWIDTH",TXTBOXWIDTH
            cont$= mid$( cont$, 1 , limit )
            limit=limit-1
            #gfxh$ "stringWidth? cont$ sw"
        loop until sw < TXTBOXWIDTH

        ' fill with spaces
        while  sw <  TXTBOXWIDTH - fontW
            scan
            cont$=  cont$ ;" "
            #gfxh$ "stringWidth? cont$ sw"
        wend
         #gfxh$ "place ";idx*TXTBOXWIDTH+int(fontW/2);" ";idy*TXTBOXHEIGHT +fontH + 1
         #gfxh$ "color black "
         #gfxh$ "|";cont$;
         gridControl$  = idx;"; ";idy;" = "; ar$( idx , idy )
        ' #gfxh$ "font Courier_new "; fontW; " "; fontH; " bold"
         #gfxh$ "setfocus"
 end select

    #gfxh$ "delsegment ";FlushSegID-1
    #gfxh$ "flush FlushSegID"
   ' print FlushSegID
End Function
 
« Last Edit: Jul 17th, 2016, 10:10am by cundo » User IP Logged

cundo
sarmednafi
New Member
Image


member is offline

Avatar




PM


Posts: 18
thumbup Re: Grid control, spreadsheet
« Reply #1 on: Jul 17th, 2016, 2:18pm »

Both needs a very hard work to get the benefits of separate sheets. Then we will find our self get involved into an endless code, don't know if we are in correct direction or not.
I think three elements solve the problem of separate sheet simply.

1- An input line (we create) consist of text boxes and combo boxes etc..
2- the example of list view that driven by Rod recently (it is old example).
2- The example of child window with scroll bars by Alyce.

And if we want to make the life much easy, we should use the wonderful scroll bars by Richard Russel (optional).

« Last Edit: Jul 17th, 2016, 3:51pm by sarmednafi » User IP Logged

tsh73
Board Moderator

member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1609
thumbup Re: Grid control, spreadsheet
« Reply #2 on: Jul 17th, 2016, 2:30pm »

First code after setting new value (enter) and switching focus to other cells, shows old values then unselected.
But if I click on cell, I see new values.
User IP Logged

The existence of bug reports means somebody is using the software and actually cares to report back to you that he is having a problem with it, instead of just deleting it from their hard disk.
(Janusz Marcin Gorycki)
cundo
Guru
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 602
thumbup Re: Grid control, spreadsheet
« Reply #3 on: Jul 17th, 2016, 2:37pm »

Quote:
Both needs a very hard work to get the benefits of separate sheets
Please explain what would be those benefits.
Of course extra code is needed to do something with the data, but I'm not a fan of spreadsheets. I studied lotus 123 when I was 9/10 years old, it was something really horrible and boring.
My code is just a bare bones demo. I don't know what to do with it, doesn't have a purpose.
The way it is right now, you create a grid, and then retrieve or enter data. That is. Very very simple and easy.


User IP Logged

cundo
cundo
Guru
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 602
xx Re: Grid control, spreadsheet
« Reply #4 on: Jul 17th, 2016, 2:42pm »

on Jul 17th, 2016, 2:30pm, tsh73 wrote:
First code after setting new value (enter) and switching focus to other cells, shows old values then unselected.
But if I click on cell, I see new values.
Yes, that can happen, the thing is I have so many versions of each code I do that I started to do a mess with the filenames, that actually happens to me very often haha. It is probably a missing Flush, plus the Refresh command to move the textbox out.
User IP Logged

cundo
RNBW
Junior Member
ImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 63
thumbup Re: Grid control, spreadsheet
« Reply #5 on: Jul 19th, 2016, 10:14am »

Seeing Cundo's code set me thinking when I was trying to solve setting up a grid of textboxes. Using only LB's native code, I couldn't find a satisfactory solution and I ended up using a solution from LBB, which works very nicely, but not compatible with LB.

I wondered if something could be done using the Windows API. I have done this successfully on Freebasic. I found some old code based on something I found on one of the old newsletters in API Corner (if only I could find it again).

The following is the code I produced. The only problem with it is that the NOTICE only displays code entered in the very last cell. How do I set the focus in the other cells so that it displays code entered in any cell?

Code:
'=================================================
'   CREATE TEXTBOX GRID CreateTextboxAPI_Grid.bas
'   USING WINDOWS API
'=================================================
' modified from LB Newsletter API Corner
'=================================================

NOMAINWIN

MENU #1, "&File","&Read", [readIt],_
        "E&xit", [quit]

NOMAINWIN
[WindowSetup]
WindowWidth = 440 : WindowHeight = 400
UpperLeftX = INT((DisplayWidth-WindowWidth)/2)
UpperLeftY = INT((DisplayHeight-WindowHeight)/2)
' -------------------------------------------------

OPEN "API Textbox Grid" FOR WINDOW AS #1
PRINT #1, "trapclose [quit]"

startH = 20: startV = 20: hRow = 20 : width = 50

FOR row = 1 to 10
   FOR col = 1 to 5
      hPos = startH + (col-1)*width
      vPos = startV + (row-1) * hRow
      hT = CreateTextbox(hwnd(#1),hPos,vPos,width+1,hRow+1)
   NEXT col
NEXT row

CALL SetFocus hT
WAIT

[quit]
    CLOSE #1: END

[readIt]
    txt$ = GetWindowText$(hT)
    NOTICE txt$
    WAIT

'--------------------------------------------
'    SUBs and FUNCTIONs
'--------------------------------------------

' Set the focus in the Texbox
SUB SetFocus hWnd
    CALLDLL #user32, "SetFocus",_
    hWnd AS LONG,_
    result AS LONG
END SUB

'Get the text entered into the Textbox
FUNCTION GetWindowText$(hWnd)
    total = GetWindowTextLength(hWnd)
    Title$ = SPACE$(total) + CHR$(0): l= LEN(Title$)

    CALLDLL #user32, "GetWindowTextA",_
    hWnd AS LONG,_
    Title$ AS PTR,_
    l AS LONG,_
    result AS LONG
    GetWindowText$ = TRIM$(Title$)
END FUNCTION

'Get the length of the text entered into Textbox
FUNCTION GetWindowTextLength(hW)
    CALLDLL #user32, "GetWindowTextLengthA",_
    hW AS LONG,_
    GetWindowTextLength AS LONG
END FUNCTION

' Function to create Textbox
FUNCTION CreateTextbox(hW, x, y, w, h)
    style = _WS_CHILDWINDOW OR _WS_BORDER _
    OR _WS_VISIBLE
    hInst = GetWindowLong(hW, _GWL_HINSTANCE)

    CALLDLL #user32, "CreateWindowExA",_
        0 AS LONG,"EDIT" AS PTR,_
        "" AS PTR, style AS LONG,_
        x AS LONG,_
        y AS LONG,_
        w AS LONG,_
        h AS LONG,_
        hW AS LONG,_
        0 AS LONG,_
        hInst AS LONG,_
        0 AS LONG,_
        CreateTextbox AS LONG
END FUNCTION

FUNCTION GetWindowLong(hW, type)
    CALLDLL #user32, "GetWindowLongA", _
    hW AS LONG,_
    type AS LONG,_
    GetWindowLong AS LONG
END FUNCTION

 
User IP Logged

cundo
Guru
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 602
thumbup Re: Grid control, spreadsheet
« Reply #6 on: Jul 19th, 2016, 11:01am »

Hi RNBW, how do I reproduce the error?

EDIT:
Code:
'=================================================
'   CREATE TEXTBOX GRID CreateTextboxAPI_Grid.bas
'   USING WINDOWS API
'=================================================
' modified from LB Newsletter API Corner
'=================================================

NOMAINWIN

MENU #1, "&File","&Read", [readIt],_
        "E&xit", [quit]

NOMAINWIN
[WindowSetup]
WindowWidth = 440 : WindowHeight = 400
UpperLeftX = INT((DisplayWidth-WindowWidth)/2)
UpperLeftY = INT((DisplayHeight-WindowHeight)/2)
' -------------------------------------------------

OPEN "API Textbox Grid" FOR WINDOW AS #1
PRINT #1, "trapclose [quit]"

startH = 20: startV = 20: hRow = 20 : width = 50
dim hT(12*6)
FOR row = 1 to 12
   FOR col = 1 to 6
      hPos = startH + (col-1)*width
      vPos = startV + (row-1) * hRow
      idx=idx+1
      hT(idx) = CreateTextbox(hwnd(#1),hPos,vPos,width+1,hRow+1)
   NEXT col
NEXT row

CALL SetFocus hT(10)
WAIT

[quit]
    CLOSE #1: END

[readIt]
    handle = GetFocus()
    txt$ = GetWindowText$( handle )
    NOTICE txt$
    WAIT

'--------------------------------------------
'    SUBs and FUNCTIONs
'--------------------------------------------

' Get the focus in the Texbox
Function GetFocus()
    CALLDLL #user32, "GetFocus",_
    GetFocus AS uLONG
END Function

' Set the focus in the Texbox
SUB SetFocus hWnd
    CALLDLL #user32, "SetFocus",_
    hWnd AS uLONG,_
    result AS LONG
END SUB

'Get the text entered into the Textbox
FUNCTION GetWindowText$(hWnd)
    total = GetWindowTextLength(hWnd)
    Title$ = SPACE$(total) + CHR$(0): l= LEN(Title$)

    CALLDLL #user32, "GetWindowTextA",_
    hWnd AS uLONG,_
    Title$ AS PTR,_
    l AS LONG,_
    result AS LONG
    GetWindowText$ = TRIM$(Title$)
END FUNCTION

'Get the length of the text entered into Textbox
FUNCTION GetWindowTextLength(hW)
    CALLDLL #user32, "GetWindowTextLengthA",_
    hW AS uLONG,_
    GetWindowTextLength AS LONG
END FUNCTION

' Function to create Textbox
FUNCTION CreateTextbox(hW, x, y, w, h)
    style = _WS_CHILDWINDOW OR _WS_BORDER _
    OR _WS_VISIBLE
    hInst = GetWindowLong(hW, _GWL_HINSTANCE)

    CALLDLL #user32, "CreateWindowExA",_
        0 AS LONG,"EDIT" AS PTR,_
        "" AS PTR, style AS LONG,_
        x AS LONG,_
        y AS LONG,_
        w AS LONG,_
        h AS LONG,_
        hW AS uLONG,_
        0 AS LONG,_
        hInst AS LONG,_
        0 AS LONG,_
        CreateTextbox AS uLONG
END FUNCTION

FUNCTION GetWindowLong(hW, type)
    CALLDLL #user32, "GetWindowLongA", _
    hW AS uLONG,_
    type AS LONG,_
    GetWindowLong AS LONG
END FUNCTION

 

I did this fast, so don't know if this is correct, but using an array to hold the handles and the getFocus API to know which one has the focus
Edit 2 : all handles should be uLong
Edit 3 : getFocus doesn't expect a handle.
« Last Edit: Jul 19th, 2016, 11:15am by cundo » User IP Logged

cundo
RNBW
Junior Member
ImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 63
thumbup Re: Grid control, spreadsheet
« Reply #7 on: Jul 19th, 2016, 12:04pm »

Cundo

Thankyou for the very quick response, which works. Pretty obvious really GetFocus. Why didn't I think of that. Old age.

One statement that I don't understand is

Code:
CALL SetFocus hT(10) 


It works, but why 10?
User IP Logged

cundo
Guru
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 602
thumbup Re: Grid control, spreadsheet
« Reply #8 on: Jul 19th, 2016, 12:33pm »

It's not important. Just a random handle to see if it was working.
« Last Edit: Jul 19th, 2016, 12:34pm by cundo » User IP Logged

cundo
RNBW
Junior Member
ImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 63
thumbup Re: Grid control, spreadsheet
« Reply #9 on: Jul 19th, 2016, 12:41pm »

Cundo

I REMd out the line and it works without the line at all.
User IP Logged

cundo
Guru
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 602
thumbup Re: Grid control, spreadsheet
« Reply #10 on: Jul 19th, 2016, 12:59pm »

Yes it is the same, but if you have for example:
CALL SetFocus hT(1) You can start typing, avoiding selecting the grid cell first.
User IP Logged

cundo
RNBW
Junior Member
ImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 63
thumbup Re: Grid control, spreadsheet
« Reply #11 on: Jul 20th, 2016, 09:45am »

Cundo,

Thank you for your help.

I may want to format some rows and columns differently (left, right and centre justification for example).

How would I go about this. With native commands I would just use Stylebits.

I've tried to set up Style as Global and introduced Style2 (_ES_RIGHT) also Global and this works by putting this into the loop. I have also added style into the CreateTextbox function.

This works by making all the texboxes right justified, but for some reason deletes the very first textbox. I can't see why. It only happens when Global Style, Style2 is included in the CreateTextbox function.

Any ideas?

Code:
'===================================================
'   CREATE TEXTBOX GRID CreateTextboxAPI_Grid_4.bas
'   USING WINDOWS API
'===================================================
' modified from LB Newsletter API Corner
' with corrections by Cundo LB Conforum 19 July 2016
' Further modifications by RNBW 20 July 2016
'===================================================

NOMAINWIN

MENU #1, "&File","&Read", [readIt],_
        "E&xit", [quit]

NOMAINWIN
[WindowSetup]
WindowWidth = 440 : WindowHeight = 400
UpperLeftX = INT((DisplayWidth-WindowWidth)/2)
UpperLeftY = INT((DisplayHeight-WindowHeight)/2)
' -------------------------------------------------

OPEN "API Textbox Grid" FOR WINDOW AS #1
PRINT #1, "trapclose [quit]"

startH = 20: startV = 20: hRow = 20 : width = 50
'dim hT(12*6)
DIM hT(12,6)
'Style = Style + Style2
FOR row = 1 to 12
   FOR col = 1 to 6
      hPos = startH + (col-1)*width
      vPos = startV + (row-1) * hRow
      Style = Style + Style2
      'idx=idx+1
      'hT(idx) = CreateTextbox(hwnd(#1),hPos,vPos,width+1,hRow+1)

      hT(row,col) = CreateTextbox(hwnd(#1),hPos,vPos,width+1,hRow+1,Style)
   NEXT col
NEXT row


'DOESN'T SEEM TO NEED THE NEXT ROW!!!!
'CALL SetFocus hT(10)
WAIT

[quit]
    CLOSE #1: END

[readIt]
    handle = GetFocus()
    txt$ = GetWindowText$( handle )
    NOTICE txt$
    WAIT

'--------------------------------------------
'    SUBs and FUNCTIONs
'--------------------------------------------

' Get the focus in the Texbox
Function GetFocus()
    CALLDLL #user32, "GetFocus",_
    GetFocus AS uLONG
END Function

' Set the focus in the Texbox
SUB SetFocus hWnd
    CALLDLL #user32, "SetFocus",_
    hWnd AS uLONG,_
    result AS LONG
END SUB

'Get the text entered into the Textbox
FUNCTION GetWindowText$(hWnd)
    total = GetWindowTextLength(hWnd)
    Title$ = SPACE$(total) + CHR$(0): l= LEN(Title$)

    CALLDLL #user32, "GetWindowTextA",_
    hWnd AS uLONG,_
    Title$ AS PTR,_
    l AS LONG,_
    result AS LONG
    GetWindowText$ = TRIM$(Title$)
END FUNCTION

'Get the length of the text entered into Textbox
FUNCTION GetWindowTextLength(hW)
    CALLDLL #user32, "GetWindowTextLengthA",_
    hW AS uLONG,_
    GetWindowTextLength AS LONG
END FUNCTION

' Function to create Textbox
FUNCTION CreateTextbox(hW, x, y, w, h, Style)
    Global Style, Style2
    Style = _WS_CHILDWINDOW OR _WS_BORDER _
                            OR _WS_VISIBLE
    Style2 = _ES_RIGHT
    hInst = GetWindowLong(hW, _GWL_HINSTANCE)

    CALLDLL #user32, "CreateWindowExA",_
        0 AS LONG,_
        "EDIT" AS PTR,_
        "" AS PTR,_
        Style AS LONG,_
        x AS LONG,_
        y AS LONG,_
        w AS LONG,_
        h AS LONG,_
        hW AS uLONG,_
        0 AS LONG,_
        hInst AS LONG,_
        0 AS LONG,_
        CreateTextbox AS uLONG
END FUNCTION

FUNCTION GetWindowLong(hW, type)
    CALLDLL #user32, "GetWindowLongA", _
    hW AS uLONG,_
    type AS LONG,_
    GetWindowLong AS LONG
END FUNCTION
 
User IP Logged

RNBW
Junior Member
ImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 63
thumbup Re: Grid control, spreadsheet
« Reply #12 on: Jul 21st, 2016, 05:15am »

Solved it!

Take the Global declaration outside the Function CreateTextbox() and put it in the main body of the program. Then put the required Style in the loop setting up the grid and Bob's Your Uncle! it works.

I've now set it up so that the textboxes are right justified and accept numbers 0 to 9 only.

Code:
'===================================================
'   CREATE TEXTBOX GRID CreateTextboxAPI_Grid_5.bas
'   USING WINDOWS API
'===================================================
' modified from LB Newsletter API Corner
' with corrections by Cundo LB Conforum 19 July 2016
' Further modifications by RNBW 21 July 2016
'===================================================

NOMAINWIN

MENU #1, "&File","&Read", [readIt],_
        "E&xit", [quit]

NOMAINWIN
[WindowSetup]
WindowWidth = 440 : WindowHeight = 400
UpperLeftX = INT((DisplayWidth-WindowWidth)/2)
UpperLeftY = INT((DisplayHeight-WindowHeight)/2)
' -------------------------------------------------

OPEN "API Textbox Grid" FOR WINDOW AS #1
PRINT #1, "trapclose [quit]"

' Set up the Textbox Styles as Global
Global Style, Style1

startH = 20: startV = 20: hRow = 20 : width = 50
DIM hT(12,6)
FOR row = 1 to 12
   FOR col = 1 to 6
      ' Set up the style for the Textboxes
      Style = _WS_CHILDWINDOW OR _WS_BORDER OR _WS_VISIBLE OR _ES_RIGHT OR _ES_NUMBER
      hPos = startH + (col-1)*width
      vPos = startV + (row-1) * hRow
      'idx=idx+1
      'hT(idx) = CreateTextbox(hwnd(#1),hPos,vPos,width+1,hRow+1)
      hT(row,col) = CreateTextbox(hwnd(#1),hPos,vPos,width+1,hRow+1,Style)
   NEXT col
NEXT row

WAIT

[quit]
    CLOSE #1: END

[readIt]
    handle = GetFocus()
    txt$ = GetWindowText$( handle )
    NOTICE txt$
    WAIT

'--------------------------------------------
'    SUBs and FUNCTIONs
'--------------------------------------------

' Get the focus in the Texbox
Function GetFocus()
    CALLDLL #user32, "GetFocus",_
    GetFocus AS uLONG
END Function

' Set the focus in the Texbox
SUB SetFocus hWnd
    CALLDLL #user32, "SetFocus",_
    hWnd AS uLONG,_
    result AS LONG
END SUB

'Get the text entered into the Textbox
FUNCTION GetWindowText$(hWnd)
    total = GetWindowTextLength(hWnd)
    Title$ = SPACE$(total) + CHR$(0): l= LEN(Title$)

    CALLDLL #user32, "GetWindowTextA",_
    hWnd AS uLONG,_
    Title$ AS PTR,_
    l AS LONG,_
    result AS LONG
    GetWindowText$ = TRIM$(Title$)
END FUNCTION

'Get the length of the text entered into Textbox
FUNCTION GetWindowTextLength(hW)
    CALLDLL #user32, "GetWindowTextLengthA",_
    hW AS uLONG,_
    GetWindowTextLength AS LONG
END FUNCTION

' Function to create Textbox
FUNCTION CreateTextbox(hW, x, y, w, h, Style)
    Style = _WS_CHILDWINDOW OR _WS_BORDER _
                            OR _WS_VISIBLE
    hInst = GetWindowLong(hW, _GWL_HINSTANCE)

    CALLDLL #user32, "CreateWindowExA",_
        0 AS LONG,_
        "EDIT" AS PTR,_
        "" AS PTR,_
        Style AS LONG,_
        x AS LONG,_
        y AS LONG,_
        w AS LONG,_
        h AS LONG,_
        hW AS uLONG,_
        0 AS LONG,_
        hInst AS LONG,_
        0 AS LONG,_
        CreateTextbox AS uLONG
END FUNCTION

FUNCTION GetWindowLong(hW, type)
    CALLDLL #user32, "GetWindowLongA", _
    hW AS uLONG,_
    type AS LONG,_
    GetWindowLong AS LONG
END FUNCTION
 


Hope it's of use to someone and also that someone might progress it further to get over some of LB's problems in respect of the construction of simple grids.
User IP Logged

cundo
Guru
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 602
thumbup Re: Grid control, spreadsheet
« Reply #13 on: Jul 21st, 2016, 1:10pm »

I started to do a mashup (mixing?) : Code:
'=================================================
'   CREATE TEXTBOX GRID CreateTextboxAPI_Grid.bas
'   USING WINDOWS API
'=================================================
' modified from LB Newsletter API Corner
'=================================================
' Some mods by RNBW 2016
'=================================================
' Some mods by cundo 2016 : one textbox, moves around, uses a double array.
'       I removed the API part too
'=================================================

nomainwin

MENU #1, "&File","&Read", [readIt],_
        "E&xit", [quit]


[WindowSetup]
WindowWidth = 640 : WindowHeight = 480
UpperLeftX = INT((DisplayWidth-WindowWidth)/2)
UpperLeftY = INT((DisplayHeight-WindowHeight)/2)
BackColorForAll$ = "White"
TextboxColor$ = "yellow"

startH = 20: startV = 20 : hRow = 30 : width = 100
r = 12 : c = 6
' -------------------------------------------------
stylebits #1.txb ,0,_WS_BORDER,0,0
textbox #1.txb,-1111,0,width,hRow
graphicbox #1.gbx 0,0,640,480
OPEN "More Grid" FOR WINDOW AS #1
PRINT #1, "trapclose [quit]"
#1.gbx "down;fill 255 215 190"
#1.gbx "color black; backcolor ";BackColorForAll$
#1.gbx "when leftButtonDown [lbd]"
#1.gbx "font courier_new 7 16"
#1.txb "!font courier_new 7 16"

dim hT(12*6)
dim ar$(6,12)
FOR row = 1 to r
   FOR col = 1 to c
      hPos = startH + (col-1)*width
      vPos = startV + (row-1) * hRow
    #1.gbx "place "; hPos; " "; vPos
    #1.gbx "boxfilled ";width+hPos;" ";hRow+vPos
   NEXT col
NEXT row
#1.gbx "flush" ' 1st flush

#1 "refresh"

WAIT

[lbd]

    #1.txb "!contents? cont$"
    ar$(ix,iy) = cont$

    if xpos>0 and ypos>0 then
        ' Make the graphics to stay on screen
        ' try to avoid the caret
        #1.txb cont$;space$(100)
            scan
        '*************************************
        ' #1.gbx "getbmp tmpbmp "; xpos +4; " "; ypos +4; " "; width-4 ; " "; hRow -5
        ' #1.gbx "drawbmp tmpbmp "; xpos +3; " "; ypos +4
        #1.gbx "getbmp tmpbmp 0 0 640 480"
        #1.gbx "drawbmp tmpbmp 0 0"
        unloadbmp "tmpbmp"

        #1.gbx "delsegment ";segId-1
        #1.gbx "segment segId"
        #1.gbx "flush"
    end if

    prevx = xpos +1 : prevy = ypos +1
    xpos = width * int( (MouseX-startH) / width )+ startH
    ypos = hRow * int( (MouseY-startV) / hRow )+ startV
    ix = int(xpos/width)
    iy = int(ypos/hRow)

    if ix >= 0 and iy >= 0 then
     if ix<c and iy<r then
        #1.txb "!locate " ; xpos +2; " "; ypos +2; " "; width -2; " "; hRow -2
        #1.txb ar$(ix,iy)
        #1.txb "!selectall"
        #1 "refresh"
     end if
    end if

WAIT

[quit]
    CLOSE #1: END

[readIt]
   #1.txb "!contents? cont$"
    NOTICE cont$
    WAIT
 
User IP Logged

cundo
cundo
Guru
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 602
thumbup Re: Grid control, spreadsheet
« Reply #14 on: Jul 21st, 2016, 8:08pm »

You can add some simple math now to the cells beginning with an =, example : =11+6


Code:
'=================================================
'   CREATE TEXTBOX GRID CreateTextboxAPI_Grid.bas
'   USING WINDOWS API
'=================================================
' modified from LB Newsletter API Corner
'=================================================
' Some mods by RNBW 2016
'=================================================
' Some mods by cundo 2016 : one textbox, moves around, uses a double array.
'       I removed the API part too
'=================================================
nomainwin


MENU #1, "&File", "&Read", [readIt], "E&xit", [quit]

[WindowSetup]
WindowWidth = 800 : WindowHeight = 480
UpperLeftX = INT((DisplayWidth-WindowWidth)/2)
UpperLeftY = INT((DisplayHeight-WindowHeight)/2)
BackColorForAll$ = "WHITE"
TextboxColor$ = BackColorForAll$
fontForAll$="font courier_new 7 16"
startH = 10 : startV = 10 : hRow = 25 : width = 70
r = 12 : c = 10
gfxWidth = width*c+startH*2+2
gfxHeight = hRow*r+startV*2+2
' -------------------------------------------------

style = _ES_RIGHT 'OR _ES_NUMBER
stylebits #1.txb, style, _WS_BORDER, 0, 0
textbox #1.txb,-1111,0,width,hRow
graphicbox #1.gbx 0,0,gfxWidth,gfxHeight
OPEN "More Grid" FOR WINDOW AS #1
PRINT #1, "trapclose [quit]"
#1.gbx "down; fill 255 215 190; size 1"
#1.gbx "color black; backcolor ";BackColorForAll$
#1.gbx "when leftButtonDown [lbd]"
#1.gbx fontForAll$
#1.txb "!";fontForAll$

dim hT(12*6)
dim ar$(c,r)
FOR row = 0 to r-1
   FOR col =0 to c-1

    if col=0 or row=0 then
        #1.gbx "color black; backcolor 255 200 255"
        tetxt$ = ""
    else
        #1.gbx "color black; backcolor ";BackColorForAll$
        tetxt$ = str$(int(rnd(1)*999))
    end if

    hPos = startH + (col)*width
    vPos = startV + (row) * hRow
    #1.gbx "place "; hPos; " "; vPos
    #1.gbx "boxfilled ";width+hPos+1;" ";hRow+vPos+1
    #1.gbx "place "; hPos+7; " "; vPos+17

    ar$(col,row) = tetxt$
    lenght = int( ( width - len( tetxt$ )*7 ) /7 ) - 1
    #1.gbx "|";space$(lenght);tetxt$
   NEXT col
NEXT row

#1.gbx "flush" ' 1st flush

'#1 "refresh"

WAIT

[lbd]

    #1.txb "!contents? cont$"

    ' some simple math if first character is '='
    if trim$( left$( cont$,1 ) )= "=" then
     if instr(cont$,"?")=0 then ' need to check better for not valid chars
         cont$ = mid$( cont$, 2 )
         cont$ = eval$( cont$ )
     end if
    end if

    if cont$<>"" then ar$(ix,iy) = cont$
        ' try to avoid the caret (scan)
        #1.txb cont$
            scan
        ' Make the graphics to stay on screen

        #1.gbx "getbmp tmpbmp 0 0 "; gfxWidth; " "; gfxHeight
        #1.gbx "drawbmp tmpbmp 0 0"
        unloadbmp "tmpbmp"

        #1.gbx "delsegment ";segId-1
        #1.gbx "segment segId"
        #1.gbx "flush"

    xpos = width * int( (MouseX-startH) / width )
    ypos = hRow * int( (MouseY-startV) / hRow )
    ix = int(xpos/width)
    iy = int(ypos/hRow)

    if ix > 0 and iy > 0 then
     if ix<c and iy<r then
        #1.txb "!locate " ; xpos +2+startH; " "; ypos +2+startV; " "; width-1 ; " "; hRow-1
        #1.txb ar$(ix,iy)
        #1.txb "!selectall"
        #1 "refresh"
     end if
    end if

wait

[quit]
    close #1: end

[readIt]
   #1.txb "!contents? cont$"
    notice cont$
    wait
 
User IP Logged

cundo
Pages: 1 2  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