全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1493
推到 Plurk!
推到 Facebook!

如何控制動態產生的PopupMenu Item

答題得分者是:anpino
GGL
資深會員


發表:104
回覆:600
積分:335
註冊:2006-11-05

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-07-02 01:54:41 IP:211.76.xxx.xxx 未訂閱
    TStrings *List=new TStringList;
    ADOConnection1->GetTableNames(List);        for(int i=0;iText.ToInt();i  )
    {
       TMenuItem *m=new TMenuItem(this);
       m->Caption=List->Strings[i];
       N9->Add(m);
    }
為了替我的程式動態產生PopupMenu裡面的一個item弄了很久,想不到還有個更大的難題... 請問一下,這樣動態產生出來的Item要如何控制他的Click()事件?我查了一下文章,但是查到的是delphi的,看不怎麼懂,而且好像也有點不一樣,請知道的人幫幫我吧謝謝了
anpino
版主


發表:31
回覆:477
積分:231
註冊:2003-01-02

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-07-02 09:20:33 IP:211.23.xxx.xxx 未訂閱
引言:
    TStrings *List=new TStringList;
    ADOConnection1->GetTableNames(List);        for(int i=0;iText.ToInt();i  )
    {
       TMenuItem *m=new TMenuItem(this);
       m->Caption=List->Strings[i];
       N9->Add(m);
    }
為了替我的程式動態產生PopupMenu裡面的一個item弄了很久,想不到還有個更大的難題... 請問一下,這樣動態產生出來的Item要如何控制他的Click()事件?我查了一下文章,但是查到的是delphi的,看不怎麼懂,而且好像也有點不一樣,請知道的人幫幫我吧謝謝了 < face="Verdana, Arial, Helvetica"> GGL 您好: bcb 的 help 裡都有範例呀~~ 貼出如下:
 
void __fastcall TForm1::AddButtonClick(TObject *Sender)
{
    const int num_items = 4;
    for (int index = 0; index < num_items;   index)
    {
        TMenuItem *NewItem = new TMenuItem(PopupMenu1); // create the new item
        PopupMenu1->Items->Add(NewItem);// add it to the Popupmenu
        NewItem->Caption = "Menu Item "   IntToStr(index);
        NewItem->Tag = index;
        NewItem->OnClick = PopupMenuItemsClick;// assign it an event handler        }
}    void __fastcall TForm1::PopupMenuItemsClick(TObject *Sender)
{
    TMenuItem *ClickedItem = dynamic_cast(Sender);
    if (ClickedItem)
    {
        switch (ClickedItem->Tag)
        {
            case 0:
            {
                ShowMessage("first item clicked");
                break;
            }
            case 1:
            {
                ShowMessage("second item clicked");                    break;
            }
            // etc...
        }
    }
}    To edit or destroy an item, grab its pointer via the TMenuItem::Items property.    void __fastcall TForm1::EditButtonClick(TObject *Sender)
{
    const int index = 1;
    TMenuItem *ItemToEdit = PopupMenu->Items->Items[index];
    ItemToEdit->Caption = "Changed Caption";
}    void __fastcall TForm1::DestroyButtonClick(TObject *Sender)
{
    const int index = 2;
    TMenuItem *ItemToDelete = PopupMenu->Items->Items[index];
    delete ItemToDelete;
}
------------------------------- 數學系是內功很強(邏輯/分析) 資工系是招式很多(程式技巧) 就像令狐沖VS東方不敗:D Anpinos Middle Earth http://anpino.hp.infoseek.co.jp/ -------------------------------
axsoft
版主


發表:681
回覆:1056
積分:969
註冊:2002-03-13

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-07-02 10:21:51 IP:61.218.xxx.xxx 未訂閱
GGL您好:    我補充一個MenuItem的作法:D 請參考下面的範例試試,跟你要的PopupMenuItem很類似:
如何動態產生MenuItem    Unit1.cpp    //---------------------------------------------------------------------------
#include 
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{ 
  TMainMenu *MainMenu1=new TMainMenu(this);
  TMenuItem *HelpItem = new TMenuItem(this);
  TMenuItem *Root = MainMenu1->Items;
  TMenuItem *EditItem = new TMenuItem(this);
  
  HelpItem->Caption = "&File";
  EditItem->Caption = "&Edit";
  Root->Add(HelpItem);
  Root->Insert(1,EditItem);
  
  TMenuItem *ContentsItem = new TMenuItem(this);
  TMenuItem *IndexItem = new TMenuItem(this);
  TMenuItem *AboutItem = new TMenuItem(this);
  TMenuItem *CopyItem = new TMenuItem(this);
  
  ContentsItem ->Caption = "&Contents Item";
  IndexItem ->Caption = "&Index Item";
  AboutItem ->Caption = "&About Item";
  CopyItem ->Caption = "&Copy";
  
  HelpItem->Add(ContentsItem);
  HelpItem->Add(IndexItem);
  HelpItem->Add(AboutItem);
  EditItem->Add(CopyItem);
} 
 
 
Unit1.h
 
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include 
#include 
#include 
#include <Forms.hpp>
#include 
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
        void __fastcall FormCreate(TObject *Sender);
private: // User declarations
public:  // User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
/*生活是一種藝術,用心生活享受生活*/
發表人 - axsoft 於 2004/07/02 10:28:33
系統時間:2024-05-14 14:48:54
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!