Link to home
Start Free TrialLog in
Avatar of BoRiS
BoRiS

asked on

Have I gone MAD (Menu Question)

I seem to have gone MAD or lost it or something...(O and hello everyone one I'm back again ;-)  )

Here's the problem I have an .ini file that looks like this...

[Section 1]
Item1=notepade.exe
Item2=wordpad.exe
Item3=calc.exe

[Section 2]
Item4=ipconfig.exe
Item5=explorer.exe
Item6=progman.exe

What I'm trying to do is with a popupmenu populate the menu from this .ini so the "Main Menu Item" are the section headers (e.g. Section 1, Section 2 etc) then as sub menus of these main item the section values.

Eg. Picture this as the menu...

Section 1 > Item1
            Item2
            Item3

Section 2 > Item4
            Item5
            Item6

Getting the main items is no problem but the subitems seem to have me in a hugs jacket...

This is all done on the FormCreate Event.

Later
BoRiS
ASKER CERTIFIED SOLUTION
Avatar of wmckie
wmckie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lischke
Lischke

Hey BoRiS,

nice to have you back. Where have you been so long?

To answer your question:

1) Start with TInitFile.ReadSections to get your main menu items and
2) Use ReadSectionValues for each section to retrive the entries there

Both methods return a TStrings (use a TStringList there) filled up with the values.


Ciao, Mike
Avatar of BoRiS

ASKER

Hi Walter

Thanx for the comment but that is just creating a popupmenu on the fly with set items, this needs to be dynamic for instance I can add the the .ini file at any time and the app will add the menu items...

Hi Mike
Thank you, I have been hard at work, coding wee hours into the night ;-)

I use what you suggested already and it works fine for main items (ReadSections)
but my problem is the subitems it either gives all the main items the same subitems (item1 to 3) or all the main items have all the subitems in one list (item1 to 6)

Am I missing something here¿¿¿

Later
BoRiS

???? Walter's code correctly creates popup sub-items, so why not simply substitiute Walter's fixed strings with the ones you read from the ini?
Avatar of BoRiS

ASKER

God Ares

Thanx for the link, not what I'm looking for but has some interesting stuff ;-)

Later
BoRiS

Avatar of BoRiS

ASKER

Mullet attack

Mmmmm I've tried this, but let me check again maybe I ***ked it up...

Later
BoRiS
Mmh, I don't understand your problem, BoRiS. When you have read the section values into a list then you can simply iterate over it like:

ReadSections;
for each section do
  create a section menu item as you already did;
  for I := 0 to SectionEntries.Count -1 do
  begin
    Item := NewItem(SectionEntries[I], 0, False, True, IconClick, 0, '');
    Item.Tag := I;
    Item.ImageIndex := I;
    SectionItem.Add(Item);
  end;

Ciao, Mike
Avatar of BoRiS

ASKER

Mullet attack

My appologies to you and Walter, it seems as though I did ***k it up the first time I tried it...

here is the code if anyone is interested

procedure TForm1.FormCreate(Sender: TObject);
var
I, M: integer;
IList, MList: TStringlist;
TestIni: TIniFile;
begin
IList := TStringList.Create;
MList := TStringList.Create;
 TestIni := TIniFile.Create('Junxion.ini');
  TestIni.ReadSections(IList);
   for I := 0 to IList.Count-1 do begin
    PopupMenu1.Items.Add(TMenuItem.Create(PopupMenu1));
    PopupMenu1.Items[I].Caption := IList.Strings[I];
     TestIni.ReadSection(IList.Strings[I], MList);
      for M := 0 to MList.Count-1 do begin
       PopupMenu1.Items[I].Add(TMenuItem.Create(PopupMenu1));
       PopupMenu1.Items[I].Items[M].Caption := MList.Strings[M];
      end;
   end;
end;

Walter I will give the points after I have posted this comment...

Thanx everyone, I had an involuntrily lapse of reason ;-)

Later
BoRiS
Avatar of BoRiS

ASKER

Mike

Like I said I had an involuntrily lapse of reason (or a blonde moment, no offense to the blondes), maybe I should go get some sleep or something...

Later
BoRiS
BoRiS,

Thanks for the points, I was in the process of drafting another reply when your acceptance came through. My amended code is almost identical to yours except I've included a bit to split the section value at the '='.

Thought I'd pass it anyway.

procedure TForm1.FormCreate(Sender: TObject);
var
  Ini: TIniFile;
  Sections, Values: TStringList;
  C1, C2: word;
  S: string;
begin
  Sections := TStringList.Create;
  Values := TStringList.Create;
  Ini := TIniFile.Create('D:\Program Files\Borland\Delphi4\Projects\Test.ini');
  Ini.ReadSections(Sections);
  for C1 := 0 to Sections.Count - 1 do
    begin
      { Create the  sections in the Popup menu }
      PopupMenu.Items.Add(TMenuItem.Create(PopupMenu));
      PopupMenu.Items[C1].Caption := Sections.Strings[C1];
      Ini.ReadSectionValues(Sections.Strings[C1], Values);
      for C2 := 0 to Values.Count - 1 do
        begin
          PopupMenu.Items[C1].Add(TMenuItem.Create(PopupMenu));
          { Use the Value name as the menu item caption, i.e before the '=' }
          S := Values.Strings[C2];
          { Change the next line if the caption is to be got from after the '=' }
          Delete(S, Pos('=', S),  Length(S) - Pos('=', S) + 1);
          PopupMenu.Items[C1].Items[C2].Caption := S;
        end;
      Values.Clear;
    end;
  Ini.Free;
  Sections.Free;
  Values.Free;
end;

Sweet dreams .....

Walter