Solved

Have I gone MAD (Menu Question)

Posted on 2000-05-11
11
271 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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now