Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comobo Box question

Status
Not open for further replies.

pierrotsc

Programmer
Nov 25, 2007
358
US
Let's see if i can explain.
I have a combobox that is going to save presets. each time a preset is saved, it is added to the drop down list.
Each preset is a command. If i know the number of preset, I can write :
case presetscombo.itemindex of
-1, 0:
begin
{ }
end;
1:
begin
ToningFile := TINIFile.Create(directoireexit+ '\presets\myinifile.ini');
ToningCurve.ImportCurvesfromINI(ToningFile,'Toning');
end;

But that is hard coded. How would I handle itemindex 7 for example that was just added to the list?

I guess i want something dynamic and not static.

I hope i am making sense.
Thanks.
PO
 
You're saying that 'case presetscombo.itemindex' won't work because you won't know how many items you'll have, meaning you can't rererence them by hardcoded number, right? So how about this:
Code:
procedure TForm1.PresetsComboCloseUp(Sender: TObject);
begin
  ToningFile := TINIFile.Create(directoireexit+ '\presets\myinifile.ini');
  ToningCurve.ImportCurvesfromINI(ToningFile, PresetsCombo.Items[PresetsCombo.ItemIndex]);
end;

Roo
Delphi Rules!
 
Roo, your assumption is correct. the 'Toning' you see is the inistection of the inifile. It is written inside the file so it is not a file name. The myinifile.ini is really the file name. It will be in a requester that asks for a user's input. So it can be anything.

Thanks for the input.
PO
 
why not save the preset at the same time you add it to the combobox?

you are aware you can assign an object for each item in the combobox?

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Nope, i am not aware of that. What do you mean? What should I look for? The presets are saved in a ini file because they contains values.
When the user click on the save preset icon, a requester asks for the preset's name. The name is added to the combobox list and it is saved in an ini file with its name. Now when i pull the drop down list, i see this new preset. When i click on it, i want to be able to reload the ini file.
In my code, i do not have an itemindex for this new value that says:
case of:
1:
2:
3:
new item: ???

Maybe i am too complicated and it is a simple answer.
Thanks.
PO
 
the answer is really simple:

make a new object:

Code:
type
  TPresetObject = class(TObject)
  public
    Name : String;
    IniSection : String;
    ... // what ever data you need 
  end;

now in the save preset code you can do this:

Code:
procedure TForm1.SavePreset(PresetName, IniSection : string);

var Preset : TPresetObject;

begin
 Preset := TPresetObject.Create;
 Preset.Name := PresetName;
 Preset.IniSection := PresetIniFile;
 // bind this object to the item in the combobox
 ComboBox1.AddItem(PresetName, Preset);
end;

now you can recall the object
Code:
procedure TForm1.Combobox1Change(Sender : Tobject); 

var Preset : TPresetObject;

begin
 Preset := TPreset(ComboBox1.Items.Objects[ComboBox1.ItemIndex]);
 // do what you need to do... 
end;

you shouldn't care about itemindex, put all the info / related code into the object and bind it to the combobox item.

clear?

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Thank you very much for your input. That is really above my delphi coding knowledge but I feel it is worth learning.

Let me ponder on you code and try to decipher it.
Thank you very much for your vaulable input.
PO
 
The myinifile.ini is really the file name
Perhaps you misunderstood my code example. It made no change to the INI file name. Where 'Toning' is the key you were seeking, you code was locating it by index-number. My example inserts the index-text, thereby eliminating the need for index-number, as I thought you were trying to avoid. Say the next item was 'Shading'. If selected, the contents of PresetsCombo.Items[PresetsCombo.ItemIndex] (which equates to a string) would be 'Shading'. I also assumed you would have added it to the INI-file elsewhere in your code.

IOW; all I was showing you was how to pick it by 'name', rather than by 'number'.

Roo
Delphi Rules!
 
I see now...It is indeed an excellent idea. Let me see if I can implement that.
Appreciate it.
Thanks.
PO
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top