Button.performclick()

来源:互联网 发布:刺客信条枭雄画面优化 编辑:程序博客网 时间:2024/06/18 10:15

 (1)WinForm中,Button按钮有PerformClick()方法,可以模拟用户单击鼠标. 即button.PerformClick()生成按钮的事件。相关网站为:http://msdn2.microsoft.com/zh-cn/library/system.windows.forms.button.performclick(VS.80).aspx

(2)在一些事件处理程序中,比如Form窗体的Click事件 Form_Click(parameter1,parameter2 )、Button按钮的Click事件Button_Click()等等,可以直接调用相应的按钮代码在其相应的事件处理程序中执行。先看下面两例:

事例1:Visual Basic

Private Sub button1_Click(sender As Object, e As EventArgs)
    ' If myVar is an even number, click Button2.
    If myVar Mod 2 = 0 Then
        button2.PerformClick()
        ' Display the status of Button2's Click event.
        MessageBox.Show("button2 was clicked ")
    Else
        ' Display the status of Button2's Click event.
        MessageBox.Show("button2 was NOT clicked")
    End If
    ' Increment myVar.  
    myVar = myVar + 1
End Sub 'button1_Click

事例2:
       Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim cust As Customer
        cust.Company = "Bottom-Dollar Markets"
        cust.Manager = "Elizabeth Lincoln"
        cust.Address = "23 Tsawassen Blvd."
        cust.City = "Tsawassen"
        cust.Country = "Canada"
        cust.CustomerSince = #10/20/1996#
        cust.Balance = 33500
        Customers(0) = cust

        cust = New Customer()
        cust.Company = "Drachenblut Delikatessen"
        cust.Manager = "Sven Ottlieb"
        cust.Address = "Walserweg 21"
        cust.City = "Aachen"
        cust.Country = "Germany"
        cust.CustomerSince = #1/2/1994#
        cust.Balance = 2400
        Customers(1) = cust

        cust = New Customer()
        cust.Company = "Furia Bacalhau e Frutos do Mar"
        cust.Manager = "Lino Rodriguez "
        cust.Address = "Jardim das rosas n. 32"
        cust.City = "Lisboa"
        cust.Country = "Portugal"
        cust.CustomerSince = #12/22/1998#
        cust.Balance = 300
        Customers(2) = cust

        cust = New Customer()
        cust.Company = "Great Lakes Food Market"
        cust.Manager = "Howard Snyder"
        cust.Address = "2732 Baker Blvd."
        cust.City = "Eugene, OR"
        cust.Country = "USA"
        cust.CustomerSince = #1/3/1998#
        cust.Balance = 6500
        Customers(3) = cust

        cust = New Customer()
        cust.Company = "QUICK-Stop"
        cust.Manager = "Horst Kloss"
        cust.Address = "Taucherstra遝 10"
        cust.City = "Cunewalde"
        cust.Country = "Germany"
        cust.CustomerSince = #1/1/1989#
        cust.Balance = 23400
        Customers(4) = cust

        cust = New Customer()
        cust.Company = "The Cracker Box"
        cust.Manager = "Liu Wong"
        cust.Address = "55 Grizzly Peak Rd."
        cust.City = "Butte"
        cust.Country = "USA"
        cust.CustomerSince = #1/1/1999#
        cust.Balance = 23400
        Customers(5) = cust

        cust = New Customer()
        cust.Company = "White Clover Markets"
        cust.Manager = "Karl Jablonski"
        cust.Address = "305 - 14th Ave. S."
        cust.City = "Seattle, WA"
        cust.Country = "USA"
        cust.CustomerSince = #5/11/1994#
        cust.Balance = 12000
        Customers(6) = cust

        cust = New Customer()
        cust.Company = "Wilman Kala"
        cust.Manager = "Matti Karttunen"
        cust.Address = "Keskuskatu 45"
        cust.City = "Helsinki"
        cust.Country = "Finland"
        cust.CustomerSince = #1/3/2000#
        cust.Balance = 2500
        Customers(7) = cust

        bttnNext.PerformClick()       执行bttnNext_Click()的功能
    End Sub


     Private Sub bttnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnNext.Click
        If currentIndex = CountCustomers() Then currentIndex = 0   把当前索引的值与Customers数组里的元素数量相比,如果它们相等,就把currentIndex = 0  
        Dim aCustomer As Customer
        aCustomer = GetCustomer(currentIndex)
        ShowCustomer(currentIndex)
        currentIndex = currentIndex + 1
    End Sub                  

    Function GetCustomer(ByVal idx As Integer) As Customer
        Return (Customers(idx))
    End Function            自定义函数,此函数返回当前记录值

    Function CountCustomers() As Integer
        Return (Customers.Length)
    End Function            自定义函数,此函数返回Customers数组里的记录数量

原创粉丝点击