Board index » delphi » Menu postion problem when updated at runtime

Menu postion problem when updated at runtime

Hi,
    The main form of my application has a menu bar, designed with the
menu editor, which works perfectly happily until I attempt to add a list
of recently used files to the File menu.  After this, the menu is
displayed at the top left of the window (covering the system icon and
menu bar) instead of its usual place below the menu bar.  The code I'm
using is:

procedure TfrmCSVNav.File1Click(Sender: TObject);

var i:    Integer;
    t:    TMenuItem;

begin
     For i := 0 to 4 do begin
         t := TMenuItem.Create(File1);
         t.Caption := CSVFileHistory.Strings[i];
         t.OnClick := OpenOldFile;
         If t.Caption <> '' then begin
            File1.Add(t);
         end;
     end;
end;

This does add the new items, but the menu position is now wrong.  I've
tried using NewItem instead, but I get the same problem.  Can anybody
help?

Cheers,
Simon

 

Re:Menu postion problem when updated at runtime


If anyone's interested, I solved this problem.  The wrong positioning of the
menu was caused by the fact that I was trying to update a File menu in its
own OnClick event handler - this seems like a reasonable thing to do, since
at this point I can be absolutely sure that it's up to date.  However, when
I move the update code elsewhere, the problem goes away.
Quote
Simon Figg wrote:
> Hi,
>     The main form of my application has a menu bar, designed with the
> menu editor, which works perfectly happily until I attempt to add a list
> of recently used files to the File menu.  After this, the menu is
> displayed at the top left of the window (covering the system icon and
> menu bar) instead of its usual place below the menu bar.  The code I'm
> using is:

> procedure TfrmCSVNav.File1Click(Sender: TObject);

> var i:    Integer;
>     t:    TMenuItem;

> begin
>      For i := 0 to 4 do begin
>          t := TMenuItem.Create(File1);
>          t.Caption := CSVFileHistory.Strings[i];
>          t.OnClick := OpenOldFile;
>          If t.Caption <> '' then begin
>             File1.Add(t);
>          end;
>      end;
> end;

> This does add the new items, but the menu position is now wrong.  I've
> tried using NewItem instead, but I get the same problem.  Can anybody
> help?

> Cheers,
> Simon

Re:Menu postion problem when updated at runtime


Quote
In article <36C2B49C.89B1E...@iol.ie>, Simon Figg <sf...@iol.ie> writes:
>procedure TfrmCSVNav.File1Click(Sender: TObject); <<<<<<<< Look Here

>var i:    Integer;
>    t:    TMenuItem;

>begin
>     For i := 0 to 4 do begin
>         t := TMenuItem.Create(File1);
>         t.Caption := CSVFileHistory.Strings[i];
>         t.OnClick := OpenOldFile;
>         If t.Caption <> '' then begin
>            File1.Add(t);
>         end;
>     end;
>end;

>This does add the new items, but the menu position is now wrong.  I've
>tried using NewItem instead, but I get the same problem.  Can anybody
>help?

Basically its because you're doing the addition of the items WITHIN the OnClick
of the menu itself. It also means that you are adding the MRU filesagain  each
time you drop down the menu - I don't think you want to do that do you <g> ?

Insert the MRU items at FormCreate or at some other appropriate event.

Alan Lloyd
alan gll...@aol.com

Re:Menu postion problem when updated at runtime


Hi Alan,
    As you probably saw, I actually managed to work this one out.  However, it does
raise some interesting questions about when the code for the Click event is
actually run - before, during, or after the menu actually is displayed?  If before
(as I would have expected), then surely it should make no difference whether the
update is here or elsewhere.  Because it doesn't work like this, I now have to call
the menu update procedure at program start, when a new file is opened, when an MRU
file is selected, etc., rather than from the one place where I can be certain the
MRU list is bang-up-to-date.  Oh well, I guess I'm just being lazy!
    By the way, the code I included is stripped down to illustrate the problem -
the full code obviously clears the existing MRU list before updating it.

Cheers,
Simon

Other Threads