線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2619
推到 Plurk!
推到 Facebook!

如何在動態產生的btn內的onclick寫程式

尚未結案
yx_huang77
一般會員


發表:46
回覆:64
積分:22
註冊:2004-03-19

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-04-21 11:02:25 IP:220.142.xxx.xxx 未訂閱
請問我如何在動態產生的btn內的onclick寫上open form的程式碼 謝謝
change.jian
版主


發表:29
回覆:620
積分:439
註冊:2003-06-02

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-04-21 11:13:37 IP:61.222.xxx.xxx 未訂閱
您好,如下:
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;    type
  TForm1 = class(TForm)
    Label1: TLabel;
    procedure Label1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    FButton:TButton;
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.dfm}    procedure TForm1.Label1Click(Sender: TObject);
begin
  if not Assigned(FButton) then
  begin
    FButton:=TButton.Create(Self);
    FButton.Parent:=Self;
    FButton.Top:=10;
    FButton.Left:=10;
    FButton.Caption:='按我產生另一個form';
    FButton.OnClick:=Button1Click;
  end;
end;    procedure TForm1.Button1Click(Sender: TObject);
var
  aForm:TForm1;
begin
  aForm:=TForm1.Create(Self);
  try
    aForm.Caption:='這是產生的另外一個from';
    aForm.Label1.Caption:='這是產生的另外一個form';
    aForm.Position:=poScreenCenter;
    aForm.ShowModal;
  finally
    aForm.Free;
  end;
end;    end.
畫面檔
object Form1: TForm1
  Left = 192
  Top = 107
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  Position = poDefault
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 120
    Top = 64
    Width = 102
    Height = 13
    Caption = '按我動態產生button'
    OnClick = Label1Click
  end
end
yx_huang77
一般會員


發表:46
回覆:64
積分:22
註冊:2004-03-19

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-04-21 13:23:58 IP:220.142.xxx.xxx 未訂閱
btn是動態產生name也是動態產生不固定,所以btnclick產生時,所要執行的內容也要跟著一樣,但是change.jian大大提供的是固定的,可有其他方法
change.jian
版主


發表:29
回覆:620
積分:439
註冊:2003-06-02

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-04-21 13:55:56 IP:61.222.xxx.xxx 未訂閱
引言: btn是動態產生name也是動態產生不固定,所以btnclick產生時,所要執行的內容也要跟著一樣,但是change.jian大大提供的是固定的,可有其他方法
??你是指什麼地方是固定的,可以再詳細說明嗎?
yx_huang77
一般會員


發表:46
回覆:64
積分:22
註冊:2004-03-19

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-04-21 14:06:02 IP:220.142.xxx.xxx 未訂閱
while (j
TATSU
版主


發表:50
回覆:135
積分:62
註冊:2003-01-16

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-04-21 14:28:29 IP:219.77.xxx.xxx 未訂閱
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;    type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure MyBtnClick(Sender : TObject) ;
  private
    { Private declarations }
    FMyBtnList : TList ;
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.dfm}    procedure TForm1.Button1Click(Sender: TObject);
var
  i : Integer ;
  NewBtn : TButton ;
begin
  for i := 1 to 10 do
  begin
    NewBtn := TButton.Create(self);
    with NewBtn do
    begin
      Left := 10 ;
      Top := 26 * i ;
      Height := 22 ;
      Width := 40 ;
      Tag := i ;
      Caption := 'No.'   IntToStr(i) ;
      Parent := Self ;
      OnClick := MyBtnClick ;
    end;
    FMyBtnList.Add(NewBtn) ;
  end;
end;    procedure TForm1.MyBtnClick(Sender : TObject);
var
  nIndex : Integer ;
begin
  nIndex := (Sender as TButton).Tag ;
  Label1.Caption := 'Button no.'   IntToStr(nIndex)   ' clicked' ;
end;    procedure TForm1.FormCreate(Sender: TObject);
begin
  FMyBtnList := TList.Create ;
end;    procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  FMyBtnList.Free ;
end;    end.    
這個簡單的示範沒有用什麼元件,你只要加入一個新的 button 就可以測試,當中除了新增十個 button 外,除將新的 Button 加入 TList 入,以備你還要參考十個 button 內某些資料。
Miles
尊榮會員


發表:27
回覆:662
積分:622
註冊:2002-07-12

