如何动态加载控件,加载好后又如何响应事件呢?
最好可以给出例子。
小弟我不胜感激。:)
问题点数:100、回复次数:5Top
1 楼whl9234(葫芦)回复于 2003-11-01 20:17:15 得分 20
如
private Table addt(string str,string cssc)
{
Table t=new Table();
t.Rows.Add(new TableRow());
t.Rows.Add(addtr(str,cssc));
t.Rows.Add(new TableRow());
t.Width=Unit.Parse("100%");
return t;
}
addtr是一个动态添加数据行的方法
动态添加事件,可以直接在动态生成控件的方法中加入:this.Button1.Click += new System.EventHandler(this.Button1_Click);(第二个this.Button1_Click是事件的名字)Top
2 楼visualbasic2000(齐谐)回复于 2003-11-01 20:18:54 得分 20
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(264, 72)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(136, 40)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.AutoScroll = True
Me.ClientSize = New System.Drawing.Size(424, 269)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "动态加载试验"
Me.ResumeLayout(False)
End Sub
#End Region
Dim Txt(10) As TextBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Int16
For i = 1 To 10
Txt(i) = New TextBox
Txt(i).Top = i * 100
Txt(i).Tag = i
Me.Controls.Add(Txt(i))
AddHandler Txt(i).TextChanged, AddressOf textchange
Next
End Sub
Sub textchange(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Text = "文本改变为:" & sender.Text & " 接受到第" & sender.Tag & "个动态加载的文本框发出的消息!"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Txt(4).BackColor = Color.Red
MsgBox("动态加载的指定文本框的颜色改变了!", MsgBoxStyle.OKOnly, "发送消息!")
End Sub
End Class
Made by e-Stack Room
http://www.mycnknow.comTop
3 楼menuvb(戏子,白日做梦)回复于 2003-11-02 09:26:20 得分 20
Private Sub btnAddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddButton.Click
' Increment the control count
m_ControlCount += 1
' Only allow 5 buttons, just to simplify drawing of the user interface
If m_ControlCount <= 5 Then
' Create a new Button
Dim x As New Button()
' Add properties to the form
x.Name = "btn" + m_ControlCount.ToString()
x.Text = "btn" + m_ControlCount.ToString()
x.Location = New Point(Me.m_Location.X + 250, Me.m_Location.Y)
m_Location.Y += x.Height + 5
' Add the two event handlers
AddHandler x.Click, AddressOf myButtonHandler_Click
end sub
Private Sub myButtonHandler_Click(ByVal sender As Object, ByVal e As EventArgs)
' Verify that the type of control triggering this event is indeed
' a Button. This is necessary since this handler can be attached
' to any event.
If TypeOf sender Is Button Then
' Let the user know what Button was pressed.
MsgBox(CType(sender, Button).Text + " was pressed!", _
MsgBoxStyle.OKOnly, Me.Text)
End If
End SubTop
4 楼NoReady(亦正亦偏)回复于 2003-11-02 11:30:11 得分 20
例:
在formload时
dim btn as new button
btn.text="ok"
me.controls.add(btn)
'这是另一个过程,不用放到formlaod中
addhandler btn.click,AddressOf(sayHello)
Private Sub sayHello(ByVal sender As Object, ByVal e As EventArgs)
msgbox("Hello! You have click me now")
End Sub
Top
5 楼itleon(良朋)回复于 2003-11-02 17:22:29 得分 20
Dim p As New PictureBox
With p
.Height =
.Width =
.Image =
.BorderStyle = BorderStyle.FixedSingle
.Location = New Point(x, y)
.SizeMode = PictureBoxSizeMode.StretchImage
.Cursor = Cursors.Hand
.Name =
End With
ImagePanel.Controls.Add(p)
AddHandler p.Click, AddressOf Double_ClickTop




