Reputation: 15
I'm a Pascal newbie and I already read some stuff about it, but it is still pretty hard for me. I want to create a simple password generator, and adjust the number of characters.
I found a function that actually generates the random password for me, which is this:
function RandomPassword(PLen: Integer): string;
var
str: string;
begin
Randomize;
//string with all possible chars
str := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
Result := '';
repeat
Result := Result + str[Random(Length(str)) + 1];
until (Length(Result) = PLen)
end;
This is the code that prints the string to the Memo:
procedure TForm1.Button2Click(Sender: TObject);
begin
Memo1.Caption := RandomPassword(10);
end;
I also got a TComboBox, and I want to use the value from the combobox to chose the number of characters (6-32). The number of characters is in this case 10 but I want to use the value from the Combobox instead of a predetermined number. Who can help me? I'd appreciate it!
Upvotes: 1
Views: 556
Reputation: 657
You can select the combobox value like this:
RandomPassword(StrToInt(ComboBox.Items[ComboBox.ItemIndex]))
ComboBox.ItemIndex
returns the index of select combobox item
ComboBox.Items[]
is used to select a item in the combobox.
StrToInt()
is used cause the combobox value is a string and you have to change it to integer
Upvotes: 0