Active Forums > QB64 Discussion
Highlight% changes by itself
Jaze:
I often make a DO LOOP with an INKEY$ to access the string values of the arrow keys. Out of probably 25 times writing a loop similar to this one, this is the second time I could not use my HaltAndDisplay% bool to stop the flicker.
On line 56, the Highlight% rapidly changes between positive and negative integers of about 3 to 5 digits. From what I can see in the code I've written, Highlight% shouldn't change until
one of the SHARED ArrowKey$ Values has been detected. I'd be grateful to anyone who can't point out my error
--- Code: QB64: --- _TITLE "Time Calculator" CONST TRUE% = 1CONST FALSE% = -1DIM SHARED LeftArrowKey$: LeftArrowKey$ = CHR$(0) + "K"DIM SHARED RightArroeKey$: rightArrowKey$ = CHR$(0) + "M"DIM SHARED UpArrowKey$: UpArrowKey$ = CHR$(0) + "H"DIM SHARED DownArrowKey$: DownArrowKey$ = CHR$(0) + "P"CONST UpArrowHit% = 18432CONST LeftArrowHit% = 19200CONST RightArrowHit% = 19712CONST DownArrowHit% = 20480' for the highlighted option in the GetTimeAmount SUBCONST YearsHO% = 1CONST DaysHO% = 2CONST HoursHO% = 3CONST MinutesHO% = 4CONST SecondsHO% = 5 WIDTH 80, 50_FULLSCREENCOLOR 14, 1: CLS CALL Menu SUB FromNowUntilEND SUB SUB HowLongSinceEND SUB SUB WhatDateAfterElapsedTimeEND SUB SUB AddElapsedTimesEND SUB SUB SubtractElapsedTimesEND SUB SUB MultiplyEND SUB SUB DivideEND SUB SUB Menu HaltAndDisplay% = 0: Highlight% = 0: yPos% = 0: UserCommand$ = "": A$ = "" xPos% = 0: MaxOption% = 0: SelectedAnOption% = 0 HaltAndDisplay% = TRUE%: Highlight% = 1: yPos% = 13: MaxOption% = 8: SelectedAnOption% = FALSE% DO UserCommand$ = INKEY$ LOCATE 45, 1: PRINT S$(Highlight%) LOCATE 46, 1: PRINT "|" + UserCommand$ + "|" IF HaltAndDisplay% = TRUE% THEN COLOR 14, 1: CLS A$ = "Time Calculator Menu": xPos% = Center(A$) LOCATE yPos%, Center(A$): PRINT A$: LOCATE yPos% + 1, Center(A$) PRINT "---- ---------- ----" A$ = "": A$ = "1.) Find How Long From Now" LOCATE yPos% + 3, Center(A$): IF Highlight% = 1 THEN COLOR 10, 0: ELSE COLOR 14, 1: END IF: PRINT A$ A$ = "": A$ = "Until A Selected Time" LOCATE yPos% + 4, Center(A$): IF Highlight% = 1 THEN COLOR 10, 0: ELSE COLOR 14, 1: END IF: PRINT A$ A$ = "": A$ = "2.) Find How Long It Has Been Since" LOCATE yPos% + 6, Center(A$): IF highlighedoption% = 2 THEN COLOR 10, 0: ELSE COLOR 14, 1: END IF: PRINT A$ A$ = "": A$ = "A Selected Time Has Passed" LOCATE yPos% + 7, Center(A$): IF Highlight% = 2 THEN COLOR 10, 0: ELSE COLOR 14, 1: END IF: PRINT A$ A$ = "": A$ = "3.) Find The Date And Time It Will Be After" LOCATE yPos% + 9, Center(A$): IF Highlight% = 3 THEN COLOR 10, 0: ELSE COLOR 14, 1: END IF: PRINT A$ A$ = "": A$ = "A Selected Amount Of Time Has Passed" LOCATE yPos% + 10, Center(A$): IF Highlight% = 3 THEN COLOR 10, 0: ELSE COLOR 14, 1: END IF: PRINT A$ A$ = "": A$ = "4.) Add Two Elapsed Times" LOCATE yPos% + 12, Center(A$): IF Highlight% = 4 THEN COLOR 10, 0: ELSE COLOR 14, 1: END IF: PRINT A$ A$ = "": A$ = "5.) Subtract One Elapsed Time From Another One" LOCATE yPos% + 14, Center(A$): IF Highlight% = 5 THEN COLOR 10, 0: ELSE COLOR 14, 1: END IF: PRINT A$ A$ = "": A$ = "6.) Multiply An Elapsed Time By A Constant" LOCATE yPos% + 16, Center(A$): IF Highlight% = 6 THEN COLOR 10, 0: ELSE COLOR 14, 1: END IF: PRINT A$ A$ = "": A$ = "7.) Divide An Elapsed Time By A Constant" LOCATE yPos% + 18, Center(A$): IF Highlight% = 7 THEN COLOR 10, 0: ELSE COLOR 14, 1: END IF: PRINT A$ A$ = "": A$ = "8.) Exit" LOCATE 45, Center(A$): IF Highlight% = 8 THEN COLOR 10, 0: ELSE COLOR 14, 1: END IF: PRINT A$ HaltAndDisplay% = FALSE% END IF SELECT CASE UserCommand$ CASE UpArrowKey$, LeftArrowKey$ Highlight% = highlightedoption - 1 IF Highlight% = 0 THEN Highlight% = MaxOption% HaltAndDisplay% = TRUE% CASE DownArrowKey$, rightarrowkey$ Highlight% = Highlight% + 1 IF highlightedoption > maxoption THEN Highlight% = 1 HaltAndDisplay% = TRUE% CASE CHR$(13) SelectedAnOption% = TRUE% CASE "1", "2", "3", "4", "5", "6", "7" Highlight% = VAL(UserCommand$) SelectedAnOption% = TRUE% END SELECT IF SelectedAnOption% = TRUE THEN SELECT CASE Highlight% CASE 1 CALL FromNowUntil: SelectedAnOption% = FALSE% CASE 2 CALL HowLongSince: SelectedAnOption% = FALSE% CASE 3 CALL WhatDateAfterElapsedTime: SelectedAnOption% = FALSE% CASE 4 CALL AddElapsedTimes: SelectedAnOption% = FALSE% CASE 5 CALL SubtractElapsedTimes: SelectedAnOption% = FALSE% CASE 6 CALL Multiply: SelectedAnOption% = FALSE% CASE 7 CALL Divide: SelectedAnOption% = FALSE% END SELECT END IF LOOP UNTIL UserCommand$ = CHR$(27) OR UserCommand$ = S$(MaxOption%)END SUB FUNCTION Center% (Text$): Center% = INT((80 - LEN(Text$)) / 2): END FUNCTIONFUNCTION S$ (Number!): S$ = LTRIM$(STR$(Number!)): END FUNCTIONFUNCTION P$: pause$ = INPUT$(1): IF pause$ = CHR$(27) THEN ENDP$ = pause$: END FUNCTION
bplus:
I added _display and _limit to stop flicker.
Once you use _display, then you must say _display every time you want to show something immediatedly. To disable _display use _autodisplay and get default automatic "display". You could put that at the end of the sub before exiting. _display once in you main drawing loop is best.
--- Code: QB64: --- _Title "Time Calculator" Const TRUE% = 1Const FALSE% = -1Dim Shared LeftArrowKey$: LeftArrowKey$ = Chr$(0) + "K"Dim Shared RightArroeKey$: rightArrowKey$ = Chr$(0) + "M"Dim Shared UpArrowKey$: UpArrowKey$ = Chr$(0) + "H"Dim Shared DownArrowKey$: DownArrowKey$ = Chr$(0) + "P"Const UpArrowHit% = 18432Const LeftArrowHit% = 19200Const RightArrowHit% = 19712Const DownArrowHit% = 20480' for the highlighted option in the GetTimeAmount SUBConst YearsHO% = 1Const DaysHO% = 2Const HoursHO% = 3Const MinutesHO% = 4Const SecondsHO% = 5 Width 80, 50_FullScreenColor 14, 1: Cls Call Menu Sub FromNowUntilEnd Sub Sub HowLongSinceEnd Sub Sub WhatDateAfterElapsedTimeEnd Sub Sub AddElapsedTimesEnd Sub Sub SubtractElapsedTimesEnd Sub Sub MultiplyEnd Sub Sub DivideEnd Sub Sub Menu HaltAndDisplay% = 0: Highlight% = 0: yPos% = 0: UserCommand$ = "": A$ = "" xPos% = 0: MaxOption% = 0: SelectedAnOption% = 0 HaltAndDisplay% = TRUE%: Highlight% = 1: yPos% = 13: MaxOption% = 8: SelectedAnOption% = FALSE% Do UserCommand$ = InKey$ Locate 45, 1: Print S$(Highlight%) Locate 46, 1: Print "|" + UserCommand$ + "|" If HaltAndDisplay% = TRUE% Then Color 14, 1: Cls A$ = "Time Calculator Menu": xPos% = Center(A$) Locate yPos%, Center(A$): Print A$: Locate yPos% + 1, Center(A$) Print "---- ---------- ----" A$ = "": A$ = "1.) Find How Long From Now" Locate yPos% + 3, Center(A$): If Highlight% = 1 Then Color 10, 0: Else Color 14, 1: End If: Print A$ A$ = "": A$ = "Until A Selected Time" Locate yPos% + 4, Center(A$): If Highlight% = 1 Then Color 10, 0: Else Color 14, 1: End If: Print A$ A$ = "": A$ = "2.) Find How Long It Has Been Since" Locate yPos% + 6, Center(A$): If highlighedoption% = 2 Then Color 10, 0: Else Color 14, 1: End If: Print A$ A$ = "": A$ = "A Selected Time Has Passed" Locate yPos% + 7, Center(A$): If Highlight% = 2 Then Color 10, 0: Else Color 14, 1: End If: Print A$ A$ = "": A$ = "3.) Find The Date And Time It Will Be After" Locate yPos% + 9, Center(A$): If Highlight% = 3 Then Color 10, 0: Else Color 14, 1: End If: Print A$ A$ = "": A$ = "A Selected Amount Of Time Has Passed" Locate yPos% + 10, Center(A$): If Highlight% = 3 Then Color 10, 0: Else Color 14, 1: End If: Print A$ A$ = "": A$ = "4.) Add Two Elapsed Times" Locate yPos% + 12, Center(A$): If Highlight% = 4 Then Color 10, 0: Else Color 14, 1: End If: Print A$ A$ = "": A$ = "5.) Subtract One Elapsed Time From Another One" Locate yPos% + 14, Center(A$): If Highlight% = 5 Then Color 10, 0: Else Color 14, 1: End If: Print A$ A$ = "": A$ = "6.) Multiply An Elapsed Time By A Constant" Locate yPos% + 16, Center(A$): If Highlight% = 6 Then Color 10, 0: Else Color 14, 1: End If: Print A$ A$ = "": A$ = "7.) Divide An Elapsed Time By A Constant" Locate yPos% + 18, Center(A$): If Highlight% = 7 Then Color 10, 0: Else Color 14, 1: End If: Print A$ A$ = "": A$ = "8.) Exit" Locate 45, Center(A$): If Highlight% = 8 Then Color 10, 0: Else Color 14, 1: End If: Print A$ HaltAndDisplay% = FALSE% End If Select Case UserCommand$ Case UpArrowKey$, LeftArrowKey$ Highlight% = highlightedoption - 1 If Highlight% = 0 Then Highlight% = MaxOption% HaltAndDisplay% = TRUE% Case DownArrowKey$, rightarrowkey$ Highlight% = Highlight% + 1 If highlightedoption > maxoption Then Highlight% = 1 HaltAndDisplay% = TRUE% Case Chr$(13) SelectedAnOption% = TRUE% Case "1", "2", "3", "4", "5", "6", "7" Highlight% = Val(UserCommand$) SelectedAnOption% = TRUE% End Select If SelectedAnOption% = TRUE Then Select Case Highlight% Case 1 Call FromNowUntil: SelectedAnOption% = FALSE% Case 2 Call HowLongSince: SelectedAnOption% = FALSE% Case 3 Call WhatDateAfterElapsedTime: SelectedAnOption% = FALSE% Case 4 Call AddElapsedTimes: SelectedAnOption% = FALSE% Case 5 Call SubtractElapsedTimes: SelectedAnOption% = FALSE% Case 6 Call Multiply: SelectedAnOption% = FALSE% Case 7 Call Divide: SelectedAnOption% = FALSE% End Select End If _Display ' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< added this and next _Limit 30 Loop Until UserCommand$ = Chr$(27) Or UserCommand$ = S$(MaxOption%)End Sub Function Center% (Text$): Center% = Int((80 - Len(Text$)) / 2): End FunctionFunction S$ (Number!): S$ = LTrim$(Str$(Number!)): End FunctionFunction P$: pause$ = Input$(1): If pause$ = Chr$(27) Then EndP$ = pause$: End Function
bplus:
Oh you are trying to highlite something, ehhh
deja vu again today:
Didn't I suggest False% = 0 ???
An If is going to see both -1 and 1 as True and take the THEN path.
Jaze:
changing the false constant to zero made no difference. besides I like the -1, 1 so I can flip them by multiplying by -1
bplus:
Ah, I use "toggle"
toggle = 1 - toggle 0,1,0,1,....
True, False a little misleading, but everyone has their styles and preferences....
You are attempting to highlight line ready to go on Enter maybe after arrow presses, I assume.
Navigation
[0] Message Index
[#] Next page
Go to full version