(06-24-2023, 02:35 PM)Dimster Wrote: I've written code in QBasic for a Drop Down Menu. I used that old standard code routine of printing one, erase that one, print two, erase two, print three .. printing and erasing was the basic way of creating the drop down effect. There has been so many improvements to the language since then. Has the basic print/erase method changed at all in terms of creating a drop down menu?
Hi Dimster!
I think you might of missed where you wanted to post your question, maybe here
https://staging.qb64phoenix.com/showthread.php?tid=1768
instead?
There are a thousand ways to do this, I was tempted to post some other ways in that post but... I didn't want to hijack.
But since you ask in this little corner of forum, I will say basic methods don't change much when QB64pe tries to stay compatible with the past but sure as shoot'n there are new ways always being added to our tool box!
So I will show some alternates I considered to do a Multiple Input:
Here is short and sweet:
Code: (Select All)
again:
Input "Please enter 3 inputs separated by commas "; i1$, i2$, i3$
Print "n for no!, Process?: "; i1$; ", "; i2$; ", "; i3$
Input confirm$
If confirm$ = "n" Then GoTo again
Print "processing..."
To fix that here is a menu of choices to edit until you get everything as you want:
Code: (Select All)
_Title "Multiple Input Menu demo" 'b+ 2021-06-11
Type InputItem
As String promptName, SValue
End Type
ReDim Shared nItems
nItems = 12 ' number of inputs plus 2 for quit and goto for processing inputs
' initial item names (like variables)
ReDim inps(1 To nItems) As InputItem
For i = 1 To nItems - 2
inps(i).promptName = "Demo Input Item #" + _Trim$(Str$(i))
Next
inps(nItems - 1).promptName = "Process Inputs"
inps(nItems).promptName = "Quit"
Do
Cls
For i = 1 To nItems
Print "#" + _Trim$(Str$(i)), inps(i).promptName;
If inps(i).SValue <> "" Then Print " = "; inps(i).SValue Else Print
Next
Print: Input "Enter choice # "; choice
If choice < nItems - 1 Then
Print "Please enter, " + inps(choice).promptName + " ";
Input inps(choice).SValue
End If
Loop Until choice >= nItems - 1 And choice <= nItems
Select Case choice
Case nItems: Print "You quit, goodbye!": End
Case nItems - 1
' <<< could check inputs here and handle errors
GoTo processInputs
End Select
processInputs:
' doit remember SValues are strings!
Print "Processing Inputs now..."
This allows you to use arrow keys up and down until you press enter on last field:
Code: (Select All)
Option _Explicit ' to remind what I haven't dim'd B+ mod Cobalt and NOVARSEG and Dav 2021-06-15
Dim As Long cursor, nInputs, i
Dim k$
'setups
nInputs = 6 'for this example we will allow up to nInputs inputs
Dim Inputs$(1 To nInputs)
Dim prompts$(1 To nInputs)
prompts$(1) = "First Name: "
prompts$(2) = " Last Name: "
prompts$(3) = "Home Phone: "
prompts$(4) = "Cell Phone: "
prompts$(5) = "Birth date: "
prompts$(6) = " State: "
cursor = 1 'start on the first input line
'start our loop up
Do
k$ = InKey$ 'keyboard input
For i = 1 To nInputs 'loop through all our input options
Locate i, 1: Print prompts$(i); Inputs$(i);
'print a "cursor" to show which input user is on, use space$ to clear other lines
If i = cursor Then Print "_" + " " Else Print Space$(2)
Next
If Len(k$) Then 'let us process which line the user is inputing
Select Case Asc(Right$(k$, 1))
Case 13: If cursor = nInputs Then Exit Do Else cursor = cursor + 1 ' no exit unless on last line
Case 8: Inputs$(cursor) = Left$(Inputs$(cursor), Len(Inputs$(cursor)) - 1) 'backspace to clear this input
Case 27: Exit Do
Case 72, 75: If cursor > 1 Then cursor = cursor - 1 ' up
Case 73: cursor = 1 'pgUp
Case 80, 77: If cursor < nInputs Then cursor = cursor + 1 'down
Case 81: cursor = nInputs ' pgDn
Case 32 To 122: Inputs$(cursor) = Inputs$(cursor) + k$
End Select
End If
_Limit 30 'up date 30times a sec to save CPU use
Loop
'show results of inputs
For i = 1 To nInputs
Print i, prompts$(i); "= "; Inputs$(i)
Next
I had that titled "best" but Steve has a really nice version that bests mine with popup like menu functioning:
(Come to think, there was a slew awhile back. Dav had a nice one too and someone else I think, Pete? ...)
Code: (Select All)
Screen _NewImage(1280, 720, 32)
Dim As String prompt(3), results(3)
prompt(0) = "Name": prompt(1) = "Age": prompt(2) = "Sex": prompt(3) = "Phone Number"
For i = 1 To 100 'Draw some stuff on the screen for a background
Line (Rnd * 1280, Rnd * 720)-(Rnd * 1280, Rnd * 720), _RGB32(Rnd * 255, Rnd * 255, Rnd * 255), BF
Next
Print "SLEEPING SO YOU CAN SEE OUR BACKGROUND"
Sleep
MultiInput 100, 100, prompt(), results(), 20
Print: Print "As you can see, when finished, our pop up restored our background..."
Print "And your answers were the following:"
For i = 0 To UBound(results): Print results(i): Next
'ref: SMcNeill https://staging.qb64phoenix.com/showthread.php?tid=141
Sub MultiInput (xPos, yPos, prompt() As String, results() As String, maxLength As Integer)
backupImage = _CopyImage(0) 'copy our screen
B = _Blend: _DontBlend: A = _AutoDisplay: u = UBound(prompt)
For i = 0 To u 'get box size
p = _PrintWidth(prompt(i)): If p > maxWidth Then maxWidth = p
Next
boxWidth = maxWidth + maxLength * _FontWidth + 10: boxheight = (u + 1) * (_FontHeight + 3)
Do
If Timer > t# + .5 Then blink = Not blink: t# = Timer
k = _KeyHit 'get input
Select Case k
Case 18432: selection = selection - 1: If selection < 0 Then selection = u 'up
Case 20480, 13: selection = selection + 1: If selection > u Then selection = 0 'down
Case 27: Exit Do 'esc is the exit/finish code
Case 8: results(selection) = Left$(results(selection), Len(results(selection)) - 1) 'backspace
Case 32 To 255: results(selection) = results(selection) + Chr$(k) 'all else
End Select
_PutImage , backupImage 'restore background
Line (xPos, yPos)-Step(boxWidth, boxheight), 0, BF: Line (x + xPos + maxWidth + 1, y + yPos)-Step(0, boxheight), -1 'draw box
For i = 0 To u
Line (x + xPos, y + i * (_FontHeight + 3) + yPos)-Step(boxWidth, _FontHeight + 3), -1, B
_PrintString (x + xPos + 2, y + i * (_FontHeight + 3) + yPos + 2), prompt(i)
If i = selection And blink Then out$ = results(i) + Chr$(219) Else out$ = results(i)
_PrintString (x + xPos + maxWidth + 3, y + i * (_FontHeight + 3) + yPos + 2), out$
Next
_Limit 30: _Display
Loop
_PutImage , backupImage
If B Then _Blend
If A Then _AutoDisplay
_FreeImage backupImage
End Sub
b = b + ...