SRL Archive - General

Yet Another SCAR Question (combobox's and INI's this time)


endlesslyonline

2011-06-21 14:33:42

Well, how would i go about populating a combo box with the values inside a INI?

I have it currently working, but it is hardcoded for a certain amount of items in the ini, i want to make it so that if sum1 goes and edits the INI file, those changes also show up in the combobox.

What i am thinking can work, but have no idea if its possible or how to implement it, is

Create a array with a undefined amount of spaces
let the array be populated until the end of section in INI is reached,
then populate the combobox with the array

is this possible, and how?
or can it be done any way else?

Well, thanks again for all the help villavu

BraK

2011-06-21 15:00:05

Try something like this. You might need to convert to Scar it's wrote for Simba.

Procedure GetComboFromINI;
var
z, i :integer;
begin
Combo := TComboBox.Create(Form);
with PCombo do
begin
Parent := Form;
Top := 30;
Left := 80;
Width := 100;
Height := 20;
z := StrToInt(ReadINI('Name', 'Length', 'Data')); // Must be the total amount of entries
For i := 0 to (z - 1) do
ITEMS.Add(ReadINI('Name', IntToStr(i), 'Data'));
end;
end;

~BraK

E: This way you have to edit the List amount in the INI along with the List of Items.

BazzBarrett

2011-06-21 15:05:00

procedure INIToComboBox;
var
I: Integer;
Str: String;
begin
repeat
inc(i);//increase I
Str := ReadINI(section, keyname + inttostr(I), FileName);//get option I note: will start at 1
if str = '' then break;//if nothing there then break the loop
Form.ComboBox.Items.Add(str);//add option to the combo box
until I>100; //fail safe i should never hit 100
end;
somthing like that?

:O someone beet me to it

BraK

2011-06-21 15:11:22

I R Ninja

Bazz will it return '' for a none existent ini Line?

~BraK

E: I don't mess with INI's too much.

Dgby714

2011-06-21 16:19:32

program _;

function ArrayFromINI(const Section, Keyname, Filename: string; const Max: integer): TVariantArray;
var
I: integer;
T: string;
begin
I := 0;
while True do
begin
T := ReadINI(Section, KeyName + IntToStr(I), FileName);

if (T <> '') then
begin
SetLength(Result, I + 1);
Result[I] := T;
end else
Break;

Inc(I);
if ((I > Max) and (Max > 0)) then
Break;
end;
end;

procedure ArrayToINI(const Arr: TVariantArray; const Section, KeyName, Filename: string);
var
I, H: integer;
begin
H := High(Arr);
for I := 0 to H do
WriteINI(Section, KeyName + IntToStr(I), ToStr(Arr[I]), Filename);
end;

begin
ArrayToINI(['test'], 'Test', 'Wha', ScriptPath + 'data.ini');
WriteLn(ArrayFromINI('Test', 'Wha', ScriptPath + 'data.ini', -1));
end.

BraK

2011-06-21 16:26:30

Pssh I knew Dgby714 would come in with a whole list of functions :)

~BraK

Dgby714

2011-06-21 16:37:24

From: BraK
Pssh I knew Dgby714 would come in with a whole list of functions :)

~BraK


=P I lost my priv in #SRL-School btw

endlesslyonline

2011-06-21 16:43:25

will test it now.

im quite new here, is there a way to add rep to you guys? because i must say this is THE best forums when it comes to speedy responses, its almost instant. You guys surprise me every time xD

Thanks again

Dgby714

2011-06-21 16:48:27

From: endlesslyonline
will test it now.

im quite new here, is there a way to add rep to you guys? because i must say this is THE best forums when it comes to speedy responses, its almost instant. You guys surprise me every time xD

Thanks again


There is a little blue check box to the upper-right of the post.

=)

BraK

2011-06-21 16:50:34

This tutorial will teach you about Reputation. Here.

~BraK

E: ninja'd by Dgby714 add me on MSN you :p

endlesslyonline

2011-06-21 16:53:57

From: Dgby714
program _;

function ArrayFromINI(const Section, Keyname, Filename: string; const Max: integer): TVariantArray;
var
I: integer;
T: string;
begin
I := 0;
while True do
begin
T := ReadINI(Section, KeyName + IntToStr(I), FileName);

if (T <> '') then
begin
SetLength(Result, I + 1);
Result[I] := T;
end else
Break;

Inc(I);
if ((I > Max) and (Max > 0)) then
Break;
end;
end;

