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.
constructor TMenu.Create();
begin
FSelectedIndex:=-1;
FStartIndex:=-1;
FMark:=' ';
FListOfLines:=TStringList.Create;
FListOfLines.Sorted:=False;
end;
destructor TMenu.Destroy;
begin
FListOfLines:=nil;
FListOfLines.Free;
end;
procedure TMenu.AddText(T: string);
begin
FListOfLines.Add(T);
end;
procedure TMenu.AddChoiceItem(Ci: string; S: Boolean);
begin
FListOfLines.Add(Ci);
// Set start index
if (FStartIndex = -1) then
begin
FStartIndex:=FListOfLines.Count - 1;
end;
// Set end index
FEndIndex:=FListOfLines.Count - 1;
// Add it according to state of selection
if (S = True) then
begin
FSelectedIndex:=FStartIndex;
FMark:=' --->';
end
else
begin
FMark:=' ';
end;
FListOfLines[FListOfLines.Count - 1]:=FMark + FListOfLines[FListOfLines.Count - 1];
end;
procedure TMenu.AddSeparator;
begin
FListOfLines.Add(#10);
end;
procedure TMenu.OnChoice(K: Char);
var
I : Integer;
begin
if (K = #72) and (FSelectedIndex > FStartIndex) then
begin
FListOfLines[FSelectedIndex]:=ReplaceStr(FListOfLines[FSelectedIndex], ' ', ' --->');
FSelectedIndex:=FSelectedIndex - 1;
end
else if (K = #80) and (FSelectedIndex < FEndIndex) then
begin
FListOfLines[FSelectedIndex]:=ReplaceStr(FListOfLines[FSelectedIndex], ' ', ' --->');
FSelectedIndex:=FSelectedIndex + 1;
end;
end;
Here's how I call the class.
Menu:=TMenu.Create();
Menu.AddText('Is this bpm correct: ' + B.ToString);
Menu.AddText('for the file ' + Nm);
Menu.AddSeparator;
Menu.AddChoiceItem('Yes', True);
Menu.AddChoiceItem('Make it half', False);
Menu.AddChoiceItem('Custom', False);
Menu.AddSeparator;
Menu.AddText('(Press escape to exit)');
repeat
ClrScr;
Writeln;Writeln;Writeln;
for I:=0 to Menu.ListOfLines.Count - 1 do
begin
WriteLn(Menu.ListOfLines[I]);
end;
Key:=ReadKey;
if Key=#0 then
Key:=ReadKey;
if (Key = #72) then
begin
Menu.OnChoice(#72);
end
else if (Key = #80) then
begin
Menu.OnChoice(#80);
end;
until Key = #27;
Menu.Destroy;
Menu:=nil;
Menu.Free;
When I hit down arrow nothing happens. How can I make it work?