Solved

ItemCount in a TMenuItem

Posted on 1998-06-17
6
338 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
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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to fill array with TArray.Create? 14 90
Delphi IDE crash without error message ... 7 91
Press three keys together and trigger a function 3 57
creating threads in delphi 1 105
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…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

809 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