Solved

ItemCount in a TMenuItem

Posted on 1998-06-17
6
334 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

22 Experts available now in Live!

Get 1:1 Help Now