Board index » delphi » Help with Array and INI file

Help with Array and INI file

Can anyone tell me how to fix a bug in my program?
My program has around 12 comboboxes with 99 items in each. I'm using a procedure to get the values
since the items for all combos are the same. I want to create an ini file to store the items selected so the user
doesn't have to reselect the items next time the program is run. Below is the code I am using (sorry about the
length of this post) When I run the program it shows the item that was saved but due to the procedure being
used it acts as if the combo hasnt got a value in it? Any ideas?  I removed some of the values in this post to
make it smaller.

procedure TForm1.AnyComboBoxChange(Sender: TObject);
const
  XPLevels: array [0..98] of Integer
          = (0, 83, 174, 276, 388, 512, 650, 801, 969, 1154, 1358, 1584,
          9684577, 10692629, 11805606, 13034431);
var
  Index: Integer;
begin
  LevelXP := 0;
  Index:=(Sender as TComboBox).ItemIndex;
  LevelXP:=XPLevels[Index];
end;

procedure TForm1.ComboBox11Change(Sender: TObject);
begin
  AnyComboBoxChange(Sender);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
INI: TIniFile;
I, Item: Integer;

begin
ComboBox1.ItemIndex := 0;

  INI := TIniFile.Create('RCalc.dat');

ComboBox2.ItemIndex := INI.ReadInteger('ComboBox2','Item', 0);
ComboBox3.ItemIndex := INI.ReadInteger('ComboBox3','Item', 0);

  INI.Free;

end;

 

Re:Help with Array and INI file


Quote
"MerLiN" <merli...@ihug.com.au> wrote in message

news:77ahhu4nnli72q93g1knt6toupl2dbfjf9@4ax.com...

try putting the call to

AnyComboBoxChange(Sender);

in the Form's OnCreate handler.

Re:Help with Array and INI file


I tried that but I get an exception class error.
The error points to this in the AnyComboBoxChange procedure

Index:=(Sender as TComboBox).ItemIndex;

On Tue, 25 Jun 2002 18:34:58 +0000 (UTC), "Windoze Ed [aka venuslocal]" <venuslo...@NOSPAM.hotmail.com> wrote:

Quote

>"MerLiN" <merli...@ihug.com.au> wrote in message
>news:77ahhu4nnli72q93g1knt6toupl2dbfjf9@4ax.com...

>try putting the call to

>AnyComboBoxChange(Sender);

>in the Form's OnCreate handler.

Re:Help with Array and INI file


Try this for the create and close procedures:

procedure TForm1.FormCreate(Sender: TObject);
var reg : Tinifile;
begin
reg := TIniFile.Create('cbtest.ini');

combobox1.ItemIndex := reg.ReadInteger('cb','item1',-1);
combobox2.ItemIndex := reg.ReadInteger('cb','item2',-1);
combobox3.ItemIndex := reg.ReadInteger('cb','item3',-1);
combobox4.ItemIndex := reg.ReadInteger('cb','item4',-1);
combobox5.ItemIndex := reg.ReadInteger('cb','item5',-1);
combobox6.ItemIndex := reg.ReadInteger('cb','item6',-1);

reg.Destroy;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var reg : Tinifile;
begin

reg := TIniFIle.Create('cbtest.ini');
reg.WriteInteger('cb','item1',combobox1.ItemIndex);
reg.WriteInteger('cb','item2',combobox2.ItemIndex);
reg.WriteInteger('cb','item3',combobox3.ItemIndex);
reg.WriteInteger('cb','item4',combobox4.ItemIndex);
reg.WriteInteger('cb','item5',combobox5.ItemIndex);
reg.WriteInteger('cb','item6',combobox6.ItemIndex);

reg.Destroy;
end;

Jeff Steinkamp

Quote
"MerLiN" <merli...@ihug.com.au> wrote in message

news:77ahhu4nnli72q93g1knt6toupl2dbfjf9@4ax.com...
Quote
> Can anyone tell me how to fix a bug in my program?
> My program has around 12 comboboxes with 99 items in each. I'm using a

procedure to get the values
Quote
> since the items for all combos are the same. I want to create an ini file

to store the items selected so the user
Quote
> doesn't have to reselect the items next time the program is run. Below is

the code I am using (sorry about the
Quote
> length of this post) When I run the program it shows the item that was

saved but due to the procedure being
Quote
> used it acts as if the combo hasnt got a value in it? Any ideas?  I

removed some of the values in this post to
Quote
> make it smaller.

