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
LVL 4
BoRiSAsked:
Who is Participating?
 
wmckieConnect With a Mentor Commented:
Hi BoRiS,

Does this do what you want?

procedure TForm1.FormCreate(Sender: TObject);
begin
  { Create the first section in the Popup menu }
  PopupMenu.Items.Add(TMenuItem.Create(PopupMenu));
  PopupMenu.Items[0].Caption := 'Section1';

  PopupMenu.Items[0].Add(TMenuItem.Create(PopupMenu));
  PopupMenu.Items[0].Items[0].Caption := 'Item1';

  PopupMenu.Items[0].Add(TMenuItem.Create(PopupMenu));
  PopupMenu.Items[0].Items[1].Caption := 'Item2';

  PopupMenu.Items[0].Add(TMenuItem.Create(PopupMenu));
  PopupMenu.Items[0].Items[2].Caption := 'Item3';

  { Create the second section in the Popup menu }
  PopupMenu.Items.Add(TMenuItem.Create(PopupMenu));
  PopupMenu.Items[1].Caption := 'Section2';

  PopupMenu.Items[1].Add(TMenuItem.Create(PopupMenu));
  PopupMenu.Items[1].Items[0].Caption := 'Item4';

  PopupMenu.Items[1].Add(TMenuItem.Create(PopupMenu));
  PopupMenu.Items[1].Items[1].Caption := 'Item5';

  PopupMenu.Items[1].Add(TMenuItem.Create(PopupMenu));
  PopupMenu.Items[1].Items[2].Caption := 'Item6';
end;

Cheers - Walter McKie
0
 
LischkeCommented:
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
0
 
BoRiSAuthor Commented:
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

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
God_AresCommented:
0
 
mullet_attackCommented:
???? 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?
0
 
BoRiSAuthor Commented:
God Ares

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

Later
BoRiS

0
 
BoRiSAuthor Commented:
Mullet attack

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

Later
BoRiS
0
 
LischkeCommented:
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
0
 
BoRiSAuthor Commented:
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
0
 
BoRiSAuthor Commented:
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
0
 
wmckieCommented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.