Author |
Topic: Data Grid with Specified Column Widths (Read 3354 times) |
|
Richard Russell
Administrator
member is offline


Posts: 1348
|
 |
Re: Data Grid with Specified Column Widths
« Reply #30 on: Nov 1st, 2015, 12:55pm » |
|
on Nov 1st, 2015, 10:15am, pnlawrence wrote:| The above sub references branch routines outside the sub in the combobox "validation" branches. I don't get any kind of compiler message or error message during execution about this. |
|
I think there may be some confusion about what is meant by a 'subroutine'. There are two kinds of subroutine available in Liberty BASIC: there's the old-fashioned kind accessed using GOSUB and there's the modern kind accessed using CALL.
When you see references to 'functions and subs' you can usually assume that it means the modern kind. You must not attempt to jump from 'inside' to 'outside' a FUNCTION or SUB. Even if you do not receive a warning or error message (see earlier threads explaining why LBB's error reporting is sometimes poor) the program will almost certainly crash.
The old-fashioned 'legacy' kind of GOSUB...RETURN subroutine is a completely different animal. Variables, labels, DATA statements etc. are not 'local' to the subroutine like they are with a SUB. You should still avoid jumping out however, because that causes a memory leak (in the same way as jumping out of a loop does), but neither LB nor LBB will actually stop you.
I would argue that GOSUB should probably not be used in a program written today. It is present in Liberty BASIC primarily for compatibility with 'traditional' BASIC programs (rather as line numbers are still supported by LB, but you would never use them in a new program).
For that matter I would argue that you shouldn't use GOTO in a modern program either, but I know that many people disagree, and I definitely don't want to provoke an outbreak of GOTO Wars on this forum. 
Richard.
|
|
|
|
joker
Global Moderator
member is offline


Gender: 
Posts: 157
|
 |
Re: Data Grid with Specified Column Widths
« Reply #31 on: Nov 1st, 2015, 2:07pm » |
|
Ok, for me there is no confusion about the difference between subs and gosubs, but thanks for saying that anyway.
Then, referring to my previous grid sub, I should include the branch statements within the sub even though it "seems" to work in the demo, because it will eventually break.
[EDIT] Thinking about it makes me wonder how to put a branch from a combobox between the sub/end sub. My sub is just drawing the grid. The actual combobox is out in the main window so the call to the combobox handler is in the main window. The way I have it should work fine.
That is good info. With all the comments everywhere about bugs that everyone is used to and just lives with, its hard not to question some of the documentation. In this case, the doc was absolutely correct.
|
| « Last Edit: Nov 1st, 2015, 2:37pm by joker » |
Logged
|
|
|
|
Richard Russell
Administrator
member is offline


Posts: 1348
|
 |
Re: Data Grid with Specified Column Widths
« Reply #32 on: Nov 1st, 2015, 3:29pm » |
|
on Nov 1st, 2015, 2:07pm, pnlawrence wrote:| Then, referring to my previous grid sub, I should include the branch statements within the sub even though it "seems" to work in the demo, because it will eventually break. |
|
The only labels which are "referenced" inside your SUB are the event handlers for the GUI controls. You are not 'jumping' to those labels from inside the SUB, you are simply 'registering' them with the GUI controls so they know what to do if (e.g.) you click on them.
Maybe you are taking the LB docs rather too literally, in the sense that they imply that the branch labels are 'invisible' inside the SUB. That doesn't mean that the name of the branch label is invisible, only that its location is.
So if you were to try to jump to that label from inside the SUB then it would fail, because it wouldn't know where to jump to. But if you simply pass the name of the label as the event handler to, say, a COMBOBOX command it doesn't need to know its location until the event actually happens.
There would only be a problem if you put the OPEN statement inside the SUB. Then it would be possible for the GUI event to occur before the END SUB, whilst its event handler is out of scope. I suppose there's a theoretical risk of that happening even if the OPEN is the last statement in the SUB.
You are not doing that, so it's not an issue, but if you were then the solution is to use SUB event handlers rather than branch event handlers (SUBs are 'in scope' permanently):
Code: combobox #gridWnd.cell, comboArray1$(), selectionGridCombo1, leftMargin+cumColumnWidth, cellMargin+cumRowHeight, columnWidth(col)+1, rowHeight
Now the combobox will CALL selectionGridCombo1(handle$) rather than GOTO [selectionGridCombo1] and it won't matter if it happens whilst still inside your SUB or not.
A general advantage of SUB event handlers is that you can safely put a WAIT or a SCAN inside any SUB or FUNCTION without being concerned that, when an event occurs, its handler will be out of scope.
It's for this reason that the TIMER bug in LB 4 is so serious. Despite what the docs say, you cannot use a SUB handler with TIMER so scope issues can be a major problem. In LBB all event handlers can be SUBs and that way scope issues are largely irrelevant.
Richard.
|
|
Logged
|
|
|
|
joker
Global Moderator
member is offline


Gender: 
Posts: 157
|
 |
Re: Data Grid with Specified Column Widths
« Reply #33 on: Nov 1st, 2015, 4:41pm » |
|
Very clear, Richard. Thanks!
|
|
Logged
|
|
|
|
joker
Global Moderator
member is offline


Gender: 
Posts: 157
|
 |
Re: Data Grid with Specified Column Widths
« Reply #34 on: Nov 2nd, 2015, 6:24pm » |
|
I'm finding it very klunky saving data out of the grid "boxes" if there's more than a few columns.
Let me clarify, "saving data." I'm saving each row of the grid as a record in a random access file. In one case, I have 20 columns with multiple combo and text boxes.
I was originally wanting to make use of the "handle;row;col" format, but what I try gets tied up with the type of box to be read from. Then there is still the, basically manual, way of mapping to the file field variables.
A magic wand to change the data into field variables is really what I would like to see. Since I'm wishing, it would be nice to allow field variables to be array variables.
EDIT: What I am doing now is transferring data from the grid to an array and resolving any data issues in the process. Then I transfer the array to the records of the RAF since there are only string values to transfer by then. However, it is still klunky.
|
| « Last Edit: Nov 2nd, 2015, 7:34pm by joker » |
Logged
|
|
|
|
joker
Global Moderator
member is offline


Gender: 
Posts: 157
|
 |
Re: Data Grid with Specified Column Widths
« Reply #35 on: Nov 2nd, 2015, 7:10pm » |
|
Single digit row and columns. I read it in another post, but I've never thought there to be a problem. http://lbb.conforums.com/index.cgi?board=extensions&action=display&num=1427217832
I never put any limits on the grid's number of rows or columns so the maphandle function that creates a new "box" handle is certainly using more than a single digit for the row and column part.
I've never seen any problem with doing that, but then I read the above post.
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline


Posts: 1348
|
 |
Re: Data Grid with Specified Column Widths
« Reply #36 on: Nov 2nd, 2015, 7:53pm » |
|
on Nov 2nd, 2015, 6:24pm, pnlawrence wrote:| what I try gets tied up with the type of box to be read from. |
|
Are you referring to the need to use !CONTENTS? var$ for the text boxes and SELECTION? var$ for the comboboxes? In that case you could create a 2D string array, indexed by row and column, containing the correct command for each control. Most conveniently you could store the command in the array when you originally declare the controls:
Code: case 0
stylebits #gridWnd.cell, 0, _WS_BORDER,0,0 ' trying to make textbox border look like combobox border
textbox #gridWnd.cell, leftMargin+cumColumnWidth, cellMargin+cumRowHeight, columnWidth(col)+1, rowHeight+1
maphandle #gridWnd.cell, "#gridWnd.cell";row;col
Command$(row,col) = "!CONTENTS? "
case 1
' COMBOBOX doesn't recognize rowHeight. Height is determined from font size. rowHeight doesn't seem to have any effect.
combobox #gridWnd.cell, comboArray1$(), [selectionGridCombo1], leftMargin+cumColumnWidth, cellMargin+cumRowHeight, columnWidth(col)+1, rowHeight
maphandle #gridWnd.cell, "#gridWnd.cell";row;col
Command$(row,col) = "SELECTION? "
Then, when you want to read the contents back, you simply need to do:
Code: for row = 1 to numberOfRows
for col = 1 to numberOfColumns
handle$ = "#gridWnd.cell";row;col
#handle$ Command$(row,col);"var$"
' do something with var$
next col
next row Or have I, once again, misunderstood?
Quote:| Since I'm wishing, it would be nice to allow field variables to be array variables. |
|
It would be fairly easy to support that, I will add it to the wish list. In fact I think you can successfully use numeric array variables in LBB now, but not string array variables.
Richard.
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline


Posts: 1348
|
 |
Re: Data Grid with Specified Column Widths
« Reply #37 on: Nov 2nd, 2015, 8:59pm » |
|
on Nov 2nd, 2015, 7:10pm, pnlawrence wrote:| I've never seen any problem with doing that, but then I read the above post. |
|
The problem arises in a situation such as the following. Suppose the row number is 1 and the column number is 10; if you simply concatenate them you would get a handle like #w.box110. Now suppose the row number is 11 and the column number is zero; what handle do you get? It's #w.box110 again!
Of course that doesn't mean you can't use more than 10 rows or columns, but it does mean you have to be more careful with the way you construct the handle variables, for example you could ensure that both row and column are always two-digit numbers. Then the first example would be #w.box0110 and the second example would be #w.box1100.
It may be that in your particular case a conflict never arises, but it might if you enlarged your grid.
Richard.
|
|
|
|
joker
Global Moderator
member is offline


Gender: 
Posts: 157
|
 |
Re: Data Grid with Specified Column Widths
« Reply #38 on: Nov 2nd, 2015, 9:15pm » |
|
Quote:| for example you could ensure that both row and column are always two-digit numbers. |
|
Got it! Don't know why I didn't think that through. (Guess I'm on the Richard Russell BASIC Welfare Program! )
|
|
Logged
|
|
|
|
joker
Global Moderator
member is offline


Gender: 
Posts: 157
|
 |
Re: Data Grid with Specified Column Widths
« Reply #39 on: Nov 2nd, 2015, 9:28pm » |
|
Quote:| Or have I, once again, misunderstood? |
|
You have "stood" it just right. I didn't imagine to store the command in the array, too. That would simplify one part of the FOR/NEXT loop, but I'm not sure that is overall more simple, though.
That's one of those "six one way; half a dozen the other way" situations. Food for thought. Thanks!
On using arrays as RAF field variables, I haven't thought out exactly how that would make it better for me, but thinking there's an advantage in my case. I'm going from GRID -> ARRAY -> FIELD now. It just seems that if the array was the field, then there would be some savings.
I have to constantly remind myself that I am a NOVICE PROGRAMMER with LBB and LB.
|
|
Logged
|
|
|
|
|