> procedure TForm1.AnyComboBoxChange(Sender: TObject);
> const
>   XPLevels: array [0..98] of Integer
>           = (0, 83, 174, 276, 388, 512, 650, 801, 969, 1154, 1358, 1584,
>           9684577, 10692629, 11805606, 13034431);
> var
>   Index: Integer;
> begin
>   LevelXP := 0;
>   Index:=(Sender as TComboBox).ItemIndex;
>   LevelXP:=XPLevels[Index];
> end;

> procedure TForm1.ComboBox11Change(Sender: TObject);
> begin
>   AnyComboBoxChange(Sender);
> end;

> procedure TForm1.FormCreate(Sender: TObject);
> var
> INI: TIniFile;
> I, Item: Integer;

> begin
> ComboBox1.ItemIndex := 0;

>   INI := TIniFile.Create('RCalc.dat');

> ComboBox2.ItemIndex := INI.ReadInteger('ComboBox2','Item', 0);
> ComboBox3.ItemIndex := INI.ReadInteger('ComboBox3','Item', 0);

>   INI.Free;

> end;

Re:Help with Array and INI file


Nope that didn't work either.
even tho I didn't include the code for formclose in my first post I have that in the program.
I know it has something to do with the procedure AnyComboBoxChange. Changing an item
in a combobox calls that procedure if done manually. But using the ini file it doesnt it only selects
the item in the combobox.
Quote
On Tue, 25 Jun 2002 19:13:35 GMT, "Jeff Steinkamp" <n...@arrl.net> wrote:
>Try this for the create and close procedures:

>procedure TForm1.FormCreate(Sender: TObject);
>var reg : Tinifile;
>begin
>reg := TIniFile.Create('cbtest.ini');

>combobox1.ItemIndex := reg.ReadInteger('cb','item1',-1);
>combobox2.ItemIndex := reg.ReadInteger('cb','item2',-1);
>combobox3.ItemIndex := reg.ReadInteger('cb','item3',-1);
>combobox4.ItemIndex := reg.ReadInteger('cb','item4',-1);
>combobox5.ItemIndex := reg.ReadInteger('cb','item5',-1);
>combobox6.ItemIndex := reg.ReadInteger('cb','item6',-1);

>reg.Destroy;
>end;

>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
>var reg : Tinifile;
>begin

>reg := TIniFIle.Create('cbtest.ini');
>reg.WriteInteger('cb','item1',combobox1.ItemIndex);
>reg.WriteInteger('cb','item2',combobox2.ItemIndex);
>reg.WriteInteger('cb','item3',combobox3.ItemIndex);
>reg.WriteInteger('cb','item4',combobox4.ItemIndex);
>reg.WriteInteger('cb','item5',combobox5.ItemIndex);
>reg.WriteInteger('cb','item6',combobox6.ItemIndex);

>reg.Destroy;
>end;

>Jeff Steinkamp

>"MerLiN" <merli...@ihug.com.au> wrote in message
>news:77ahhu4nnli72q93g1knt6toupl2dbfjf9@4ax.com...
>> Can anyone tell me how to fix a bug in my program?
>> My program has around 12 comboboxes with 99 items in each. I'm using a
>procedure to get the values
>> since the items for all combos are the same. I want to create an ini file
>to store the items selected so the user
>> doesn't have to reselect the items next time the program is run. Below is
>the code I am using (sorry about the
>> length of this post) When I run the program it shows the item that was
>saved but due to the procedure being
>> used it acts as if the combo hasnt got a value in it? Any ideas?  I
>removed some of the values in this post to
>> make it smaller.

>> procedure TForm1.AnyComboBoxChange(Sender: TObject);
>> const
>>   XPLevels: array [0..98] of Integer
>>           = (0, 83, 174, 276, 388, 512, 650, 801, 969, 1154, 1358, 1584,
>>           9684577, 10692629, 11805606, 13034431);
>> var
>>   Index: Integer;
>> begin
>>   LevelXP := 0;
>>   Index:=(Sender as TComboBox).ItemIndex;
>>   LevelXP:=XPLevels[Index];
>> end;

>> procedure TForm1.ComboBox11Change(Sender: TObject);
>> begin
>>   AnyComboBoxChange(Sender);
>> end;

>> procedure TForm1.FormCreate(Sender: TObject);
>> var
>> INI: TIniFile;
>> I, Item: Integer;

>> begin
>> ComboBox1.ItemIndex := 0;

>>   INI := TIniFile.Create('RCalc.dat');

>> ComboBox2.ItemIndex := INI.ReadInteger('ComboBox2','Item', 0);
>> ComboBox3.ItemIndex := INI.ReadInteger('ComboBox3','Item', 0);

>>   INI.Free;

>> end;

Other Threads