现在的位置: 首页 > 综合 > 正文

VS2005动态加载控件并给控件加事件

2013年07月20日 ⁄ 综合 ⁄ 共 3505字 ⁄ 字号 评论关闭

 

Public Class Form1

    
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
'加按钮
        Dim bt1 As New Button
        bt1.Text 
= "TestButton"
        bt1.Left 
= 100
        bt1.Top 
= 100
        bt1.Width 
= 100
        bt1.Height 
= 30
        
Me.Controls.Add(bt1)
        bt1.Show()
        
AddHandler bt1.Click, AddressOf ClickHandler'给按钮加事件,同样适用于其它控件
        'bt1.PerformClick() '自动点击按钮

        
'加工具栏
        Dim ts As New ToolStrip
        ts.Items.Add(
"Test03")
        ts.Items.Add(
"Test04"NothingNew EventHandler(AddressOf windowNewToolStrip_Click))
        ts.Dock 
= DockStyle.Top
        
Me.Controls.Add(ts)

        
'加菜单
        Dim ms As New MenuStrip()
        
Dim windowMenu As New ToolStripMenuItem("MainMenu-1")
        
Dim windowNewMenu As New ToolStripMenuItem("SubMenu-1.1"NothingNew EventHandler(AddressOf windowNewMenu_Click))
        windowMenu.DropDownItems.Add(windowNewMenu)
        
CType(windowMenu.DropDown, ToolStripDropDownMenu).ShowImageMargin = False
        
CType(windowMenu.DropDown, ToolStripDropDownMenu).ShowCheckMargin = True
        
' Assign the ToolStripMenuItem that displays 
        ' the list of child forms.
        ms.MdiWindowListItem = windowMenu
        
' Add the window ToolStripMenuItem to the MenuStrip.
        ms.Items.Add(windowMenu)
        
' Dock the MenuStrip to the top of the form.
        ms.Dock = DockStyle.Top
        
' The Form.MainMenuStrip property determines the merge target.
        Me.MainMenuStrip = ms
        
Me.Controls.Add(ms)

        
'加状态栏
        Dim sb As New StatusBar
        sb.Dock 
= DockStyle.Bottom
        
Dim panel1 As New StatusBarPanel
        
Dim panel2 As New StatusBarPanel
        
' Set the width of panel2 explicitly and set
        ' panel1 to fill in the remaining space.
        panel2.Width = 80
        panel1.AutoSize 
= StatusBarPanelAutoSize.Spring
        
' Set the text alignment within each panel.
        panel1.Alignment = HorizontalAlignment.Left
        panel2.Alignment 
= HorizontalAlignment.Right
        
' Display the first panel without a border and the second
        ' with a raised border.
        panel1.BorderStyle = StatusBarPanelBorderStyle.Sunken
        panel2.BorderStyle 
= StatusBarPanelBorderStyle.Raised
        
' Set the text of the panels. The panel1 object is reserved
        ' for line numbers, while panel2 is set to the current time.
        panel1.Text = "Reserved for important information."
        panel2.Text 
= System.DateTime.Now.ToShortTimeString
        
' Set a tooltip for panel2
        panel2.ToolTipText = "Click time to display seconds"
        
' Display panels in statusBar1 and add them to the
        ' status bar's StatusBarPanelCollection.
        sb.ShowPanels = True
        sb.Panels.Add(panel1)
        sb.Panels.Add(panel2)
        
Me.Controls.Add(sb)

    
End Sub

    
Private Sub ClickHandler(ByVal sender As ObjectByVal e As System.EventArgs)
        
MsgBox("Just a ButtonTest")
    
End Sub

    
Private Sub windowNewToolStrip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        
MsgBox("Just a ToolStripTest")
    
End Sub

    
Private Sub windowNewMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        
MsgBox("Just a MenuStripTest")
    
End Sub

    
Private Sub sb_PanelClick(ByVal sender As ObjectByVal e As StatusBarPanelClickEventArgs)
        
MsgBox("Just a StatusBar")
    
End Sub


End Class

抱歉!评论已关闭.