Solved

Have I gone MAD (Menu Question)

Posted on 2000-05-11
11
269 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

11 Experts available now in Live!

Get 1:1 Help Now