Solved

ItemCount in a TMenuItem

Posted on 1998-06-17
6
348 Views
Last Modified: 2010-04-04
A simple question...

I am using D1 and WFW 3.11.  Can someone please tell me what is wrong in the following procedure.  It won't accept ItemCount as a property of the TMenuItem, but in the help file the example shows it should.  I have created the MenuItem(s) at runtime and want to check/uncheck items when clicked by the user. (So that only the latest clicked item is checked).  If I use Count instead of ItemCount it is accepted but always gives a value of 0.

procedure TForm1.NewItemClick(Sender: TObject);
var FName: String; N: Integer; ThisItem: TMenuItem;
begin
     ThisItem := TMenuItem.Create(File1);
     FName := TMenuItem(Sender).Name;
     For N := 0 to ThisItem.ItemCount-1 do               ThisItem.Items[N].Checked := False;
     TMenuItem(Sender).Checked := True;
     ThisItem.Free;
end;
0
Comment
Question by:jdthedj
[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
6 Comments
 
LVL 5

Expert Comment

by:ronit051397
ID: 1353340
As far as I know there is no propery called ItemCount
0
 
LVL 1

Expert Comment

by:Socrates050697
ID: 1353341
Try using TMenuitem.Items.Count

ie in your program



    procedure TForm1.NewItemClick(Sender: TObject);
    var FName: String; N: Integer; ThisItem: TMenuItem;
    begin
         ThisItem := TMenuItem.Create(File1);
         FName := TMenuItem(Sender).Name;
         For N := 0 to ThisItem.Items.Count-1 do               ThisItem.Items[N].Checked := False;
         TMenuItem(Sender).Checked := True;
         ThisItem.Free;
    end;


Socrates
0
 
LVL 3

Accepted Solution

by:
vladika earned 50 total points
ID: 1353342
type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    file1: TMenuItem;
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
    function CreateMenuItem(MenuCaption: string): TMenuItem;
    procedure OnMenuClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function TForm1.CreateMenuItem(MenuCaption: string): TMenuItem;
begin
  Result := TMenuItem.Create(Self);
  Result.Caption := MenuCaption;
  Result.RadioItem := True;        // !!!!!!!!!!!!!!! - only one item checked
  Result.OnClick := OnMenuClick;     // OnClick event handler
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  // add to menu File1 MenuItem with Caption = Edit1.Text
  File1.Add(CreateMenuItem(Edit1.Text));
end;

procedure TForm1.OnMenuClick(Sender: TObject);
begin
  TMenuItem(Sender).Checked := True;  // Set checked
end;

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 3

Author Comment

by:jdthedj
ID: 1353343
Hi

Thanks for all the replies.  

Socrates - that doesn't work either.

Ronit - One has to wonder why the help file shows ItemCount in its example code

Vladika - D1 doesn't have RadioItem capability in a TMenuItem, so I'm still stuck.

Any further ideas??
0
 
LVL 3

Expert Comment

by:vladika
ID: 1353344
In Delphi 1

type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    file1: TMenuItem;
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure OnMenuClick(Sender: TObject);
  private
    function CreateMenuItem(MenuCaption: string): TMenuItem;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function TForm1.CreateMenuItem(MenuCaption: string): TMenuItem;
begin
  Result := TMenuItem.Create(Self);
  Result.Caption := MenuCaption;   // Set Caption
  Result.OnClick := OnMenuClick;   // OnClick event handler
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  File1.Add(CreateMenuItem(Edit1.Text));
end;

procedure TForm1.OnMenuClick(Sender: TObject);
var I: Integer;
    ParentMenu: TMenuItem;
begin
  ParentMenu := TMenuItem(Sender).Parent;  // Parent Menu Item
  for I := 0 to ParentMenu.Count-1 do
    ParentMenu.Items[I].Checked := False;  // Uncheck all Items in ParentMenu
  TMenuItem(Sender).Checked := True;  // Check clicked MenuItem
end;

//

ItemCount is misprint -> Use Count



0
 
LVL 3

Author Comment

by:jdthedj
ID: 1353345
Thanks Vladika - I tried all sorts of things, but couldn't figure out how to refer to the MenuItems.  I'm very grateful for your help.
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SUM 2 INTEGER ARRAYS INTO 1 10 118
Need Help Delphi 2010 CheckBox1 Stored value in memo 13 102
DBGrid or StringGrid ? 6 138
Installshield for Embarcadero EX 10.1 Berlin 4 93
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

752 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