Solved

Have I gone MAD (Menu Question)

Posted on 2000-05-11
11
277 Views
Last Modified: 2010-04-04
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
0
Comment
Question by:BoRiS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +2
11 Comments
 
LVL 1

Accepted Solution

by:
wmckie earned 200 total points
ID: 2799455
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
 
LVL 10

Expert Comment

by:Lischke
ID: 2799464
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
 
LVL 4

Author Comment

by:BoRiS
ID: 2799496
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:God_Ares
ID: 2799530
0
 
LVL 2

Expert Comment

by:mullet_attack
ID: 2799558
???? 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
 
LVL 4

Author Comment

by:BoRiS
ID: 2799559
God Ares

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

Later
BoRiS

0
 
LVL 4

Author Comment

by:BoRiS
ID: 2799575
Mullet attack

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

Later
BoRiS
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2799607
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
 
LVL 4

Author Comment

by:BoRiS
ID: 2799642
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
 
LVL 4

Author Comment

by:BoRiS
ID: 2799654
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
 
LVL 1

Expert Comment

by:wmckie
ID: 2799874
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question