Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

Show date into a TMenu

I wish to show the current date on the right part of my menu. How to do that?
0
ginsonic
Asked:
ginsonic
  • 3
  • 3
  • 2
1 Solution
 
mocartsCommented:
minTime: TMenuItem;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  mi: MENUITEMINFO;
begin
  mi.cbSize := sizeOf(MENUITEMINFO);
  mi.fMask := MIIM_TYPE;
  mi.fType := MFT_RIGHTJUSTIFY or MFT_STRING;
  mi.dwTypeData := PAnsiChar(TimeToStr(Now));
  mi.cch := strLen(mi.dwTypeData);
  SetMenuItemInfo(MainMenu1.Handle, mniTime.MenuIndex, True, mi);
  DrawMenuBar(Handle);
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  Timer1Timer(Timer1);
end;

wbr, mo.
0
 
ginsonicAuthor Commented:
Where is declared mniTime? I get an error message here.
0
 
DragonSlayerCommented:
mniTime is the name of your TMenuItem that you want to designate as the one containing the time.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ginsonicAuthor Commented:
Sorry, I see it now :P, but give me an error.
0
 
DragonSlayerCommented:
what error?
0
 
mocartsCommented:
in previous example I provided code to show current time with TTimer, but q was about date :) here will be as complete example as it can be;
I removed some code which disables menu item as it can be done in menu builder and put all code in MainMenu.OnChange event to realign date menu if there is changes in menu (that forces to loose date menu item alignment).

....
type
  TForm1 = class(TForm)
  ...
    MainMenu1: TMainMenu;
    mniTime: TMenuItem;
    procedure MainMenu1Change(Sender: TObject; Source: TMenuItem;
      Rebuild: Boolean);
  ....
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.MainMenu1Change(Sender: TObject; Source: TMenuItem;
  Rebuild: Boolean);
var
  mi: MENUITEMINFO;
begin
  mi.cbSize := sizeOf(MENUITEMINFO);
  mi.fMask := MIIM_TYPE;
  mi.fType := MFT_RIGHTJUSTIFY or MFT_STRING;
  mi.dwTypeData := PAnsiChar(FormatDateTime('dd.mm.yyyy', Date));
  mi.cch := strLen(mi.dwTypeData);
  SetMenuItemInfo(MainMenu1.Handle, mniTime.MenuIndex, True, mi);
  DrawMenuBar(MainMenu1.Handle);
end;

wbr, mo.
0
 
ginsonicAuthor Commented:
I get an error here:

SetMenuItemInfo(MainMenu1.Handle, mniTime.MenuIndex, True, mi);

"Access violation at address ..... in Project1"
0
 
mocartsCommented:
you should add in MainMenu1 design time designer a menu item called mniTime.. i.e. mniTime must be created in runtime or in designer. look in debug mode is mniTime assigned?

in runtime you can create item in OnCreate event of form:

procedure TForm1.FormCreate(Sender: TObject);
begin
  mniTime := TMenuItem.Create(MainMenu1);
  MainMenu1.Items.Add(mniTime);
end;

wbr, mo.
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!

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now