procedure ArrayToINI(const Arr: TVariantArray; const Section, KeyName, Filename: string);
var
I, H: integer;
begin
H := High(Arr);
for I := 0 to H do
WriteINI(Section, KeyName + IntToStr(I), ToStr(Arr[I]), Filename);
end;

begin
ArrayToINI(['test'], 'Test', 'Wha', ScriptPath + 'data.ini');
WriteLn(ArrayFromINI('Test', 'Wha', ScriptPath + 'data.ini', -1));
end.


while running this i get an error "unknown identifier ToStr(Arr) is that a type? shoud it be intToStr?
*edit : if i change it to intToStr, i get a type mismatch at the last line

Dgby714

2011-06-21 17:00:11

From: endlesslyonline
while running this i get an error "unknown identifier ToStr(Arr) is that a type? shoud it be intToStr?
*edit : if i change it to intToStr, i get a type mismatch at the last line


Dang… um I forgot this is in the SCAR section =O, make it a TStringArray and take away the ToStr

ToStr is a magic function in Simba, hmm i might try to make one in SCAR using format =)

program _;

function ArrayFromINI(const Section, Keyname, Filename: string; const Max: integer): TStringArray;
var
I: integer;
T: string;
begin
I := 0;
while True do
begin
T := ReadINI(Section, KeyName + IntToStr(I), FileName);

if (T <> '') then
begin
SetLength(Result, I + 1);
Result[I] := T;
end else
Break;

Inc(I);
if ((I > Max) and (Max > 0)) then
Break;
end;
end;

procedure ArrayToINI(const Arr: TStringArray; const Section, KeyName, Filename: string);
var
I, H: integer;
begin
H := High(Arr);
for I := 0 to H do
WriteINI(Section, KeyName + IntToStr(I), Arr[I], Filename);
end;

begin
ArrayToINI(['test'], 'Test', 'Wha', ScriptPath + 'data.ini');
WriteLn(ArrayFromINI('Test', 'Wha', ScriptPath + 'data.ini', -1));
end.

endlesslyonline

2011-06-21 17:09:20

um, okay, i can see where this is going, maybe i forgot to mention something, or i am reading it wrong.

this is what my INI looks like:


[monster]
goat=2324
sheep=45463
pig=5442
wrath=674

i want to populate the combo with the names of the monsters, but by the looks of these functions, it looks like its assuming the keyname is in number format? correct me if i am wrong

Dgby714

2011-06-21 17:10:48

From: endlesslyonline
um, okay, i can see where this is going, maybe i forgot to mention something, or i am reading it wrong.

this is what my INI looks like:


[monster]
goat=2324
sheep=45463
pig=5442
wrath=674

i want to populate the combo with the names of the monsters, but by the looks of these functions, it looks like its assuming the keyname is in number format? correct me if i am wrong


Why not use SaveToFile? (Sorry if this is not in SCAR) Like say ComboBox.Lines.SaveToFile and LoadFromFile =)
Actually that will look weird with the numbers


program _;

var
Data: TStringList;

procedure Change(Sender: TObject);
begin
TForm(TComboBox(Sender).Parent).Caption := 'Test: ' + Data.Values[TComboBox(Sender).Text];
end;

procedure FormInit;
var
Form: TForm;
Combo: TComboBox;
I: integer;
begin
Form := TForm.Create(nil);
with Form do
begin
Caption := 'Test';
Width := 200;
Height := 22;
end;

Data := TStringList.Create;
//Data.LoadFromFile('...');
Data.Values['Sheep'] := '1';
Data.Values['Cat'] := '2';
Data.Values['Dgby714'] := '3';
Data.Values['BraK'] := '4';
Data.Values['Dog'] := '5';

Combo := TComboBox.Create(Form);
with Combo do
begin
Parent := Form;
Align := alClient;
Style := csDropDownList;

for I := 0 to Data.Count - 1 do
Items.Add(Data.Names[I]);

OnChange := @Change;
end;

Form.ShowModal;

Data.Free;
Form.Free;
end;

var
V: TVariantArray;

begin
ThreadSafeCall('FormInit', V);
end.

OffTopic: BraK, Your not responding =P

endlesslyonline

2011-06-22 02:31:00

i think i have seen the saveToFile and loadFromFile in SCAR, will check.

Thanks again for all the responses.

Freddy1990

2011-06-26 04:16:54

On a side-note, make sure you call the method to fill up the combobow with ThreadSafeCall or you risk getting access violations.