Navigation: TextEd > Extension and Scripts > Visual components >

TElMainMenu, TElMenuItem

 

 

 

 

Create a main menu at the top of the form.

 


TElMainMenu

 


Properties



 

property int DrawStyle;

The main menu can be drawn using 1 of 4 different drawing styles.

 

0 - Normal

1 - Office XP style

2 - Windows XP

3 - Windows Vista


 

property TElMenuItem Items;

Items is a single TElMenuItem object that describes the elements of the menu in its own Items property.

 

Use Items to add new menu items in the top menu row.


Ex.

TElMainMenu menu = TElMainMenu.Create(form);

TElMenuItem menuFile = TElMenuItem.Create(menu);

menuFile.Caption = "&File";

menuFile.Name = "mFile";

menu.Items.Add(menuFile);

 

 


Methods



 

constructor TElMainMenu Create(TComponent AOwner);

Create a main menu object. The AOwner parameter indicates the component (usually a form) that is responsible for managing the memory associated with the menu.


Ex.

TElMainMenu menu = TElMainMenu.Create(form);


 

procedure Free;

Destroys a menu object and frees its associated memory. This is usually not necessary though. The main menu will be destroyed automatically when the form closes.

 

 


TElMenuItem

TMenuItem describes the properties of an item in a menu.

 


Properties



 

property bool AutoCheck;

Indicates whether the menu item's checked state toggles automatically when the item is clicked.


 

property string Caption;

Specifies the text of the menu item.


 

property bool Checked;

Specifies whether a check mark should appear beside the Caption.

Use Checked to allow a menu item to work like a check box. If Checked is true, the menu item appears checked. If Checked is false, the menu item remains unchecked.


 

property int Count;

Indicates the number of subitems of the menu item. Read Count to determine the number of subitems listed in the Items property array.


 

property bool Enabled;

Specifies whether the menu item is enabled. Use Enabled to enable or disable a menu item. If Enabled is true, the Click method is called when the user selects the item with the mouse. If Enabled is false, the menu item appears dimmed and the user cannot select it.


 

property int GroupIndex;

Identifies the logical group to which the menu item belongs.


 

property TMenuItem Items[int Index];

Lists the menu items in the submenu of the menu item. Use Items to access to a subitem by its position in the list of subitems. The value of Index is the position of the subitem within the Items array. For example, if an application has a File drop-down menu that contains the menu items New, Open, and Save, in that order, the expression

 

TElMenuItem saveMenu = fileMenu.Items[2];

 

refers to the Save command.


 

property int MenuIndex;

Indicates the index of the menu item within its parent menu.


 

property string Name;

Specifies the name of the component as referenced in code.


 

property TMenuItem Parent;

Identifies the parent menu item of this menu item.


 

property Boolean RadioItem;

Specifies whether the menu item is mutually exclusive with other menu items in its group.

Use RadioItem to make the menu item behave like a radio button with a group of other menu items. The group of menu items is the set of all menu items in a pop-up or drop-down menu that have the same value of GroupIndex. When RadioItem is true, only one menu item in the group that contains the menu item can be checked at a time. The single selected menu item in the group is indicated by a round dot next to the Caption.


 

property Boolean Visible;

Specifies whether the menu item appears in the menu.


 


Methods



 

constructor TMenuItem Create(TComponent AOwner);

Create a menu item at runtime.

 

Ex.

TElMainMenu menu = TElMainMenu.Create(form);

TElMenuItem menuFile = TElMenuItem.Create(menu);


 

procedure Add(TMenuItem Item);

Use Add to add new menu items to the dropdown menu for this menu item.


 

procedure Clear;

Removes and frees all menu items listed in the Items property.


 

procedure Click;

Simulates a mouse click. Click generates an OnClick event, as if the user had clicked the menu item.


 

procedure Delete(int Index);

Removes a menu item from the Items property array.


 

function TMenuItem Find(string ACaption);

Locates a menu item in the Items property array given its caption.


Ex.

TElMenuItem menuSave = menu.Find("&Save");


 

procedure Free;

Destroys an object and frees its associated memory. This is usually not necessary for menu items since they are freed automatically when the window closes.


 

function int IndexOf(TMenuItem Item);

Returns the position of a menu item within the Items array.


 

procedure Insert(int Index, TMenuItem Item);

Inserts a menu item into a specified position in the Items array.


 

procedure Remove(TMenuItem Item);

Removes a menu item from the Items property array. Call Remove to remove a menu item and all its submenus from a menu. The Item parameter is the item to be removed.


 


Events



 

OnClick 

Occurs when the user select the menu.


 


Usage



 

C++ example


 

TForm f;

TMemo memo;

TElMainMenu menu;

TElMenuItem menuFile,menuOpen,menuSave,menuPrint,menuExit;

TElMenuItem menuDiv1,menuDiv2;

TElMenuItem menuHelp,menuAbout;

 

