vb中动态添加控件的事件问题

vb中动态添加控件的事件问题,第1张

你应用控件数组

你可以先在窗体上加一个Command

然后把他的index

属性改为0

要添加10个按钮就这样写

Private

Sub

Form_Load()

For

i

=

1

To

10

Load

Command1(i)

Command1(i).Top

=

Command1(i

-

1).Top

+

Command1(0).Height

Command1(i).Visible

=

True

Next

事件如下

Private

Sub

Command1_Click(Index

As

Integer)

MsgBox

Index

End

Sub

MyButtonClick过程需要属于某个对象,才可以;即MyButtonClick需要定义在某个对象内部,且要符合Procedure (Sender:TObject) 规范;然后再赋值给MyButton1.OnClick。

例如:

================================================================

type

TForm1 = class(TForm)

Button1: TButton

procedure Button1Click(Sender: TObject)

private

{ Private declarations }

procedure Button1Click2(Sender: TObject)

public

{ Public declarations }

end

var

Form1: TForm1

implementation

{$R *.dfm}

procedure TForm1.Button1Click2(Sender: TObject)

begin

//

end

procedure TForm1.Button1Click(Sender: TObject)

begin

button1.on .OnClick:= Button1Click2

end

end.

'把图片的声明放在外面,类里面过程外面,而且要用As,不然等会儿没法用

Dim myPicture As New System.Windows.Forms.PictureBox()

'动态生成的控件,加一行

Private Sub UserControl1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

Me.Panel3.Controls.Add(myPicture)

myPicture.Size =New System.Drawing.Size(115, 160)

myPicture.TabStop =False

myPicture.Name ="p"

myPicture.Cursor = Cursors.Hand

AddHandler myPicture.Click, AddressOf mypic_Click '添加事件

AddHandler returnid, AddressOf idReturned '绑定事件和过程

End Sub

'再来个事件,放在声明的地方,就是类里面、过程外面

Private Event returnid(ByVal id As String) '这个事件可以传递值哦~

'普通的单击事件

Private Sub mypic_Click()

RaiseEvent returnid(myPicture.Name)

End Sub

'会传值的事件过程

Private Sub idReturned(ByVal id As String)

Dim bookInfo As New BookInfo

bookInfo.Show()

Me.Parent.Enabled = False

'用id变量做点什么吧

End Sub

如果要改事件的签名(参数一类的)的话,得重写控件。你可以这样自己弄一个事件。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11521957.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存