Recent

Author Topic: [Solved] Console program with select menu  (Read 301 times)

nikel

  • Sr. Member
  • ****
  • Posts: 252
[Solved] Console program with select menu
« on: April 29, 2025, 02:13:01 pm »
Hello, I'm trying to make a selection menu for my console program. I couldn't make hover background but I made it using an arrow to mark. I moved the code to a new class but it's not working now. Here's my code.

Code: Pascal  [Select][+][-]
  1. constructor TMenu.Create();
  2. begin
  3.   FSelectedIndex:=-1;
  4.   FStartIndex:=-1;
  5.   FMark:='     ';
  6.   FListOfLines:=TStringList.Create;
  7.   FListOfLines.Sorted:=False;
  8. end;
  9.  
  10. destructor TMenu.Destroy;
  11. begin
  12.   FListOfLines:=nil;
  13.   FListOfLines.Free;
  14. end;
  15.  
  16. procedure TMenu.AddText(T: string);
  17. begin
  18.   FListOfLines.Add(T);
  19. end;
  20.  
  21. procedure TMenu.AddChoiceItem(Ci: string; S: Boolean);
  22. begin
  23.   FListOfLines.Add(Ci);
  24.  
  25.   // Set start index
  26.   if (FStartIndex = -1) then
  27.   begin
  28.     FStartIndex:=FListOfLines.Count - 1;
  29.   end;
  30.  
  31.   // Set end index
  32.   FEndIndex:=FListOfLines.Count - 1;
  33.  
  34.   // Add it according to state of selection
  35.   if (S = True) then
  36.   begin
  37.     FSelectedIndex:=FStartIndex;
  38.     FMark:=' --->';
  39.   end
  40.   else
  41.   begin
  42.     FMark:='     ';
  43.   end;
  44.  
  45.   FListOfLines[FListOfLines.Count - 1]:=FMark + FListOfLines[FListOfLines.Count - 1];
  46. end;
  47.  
  48. procedure TMenu.AddSeparator;
  49. begin
  50.   FListOfLines.Add(#10);
  51. end;
  52.  
  53. procedure TMenu.OnChoice(K: Char);
  54. var
  55.   I       : Integer;
  56. begin
  57.   if (K = #72) and (FSelectedIndex > FStartIndex) then
  58.   begin
  59.     FListOfLines[FSelectedIndex]:=ReplaceStr(FListOfLines[FSelectedIndex], '     ', ' --->');
  60.     FSelectedIndex:=FSelectedIndex - 1;
  61.   end
  62.   else if (K = #80) and (FSelectedIndex < FEndIndex) then
  63.   begin
  64.     FListOfLines[FSelectedIndex]:=ReplaceStr(FListOfLines[FSelectedIndex], '     ', ' --->');
  65.     FSelectedIndex:=FSelectedIndex + 1;
  66.   end;
  67. end;

Here's how I call the class.

Code: Pascal  [Select][+][-]
  1. Menu:=TMenu.Create();
  2. Menu.AddText('Is this bpm correct: ' + B.ToString);
  3. Menu.AddText('for the file ' + Nm);
  4. Menu.AddSeparator;
  5. Menu.AddChoiceItem('Yes', True);
  6. Menu.AddChoiceItem('Make it half', False);
  7. Menu.AddChoiceItem('Custom', False);
  8. Menu.AddSeparator;
  9. Menu.AddText('(Press escape to exit)');
  10.  
  11. repeat
  12.   ClrScr;
  13.   Writeln;Writeln;Writeln;
  14.  
  15.   for I:=0 to Menu.ListOfLines.Count - 1 do
  16.   begin
  17.     WriteLn(Menu.ListOfLines[I]);
  18.   end;
  19.  
  20.   Key:=ReadKey;
  21.    if Key=#0 then
  22.       Key:=ReadKey;
  23.  
  24.   if (Key = #72) then
  25.   begin
  26.     Menu.OnChoice(#72);
  27.   end
  28.   else if (Key = #80) then
  29.   begin
  30.     Menu.OnChoice(#80);
  31.   end;
  32. until Key = #27;
  33.  
  34. Menu.Destroy;
  35. Menu:=nil;
  36. Menu.Free;

When I hit down arrow nothing happens. How can I make it work?
« Last Edit: April 29, 2025, 03:16:10 pm by nikel »

Thaddy

  • Hero Member
  • *****
  • Posts: 16928
  • Ceterum censeo Trump esse delendam
Re: Console program with select menu
« Reply #1 on: April 29, 2025, 02:25:19 pm »
Have a look at the FreeVision demo.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

nikel

  • Sr. Member
  • ****
  • Posts: 252
Re: Console program with select menu
« Reply #2 on: April 29, 2025, 02:46:34 pm »
Thank you for your reply. FreeVision is too complex for me.

nikel

  • Sr. Member
  • ****
  • Posts: 252
Re: Console program with select menu
« Reply #3 on: April 29, 2025, 03:15:33 pm »
It was a careless mistake. I had to change OnChoice function and set the variable first.

dseligo

  • Hero Member
  • *****
  • Posts: 1507
Re: Console program with select menu
« Reply #4 on: April 29, 2025, 03:38:12 pm »
It was a careless mistake. I had to change OnChoice function and set the variable first.

You also have error in OnDestroy. You first set FListOfLines to nil, and after that you call Free. It should be other way around.

P.S.: And in the main program similar:
Code: Pascal  [Select][+][-]
  1. Menu.Destroy;
  2. Menu:=nil;
  3. Menu.Free;

Do it like this:
Code: Pascal  [Select][+][-]
  1. Menu.Free;
  2. Menu:=nil; // this is optional, you don't have to do it
« Last Edit: April 29, 2025, 03:40:48 pm by dseligo »

 

TinyPortal © 2005-2018