从数据库中读数据建立菜单

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, DB, ADODB, StdCtrls;

type
  PMenuRecord = ^TMenuRecord;
  TMenuRecord = Record
    MenuId:string[20];

  end;

  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    ADOConnection1: TADOConnection;
    Button1: TButton;
    ADOQuery1: TADOQuery;
    ADOQuery2: TADOQuery;
    N1231: TMenuItem;
    N1: TMenuItem;
    ADOQuery3: TADOQuery;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private

    function createItem(s:string;ParentMenu:Tmenuitem;MainMEnu:TMainmenu
      ;Mid:string):TmenuItem;
    procedure createChileItem(mid:string;layer:string;parentItem:TMenuItem);

    procedure execItem(sender:Tobject);
  public
    { Public declarations }
  end;


var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.execItem(sender:tobject);
var
  mid:string;
  adoquery:TAdoquery;
begin
  if sender is TMenuItem then
  begin
    mid := TMenuItem(sender).name;
    adoquery := TAdoquery.Create(nil);
    adoQuery.Connection := self.ADOConnection1;
    try
      with adoquery do
      begin
        sql.Add('select m_name_call from menu where m_id='''+mid+'''');
        open;
        showmessage(mid);
      end;
    finally
      adoQuery.Free;
    end;
  end;
end;

function TForm1.createItem(s:string;ParentMenu:Tmenuitem;
     MainMEnu:TMainmenu;Mid:string):TMEnuItem;
var
    Item1:TMenuItem;
    adoquery:TAdoquery;
begin
  adoquery := TAdoquery.Create(nil);
  adoQuery.Connection := self.ADOConnection1;
  item1 := TMenuITem.Create(mainMenu);
  item1.Name := mid;

  parentMenu.Add(item1);
  item1.Caption := s;
  result := item1;
  try
    with adoquery do
    begin
      sql.Add('select m_name_call from menu where m_id='''+mid+'''');
      open;
      if Fields[0].IsNull then
        item1.OnClick := nil
      else
        item1.OnClick := self.execItem;
    end;
  finally
    adoquery.Free;
  end;
 
end;

procedure TForm1.createChileItem(mid:string;layer:string;parentItem:TMenuItem);
var
  Adoquery:TAdoQuery;
  aItem:TMenuitem;
begin
  adoquery := TAdoquery.Create(nil);
  adoquery.Connection := self.ADOConnection1;
  try
    with adoquery do
    begin
      sql.Add('select m_id,M_name from menu where m_parent_id='''+mid+'''');
      open;
      while not eof do
      begin
        aItem:=self.createItem(fields[1].AsString,parentItem,self.MainMenu1,Fields[0].asstring);
        self.createChileItem(Fields[0].asstring,'1',aItem);
        next;
      end;
    end;
  finally
    AdoQuery.Free;
  end;

end;

procedure TForm1.Button1Click(Sender: TObject);
var
  menuRecord:PMenuRecord;
  mid : string;
  aItem:TMenuitem;
  i:integer;
begin
  with adoQuery1 do
  begin
    if active then close;
    sql.Clear;
    sql.Add('select M_Id,M_name from Menu where M_layer=1');
    open;
    i:=2;
    while not eof do
    begin
      mid := fields[0].AsString;
      aitem:=self.createItem(Fields[1].asstring,self.MainMenu1.Items,self.MainMenu1,mid);
      aitem.MenuIndex := i;
      self.createChileItem(Fields[0].asstring,'2',aItem);
      next;
      i:=i+1;
    end;
  end;
   n1.MenuIndex := i;  //改变菜单的位置
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
  n1231.MenuIndex :=1;
 
end;

end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值