我的项目是这样的:我建了一个StringGrid表格,表格中有如下内容(远不止这些,有3000多条)显示参数
参数号 名称                  含义
1.00 DI1状态              0=低电平,1=高电平
1.01 DI2状态              0=低电平,1=高电平
1.02 DI3状态              0=低电平,1=高电平
1.03 DI4状态              0=低电平,1=高电平
1.04 DI5状态              0=低电平,1=高电平
1.05 DI6状态              0=低电平,1=高电平
1.06 DI7状态              0=低电平,1=高电平
1.07 DI8状态              0=低电平,1=高电平
1.08 DI9状态              0=低电平,1=高电平
1.09 DI10状态              0=低电平,1=高电平
1.10 DO1状态              0=低电平,1=高电平
1.11 DO2状态              0=低电平,1=高电平
1.12 DO3状态              0=低电平,1=高电平
1.13 DI1取反状态     0=高电平,1=低电平
1.14 DI2取反状态     0=高电平,1=低电平
1.15 DI3取反状态     0=高电平,1=低电平
1.16 DI4取反状态     0=高电平,1=低电平
1.17 DI5取反状态     0=高电平,1=低电平
1.18 DI6取反状态     0=高电平,1=低电平
1.19 DI7取反状态     0=高电平,1=低电平
1.20 DI8取反状态     0=高电平,1=低电平设置参数
参数号 名称                 连接对象
20.00 DO1取反使能
20.01 DO2取反使能
20.02 DO3取反使能
20.03 AI1输出使能
20.04 AI2输出使能
20.05 AI3输出使能
20.06 AI4输出使能
20.07 AO1取反使能
20.08 AO1取绝对值使能
20.09 AO2取反使能
20.10 AO2取绝对值使能
20.11 AO3取反使能
20.12 AO3取绝对值使能
20.13 AO4取反使能
20.14 AO4取绝对值使能
20.15 DO1使能
20.16 DO2使能
20.17 DO3使能 在界面上需要完成的功能是:1、程序初始化时,动态生成一个菜单项,菜单项中包括“显示参数”表中每一条内容,2、建立N多个Edit框(根据“设置参数”内容可固定建立),3、需要在任何一个Edit框中点击右键(左键)都能弹出“显示参数”菜单,当鼠标选中其中一项时,这个被选中项能显示在当前的Edit中,并将被选中内容的参数号赋给StringGrid表中的“设置参数”的“连接对象”中去。比如:固定建立一个Edit1框-20.00 DO1取反使能,选中了菜单中的1.05 DI6状态,则在Edit1中显示:1.05 DI6状态,并且在StringGrid表中20.00 DO1取反使能的连接对象单元中写入“1.05或105”目的:我的StringGrid的内容是会随着项目增加的(数据库内容),如果不动态生成菜单和动态赋值,程序的维护量会很大,会不知道在哪个Edit中需要修改什么东西。如果按这个思路来做,我就只要维护StringGrid表了。其实我以前提过类似的问题,但是没讲的清楚:http://topic.csdn.net/u/20090804/14/4af1dab5-67e5-43b4-8832-9ca4b12ace6f.html。由于是新手,请给出具体代码,说一说我是看不会的,不好意思,谢谢!