發送簡訊給我
#7 引用回覆 回覆 發表時間:2004-04-21 14:46:38 IP:218.160.xxx.xxx 未訂閱
Hi yx_huang77 您好: 試試這個
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Comobj, Excel2000, StdCtrls;    type
  TForm1 = class(TForm)
    Button2: TButton;
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
    MyOnClick : array [0..2] of TNotifyEvent;
    procedure a(Sender : TObject);
    procedure b(Sender : TObject);
    procedure c(Sender : TObject);
  public
    { Public declarations }
  end;        var
  Form1: TForm1;    implementation    {$R *.dfm}    procedure TForm1.a(Sender: TObject);
begin
   ShowMessage('1st Click');
end;    procedure TForm1.b(Sender: TObject);
begin
   ShowMessage('2st Click');
end;    procedure TForm1.c(Sender: TObject);
begin
   ShowMessage('3st Click');
end;    procedure TForm1.Button2Click(Sender: TObject);
var MyButton : TButton;
begin
   MyOnClick[0] := a;
   MyOnClick[1] := b;
   MyOnClick[2] := c;
   MyButton := TButton.Create(Self);
   MyButton.Parent := Self;
   MyButton.Caption := 'Now Button';
   MyButton.Left := 10;
   MyButton.Top  := 10;
   MyButton.OnClick := MyOnClick[2];
end;    end.
我不是高手, 高手是正在銀幕前微笑的人. 發表人 - miles 於 2004/04/21 14:59:40
------


我不是高手, 高手是正在銀幕前微笑的人.
change.jian
版主


發表:29
回覆:620
積分:439
註冊:2003-06-02

發送簡訊給我
#8 引用回覆 回覆 發表時間:2004-04-21 14:57:15 IP:61.222.xxx.xxx 未訂閱
引言: while (j 仍然不太懂你意思,如果btnclick的執行內容是固定的話,那就用同一個procedure就好,如果是不固定,但有跡可循的變動的話,那就要看情形了,你要不要把你動態產生的button的onclick執行內容放上來看一下,這樣應該比較快找到答案
change.jian
版主


發表:29
回覆:620
積分:439
註冊:2003-06-02

發送簡訊給我
#9 引用回覆 回覆 發表時間:2004-04-21 15:08:26 IP:61.222.xxx.xxx 未訂閱
不知道這是不是你要的
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;    type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    FNotify:array[0..2] of TNotifyEvent;
    procedure ButtonX0Click(Sender:TObject);
    procedure ButtonX1Click(Sender:TObject);
    procedure ButtonX2Click(Sender:TObject);
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.dfm}    procedure TForm1.Button1Click(Sender: TObject);
var
  i:Integer;
  btn:TButton;
begin
  for i:=0 to high(FNotify) do
  begin
    btn:=TButton.Create(Self);
    btn.Parent:=Self;
    btn.Top:=10 30*(i-0);
    btn.Left:=10;
    btn.Caption:='ButtonX' IntToStr(i 1);
    btn.OnClick:=FNotify[i];
  end;
end;    procedure TForm1.ButtonX0Click(Sender: TObject);
begin
  ShowMessage('這是第1個Button的OnClick事件');
end;    procedure TForm1.ButtonX1Click(Sender: TObject);
begin
  ShowMessage('這是第2個Button的OnClick事件');
end;    procedure TForm1.ButtonX2Click(Sender: TObject);
begin
  ShowMessage('這是第3個Button的OnClick事件');
end;    procedure TForm1.FormCreate(Sender: TObject);
begin
  FNotify[0]:=ButtonX0Click;
  FNotify[1]:=ButtonX1Click;
  FNotify[2]:=ButtonX2Click;
end;    end.
yx_huang77
一般會員


發表:46
回覆:64
積分:22
註冊:2004-03-19

發送簡訊給我
#10 引用回覆 回覆 發表時間:2004-04-22 08:37:59 IP:220.142.xxx.xxx 未訂閱
其實我想要的是動態產生的btn內的onclick事件是從資料庫抓取,還有就是btn的產生數量也是由資料庫抓取,如何動態產生onclick事件事件 謝謝
TATSU
版主


發表:50
回覆:135
積分:62
註冊:2003-01-16

發送簡訊給我
#11 引用回覆 回覆 發表時間:2004-04-22 09:12:38 IP:219.77.xxx.xxx 未訂閱
引言: 其實我想要的是動態產生的btn內的onclick事件是從資料庫抓取,還有就是btn的產生數量也是由資料庫抓取,如何動態產生onclick事件事件 謝謝
你先看看是否那多個欄位所對應的事件都是要處理什麼程序,其實程式已 令每個 button 有不同的資料去讓你識別,再下來,就是要你自己發揮如何 處理及運用。
procedure TForm1.Button1Click(Sender: TObject);
var
  i : Integer ;
  NewBtn : TButton ;
