Solved

ItemCount in a TMenuItem

Posted on 1998-06-17
6
342 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FMX enumerated colours 2 117
Making delphi communicate with a c# service 16 112
Multiple image collision 13 80
tvirtualstringtree freeze when load too manny images 10 71
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…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

829 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