解决方案 »

  1.   

    如果你还不会创建菜单,那下面就不要看了
    如果你会动态创建菜单,那就知识循环Grid,去显示菜单的内容,如果一个菜单对应一行的话,可以在菜单的tag属性中记录行号维护一个菜单列表,在选中grid时,根据行索引,去定位菜单列表,然后去反向控制菜单以上完全可以搞定你的问题,如果你非要想要现成的代码,那就等待有耐心的人吧
      

  2.   


    请问怎么创建tag属性呢?
    procedure TForm1.menuItemClick(Sender: TObject);
    begin
      TEdit(self.PopupMenu1.PopupComponent).Text:= TMenuItem(sender).Caption;
      with Sender as TMenuItem do
      begin
        case   Tag   of
          0:ShowMessage('1 item  clicked');
          1:ShowMessage('2 item  clicked');
          2:ShowMessage('3 item  clicked');
          3:ShowMessage('4 item  clicked');
        end;
      end;
    end;
    这样,tag永远是0?
      

  3.   

    创建了就当然得设置子菜单的TAG,默认是为0的.
      

  4.   

    解决了!这个语句就可以实现功能。但是为什么Edit中显示的内容为什么有一个&符号呢,怎么去掉?unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Menus, Grids;type
      TForm1 = class(TForm)
        Edit1: TEdit;
        PopupMenu1: TPopupMenu;
        Edit2: TEdit;
        Edit3: TEdit;
        Edit4: TEdit;
        Edit5: TEdit;
        StringGrid1: TStringGrid;
        Label1: TLabel;
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        procedure menuItemClick(Sender: TObject);
      public
        { Public declarations }  end;var
      Form1: TForm1;implementation{$R *.dfm}
    procedure TForm1.FormCreate(Sender: TObject);
    var
      index,i:Integer;
      NewItem:TMenuItem;
      NewSubitem:TMenuItem;
    begin
      { Set grid headers titles }
      StringGrid1.Cells[0, 0] := '参数号';
      StringGrid1.Cells[1, 0] := '十进制';
      StringGrid1.Cells[2, 0] := '十六进制';
      StringGrid1.Cells[3, 0] := '二进制';
    { Set the column width }
      StringGrid1.ColWidths[3] := 120;  StringGrid1.Cells[0, 1] := '100';
      StringGrid1.Cells[0, 2] := '101';
      StringGrid1.Cells[0, 3] := '102';
      StringGrid1.Cells[0, 4] := '103';
      StringGrid1.Cells[0, 5] := '104';
      StringGrid1.Cells[0, 6] := '105';
      StringGrid1.Cells[0, 7] := '106';
      StringGrid1.Cells[0, 8] := '';
      StringGrid1.Cells[0, 9] := '2000';
      StringGrid1.Cells[0, 10] := '2001';
      StringGrid1.Cells[0, 11] := '2002';
      StringGrid1.Cells[0, 12] := '2003';
      StringGrid1.Cells[0, 13] := '2004';
      StringGrid1.Cells[0, 14] := '2005';
      StringGrid1.Cells[0, 15] := '2006';  Edit1.Tag:= 2000;
      Edit2.Tag:= 2001;
      Edit3.Tag:= 2002;
      Edit4.Tag:= 2003;
      Edit5.Tag:= 2004;
      for index:=1 to StringGrid1.RowCount do
      begin
        NewItem:=TMenuItem.Create(PopupMenu1);
        NewSubitem:=TMenuItem.Create(PopupMenu1);
        PopupMenu1.Items.Add(NewItem);
        Newitem.Name:='MI'+inttostr(index);
    //    NewItem.add(NewSubitem);
        NewItem.Caption:=StringGrid1.Cells[0,index];
    //    NewSubitem.Caption:='subsub';
        if trim(StringGrid1.Cells[0, index]) = '' then StringGrid1.Cells[0, index]:='0';
        NewItem.Tag:=StrToInt(StringGrid1.Cells[0,index]);
      end;  for i:= 0 to self.PopupMenu1.Items.Count-1 do
      begin
        self.PopupMenu1.Items[i].OnClick:=menuItemClick;
      end;
    end;
    procedure TForm1.menuItemClick(Sender: TObject);
    begin
      TEdit(self.PopupMenu1.PopupComponent).Text:= TMenuItem(sender).Caption;
      StringGrid1.Cells[1, TEdit(self.PopupMenu1.PopupComponent).Tag]:=IntToStr(TMenuItem(sender).Tag);
    end;
    end.
      

  5.   

    知道了,将PopupMenu1.AutoHotKeys:=maManual;就可以不显示&了。哎,要有静下心来慢慢研究啊。