begin
  for i := 1 to 10 do
  begin
    NewBtn := TButton.Create(self);
    with NewBtn do
    begin
      Left := 10 ;
      Top := 26 * i ;
      Height := 22 ;
      Width := 40 ;
      Tag := i ; // 每一個按鈕的 Tag 及 caption 都不相同
      Caption := 'No.'   IntToStr(i) ;
      Parent := Self ;
      OnClick := MyBtnClick ;
    end;
    FMyBtnList.Add(NewBtn) ;
  end;
end;    procedure TForm1.MyBtnClick(Sender : TObject);
var
  nIndex : Integer ;
begin
  nIndex := (Sender as TButton).Tag ;
  Label1.Caption := 'Button no.'   IntToStr(nIndex)   ' clicked' ;     //因應不同的欄位已經可以放入不同的程式碼 
  case nIndex of 
  0 :  {your code}
  1 :  {your code}
  2 :  {your code}
 .
 .
 .
 .
 .
 .
 100 : {your code}
  end;
end;
如果這樣的做法,你仍覺得不妥善,請你將你數據庫資料簡單列出,好讓你的問題盡快解決。
yx_huang77
一般會員


發表:46
回覆:64
積分:22
註冊:2004-03-19

發送簡訊給我
#12 引用回覆 回覆 發表時間:2004-04-22 09:48:27 IP:220.142.xxx.xxx 未訂閱
數據庫資料如下 btncaption Event 人事資料維護 openform(fma010) 廠商資料維護 openform(fma020) 採購資料維護 openform(fma030) 入庫資料維護 openform(fma040) 客戶資料維護 openform(fma050) 假設以上資料為數據庫資料,系統會依照使用者登入權限來判斷可使用的作業 例如 A使用者只能用 人事資料維護 openform(fma010) 廠商資料維護 openform(fma020) 採購資料維護 openform(fma030) B使用者只能用 廠商資料維護 openform(fma020) 採購資料維護 openform(fma030) 客戶資料維護 openform(fma050) 其他維護則不顯示
TATSU
版主


發表:50
回覆:135
積分:62
註冊:2003-01-16

發送簡訊給我
#13 引用回覆 回覆 發表時間:2004-04-22 13:41:36 IP:219.77.xxx.xxx 未訂閱
引言: 數據庫資料如下 btncaption Event 人事資料維護 openform(fma010) 廠商資料維護 openform(fma020) 採購資料維護 openform(fma030) 入庫資料維護 openform(fma040) 客戶資料維護 openform(fma050) 假設以上資料為數據庫資料,系統會依照使用者登入權限來判斷可使用的作業 例如 A使用者只能用 人事資料維護 openform(fma010) 廠商資料維護 openform(fma020) 採購資料維護 openform(fma030) B使用者只能用 廠商資料維護 openform(fma020) 採購資料維護 openform(fma030) 客戶資料維護 openform(fma050) 其他維護則不顯示
 
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
const      BTN_MAX_COUNT   = 5 ;      BTN_ID_HR       = 1 ;
  BTN_ID_SUPPLIER = 2 ;
  BTN_ID_PURCHASE = 3 ;
  BTN_ID_STOCK    = 4 ;
  BTN_ID_CLIENT   = 5 ;
 
  aBtnCaption : Array[1..5] of String = ('人事資料維護',
                                         '廠商資料維護',
                                         '採購資料維護',
                                         '入庫資料維護',
                                         '客戶資料維護') ;
type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    procedure FormShow(Sender: TObject);
    procedure MyBtnClick(Sender : TObject) ;
  private
    { Private declarations }
    FUserID    : Integer ;
    function GetUserPassword : Integer ;
    function CheckUserRights(UserID : Integer) : Boolean ;
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.dfm}    procedure TForm1.FormShow(Sender: TObject);
var
  i, j : Integer ;
  NewBtn : TButton ;
begin
  FUserID := GetUserPassword ;
  if FUserID = -1 then 
    close 
  else
  begin
    j := 0 ;
    for i := 1 TO BTN_MAX_COUNT do
    begin
      if CheckUserRights(FUserID, i) then
      begin
        inc(j) ;
        NewBtn := TButton.Create ;
        with NewBtn do
        begin
          Left := 10 ;
          Top := 26 * j ;
          Height := 22 ;
          Width := 40 ;
          Tag := i ;
          Caption := aBtnCaption[i] ;
          Parent := Self ;
          OnClick := MyBtnClick ;
        end;
      end;
    end;
  end;