// Open a file dialog and open a file in the memo.

void menuOpenClick(TObject Sender) {

   TOpenDialog openDlg = TOpenDialog.Create(f);

   openDlg.DefaultExt = ".txt";

   openDlg.Filter = "Text files|*.txt";

   if (openDlg.Execute) {

      memo.Lines.LoadFromFile(openDlg.FileName);

   }

   openDlg.Free;

}

 

// Open a file dialog and save the text.

void menuSaveClick(TObject Sender) {

   TSaveDialog saveDlg = TSaveDialog.Create(f);

   saveDlg.DefaultExt = ".txt";

   saveDlg.Filter = "Text files|*.txt";

   if (saveDlg.Execute) {

      memo.Lines.SaveToFile(saveDlg.FileName);

   }

   saveDlg.Free;

}

 

// Open a print dialog and print.

void menuPrintClick(TObject Sender) {

   TPrintDialog printDlg = TPrintDialog.Create(f);

   printDlg.PrintRange = prAllPages;

   if (printDlg.Execute) {

      int line = 0;

      Printer.BeginDoc;

      Printer.Canvas.Font = memo.Font;

      for (int i = 0; i < memo.Lines.Count - 1; i++) {

         Printer.Canvas.TextOut(0, line, memo.Lines[i]);

         line = line + (-Printer.Canvas.Font.Height);

         if (line >= Printer.PageHeight) {

           Printer.NewPage;

         }

      }

      Printer.EndDoc;

   }

   printDlg.Free;

}

 

// Exit the script

void menuExitClick(TObject Sender) {

   f.Close;

}

 

// Show about message

void menuAboutClick(TObject Sender) {

   ShowMessage("Script created by Rickard Johansson!");

}

 

// Main procedure

{

   // Create a new window (form)

   f = new TForm(nil);

   f.Caption = "A simple text editor!";

   f.Position = poScreenCenter;

   f.Width = 400;

   f.Height = 300;

  

   // Create main menu ---------------------------------------------------------

  

   menu = TElMainMenu.Create(f);

   menu.DrawStyle = 1;

  

   // Create "File" menu item

   menuFile = TElMenuItem.Create(menu);

   menuFile.Caption = "&File";

   menuFile.Name = "mFile";

   menu.Items.Add(menuFile);

      

   // Add menu item "Open..."

   menuOpen = TElMenuItem.Create(menuFile);

   menuOpen.Caption = "&Open...";

   menuOpen.OnClick = &menuOpenClick;

   menuOpen.Name = "mOpen";

   menuFile.Add(menuOpen);

 

   // Add menu item "Save As..."

   menuSave = TElMenuItem.Create(menuFile);

   menuSave.Caption = "&Save As...";

   menuSave.OnClick = &menuSaveClick;

   menuSave.Name = "mSave";

   menuFile.Add(menuSave);

      

   menuDiv1 = TElMenuItem.Create(menuFile);

   menuDiv1.Caption = "-";

   menuFile.Add(menuDiv1);       

      

   // Add menu item "Print..."

   menuPrint = TElMenuItem.Create(menuFile);

   menuPrint.Caption = "&Print...";

   menuPrint.OnClick = &menuPrintClick;

   menuPrint.Name = "mPrint";

   menuFile.Add(menuPrint);

      

   menuDiv1 = TElMenuItem.Create(menuFile);

   menuDiv1.Caption = "-";

   menuFile.Add(menuDiv1);

      

   // Add menu item "Exit"

   menuExit = TElMenuItem.Create(menuFile);

   menuExit.Caption = "&Exit";

   menuExit.OnClick = &menuExitClick;

   menuExit.Name = "mExit";

   menuFile.Add(menuExit);

      

   // Create "Help" menu item

   menuHelp = TElMenuItem.Create(menu);

   menuHelp.Caption = "&Help";

   menuHelp.Name = "mHelp";

   menu.Items.Add(menuHelp);

      

   // Add menu item "About..."

   menuAbout = TElMenuItem.Create(menuHelp);

   menuAbout.Caption = "&About...";

   menuAbout.OnClick = &menuAboutClick;

   menuAbout.Name = "mAbout";

   menuHelp.Add(menuAbout);

 

   //---------------------------------------------------------------------------

  

   // Add a text edit control (memo) to the window

   memo = new TMemo(f);

   memo.Name = "memoLorem";

   memo.Parent = f;

   memo.ScrollBars = ssBoth;

   memo.SetBounds(10, 10, 380, 280);

   memo.Anchors = akLeft+akTop+akRight+akBottom;

   memo.Lines.Text = "Lorem Ipsum iaculis audire mi moderatius\r\ncorpora dictumst";

                  

   // Show the window

   f.ShowModal;

   f.Free;

}


 


 


 


 


 


 


 


 

Copyright © 2004-2020 by Rickard Johansson. All Rights Reserved.

 

 

 

Copyright © 2024 Rickard Johansson