end;    function TForm1.GetUserPassword : Integer ;
begin
  {put your password check routine here}
end;    function TForm1.CheckUserRights(UserID : Integer) : Boolean ;
begin
  {put user rights routine here} 
end;    procedure TForm1.MyBtnClick(Sender : TObject);
var
  nBtnID : Integer ;
begin
  nBtnID := (Sender as TButton).Tag ;
  case nBtnID of 
  BTN_ID_HR       : {人事資料維護}
  BTN_ID_SUPPLIER : {廠商資料維護}
  BTN_ID_PURCHASE : {採購資料維護}
  BTN_ID_STOCK    : {入庫資料維護}
  BTN_ID_CLIENT   : {客戶資料維護}
  end;
end;    end.
你嘗試在程式開首部份定義一堆 constant ,這樣做,除了可讀性增加,以後維護這支程式時,亦會簡單得多。
yachanga
資深會員


發表:24
回覆:335
積分:296
註冊:2003-09-27

發送簡訊給我
#14 引用回覆 回覆 發表時間:2004-04-22 14:45:00 IP:210.68.xxx.xxx 未訂閱
Hi yx_huang77 您好: 小弟拙作跟您的概念很像.. 您想利用動態按鈕實現資料庫權限設計 而小弟是利用 動態表單來實現資料庫權限設計 不同的表單按下去的功能皆放在資料庫    您可以將表單部份改成按鈕設計即可, 給您參考 希望對您有幫助 >~悠遊法國號~
yx_huang77
一般會員


發表:46
回覆:64
積分:22
註冊:2004-03-19

發送簡訊給我
#15 引用回覆 回覆 發表時間:2004-04-23 10:47:34 IP:220.142.xxx.xxx 未訂閱
Tag := i ; 好像無法將Tag值置換過去 nBtnID := (Sender as TButton).Tag ; 好像抓不到值???
TATSU
版主


發表:50
回覆:135
積分:62
註冊:2003-01-16

發送簡訊給我
#16 引用回覆 回覆 發表時間:2004-04-23 11:47:49 IP:219.77.xxx.xxx 未訂閱
引言: Tag := i ; 好像無法將Tag值置換過去 nBtnID := (Sender as TButton).Tag ; 好像抓不到值???
你將你的程式碼放上來,讓我看看。
yx_huang77
一般會員


發表:46
回覆:64
積分:22
註冊:2004-03-19

發送簡訊給我
#17 引用回覆 回覆 發表時間:2004-04-23 11:58:16 IP:220.142.xxx.xxx 未訂閱
procedure TfmMain.MyBtnClick(Sender: TObject); var   sFrom, sCommand, sSysName : String;   nBtnID : Integer ; begin   nBtnID := (Sender as TButton).Tag ;   sSysName := Pchar(MyExeArray[nBtnID]);   sFrom := 'G:\Winprg\';    sCommand := sFrom +  sSysName + '\' + sSysName + 'exe';   if WinExec(Pchar(sCommand), SW_MAXIMIZE)<31 then begin MyWarning('找不到該程式、請聯絡資訊人員!'); abort; end else Application.Terminate; end;
TATSU
版主


發表:50
回覆:135
積分:62
註冊:2003-01-16

發送簡訊給我
#18 引用回覆 回覆 發表時間:2004-04-23 12:23:32 IP:219.77.xxx.xxx 未訂閱
引言:
 
procedure TfmMain.MyBtnClick(Sender: TObject);
var
  sFrom, sCommand, sSysName : String;
  nBtnID : Integer ;
begin
  nBtnID    := (Sender as TButton).Tag ; // 你有檢查這個值是否正確? 
  sFrom     := 'G:\Winprg\' ;
  sSysName  := MyExeArray[nBtnID]; // 這裏應該不用轉為 PChar,你也好列出這個 Array 讓我參考
  sCommand  := sFrom    sSysName   '\'   sSysName   'exe';
  if FileExists(sCommand) then
  begin
    if WinExec(Pchar(sCommand), SW_MAXIMIZE) < 32 then
    begin
      MyWarning('程式執行錯誤、請聯絡資訊人員!');
      abort;
    end
    else
      Application.Terminate;
  end
  else
    MyWarning('找不到該程式、請聯絡資訊人員!');
end;
yx_huang77
一般會員


發表:46
回覆:64
積分:22
註冊:2004-03-19

發送簡訊給我
#19 引用回覆 回覆 發表時間:2004-04-23 13:40:33 IP:220.142.xxx.xxx 未訂閱
 
MyExeArray: array of String;
  sCount := DM.qyTemp0.RecordCount ;
  setlength(MyExeArray,sCount);          i:=0;
      j:=0;
      k:=0;          while not DM.qyTemp0.Eof do
      begin
        while (i<4) and (not DM.qyTemp0.Eof) do
        begin
          while (j    發表人 - tatsu 於 2004/04/23  14:14:13
        
TATSU
版主


發表:50
回覆:135
積分:62
註冊:2003-01-16

發送簡訊給我
#20 引用回覆 回覆 發表時間:2004-04-23 14:19:22 IP:219.77.xxx.xxx 未訂閱
你如果要貼上程式碼,請按上方那個 # 按鈕,產生了 〔code〕〔/code〕 後才把你的程式碼貼到這兩個代碼的範圍內。 一:產生新的 Button 時,程式執行到放入 Tag 值時,有沒有發生問題? 二:執行這一行時,有沒有發生錯誤? nBtnID := (Sender as TButton).Tag ; // 你有檢查這個值是否正確? 三:如果沒有錯誤,那麼這個 nBtnID 的值是什麼?
yx_huang77
一般會員


發表:46
回覆:64
積分:22
註冊:2004-03-19

發送簡訊給我
#21 引用回覆 回覆 發表時間:2004-04-23 14:37:19 IP:220.142.xxx.xxx 未訂閱
   
nBtnID := (Sender as TButton).Tag ; <-----------1
sSysName := MyExeArray[nBtnID];<----------------2
執行到1時沒錯誤產生,從watch看,得到下面的東東 nBtnID: Variable 'nBtnID' inaccessible here due to optimization 執行到2時錯誤產生
TATSU
版主


發表:50
回覆:135
積分:62
註冊:2003-01-16

發送簡訊給我
#22 引用回覆 回覆 發表時間:2004-04-23 15:05:27 IP:219.77.xxx.xxx 未訂閱
 
  MyExeArray: array of String;
  sCount := DM.qyTemp0.RecordCount ;
  setlength(MyExeArray,sCount);      i := 0;
  j := 0;
  k := 0;
  nRow := 5 ;
  while not DM.qyTemp0.Eof do
  begin
    MyExeArray[k] := DM.qyTemp0.FieldByName('SYS_NO').AsString;
    NewBtn :=  Tbutton.Create(self) ;
    with NewBtn do
    begin
      parent    := Self; 
      top       := j * 30   ((j 1) * 12);
      left      := i*150 (i 1)*16; 
      height    := 30;
      width     := 150;
      name      := 'btn' inttostr(k);
      Tag       := k ;   <-------------------這裡的值無法給 Tag
      OnClick       := MyBtnClick ;
      Font.Size     := 12;
    end;
    Inc(k) ;
    Inc(j) ;
    if j = nRow then
    begin
      Inc(i) ;
      j := 0 ;
    end;
    DM.qyTemp0.Next;
  end;
你這一段程式碼有問題
            k := k 1;
            with tbutton.Create(self) do
            begin
              parent := Self; 
              top := j*30 (j 1)*12;
              left := i*150 (i 1)*16; 
              height := 30;
              width := 150;
              name := 'btn' inttostr(k);
              Tag := k ;   <-------------------這裡的值無法給 Tag
              OnClick := MyBtnClick ;
              Font.Size := 12;
              MyExeArray[k-1] := DM.qyTemp0.FieldByName('SYS_NO').AsString;
            end;
            j := j 1;
那麼 Tag 的值是由 1 開始 但你又懂得先減 1 ,再存放 string 入 Array。 如果你的 Array 有五個 string ,當你按第五個 Button 時,傳回 Tag 的值是 5。但你的 Array 是由 0 開始到 4 ,總數五個,你去找 MyExeArray[5] ,不出錯才怪耶。你試著將程式改到以上的格式,你較易找出錯誤的地方。 發表人 - tatsu 於 2004/04/23 15:14:39
yx_huang77
一般會員


發表:46
回覆:64
積分:22
註冊:2004-03-19

發送簡訊給我
#23 引用回覆 回覆 發表時間:2004-04-23 15:28:02 IP:220.142.xxx.xxx 未訂閱
問題解決了 TATSU 3摳乎你美麻幾
系統時間:2024-04-28 14:15:47
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!