如何举办活动 [英] How to raise an Event

查看:60
本文介绍了如何举办活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户控件,其中包含一个面板和一个按钮.
当我单击按钮时,我需要引发控件的click事件.

我执行了以下操作,但始终无法控制下一行

panel_Click(Byval Sender as object, byval e as system.event args)

我在usercontrol1.vb
上写了以下代码 ----------------------------------------

I have a user control contains a panel and a button on it.
When I click on the button I need to raise a click event for the control.

I did the following but control never going to the following line

panel_Click(Byval Sender as object, byval e as system.event args)

I wrote following code on usercontrol1.vb
----------------------------------------

public event panelClick(byval sender,byval e)

Public sub New()
  AddHandler button1.click, Addressof panel1_Click
end sub

private sub Button1_Click(Byval....)
  RaiseEvent  Panel1Click(sender,e)
ens sub



希望您可以解决此问题



Hope you can solve this

推荐答案

双击设计视图中的按钮,将为您创建事件.或在设计视图中选择按钮,然后在属性窗口中单击适当的事件;再次,该活动将为您创建.
Double-click on the button in design view and the event will be created for you. Or select the button in design view and click on the appropriate event in the properties window; again, the event will be created for you.


我目前正在学习如何举办活动,并记住了您的问题.这不完全是您所要求的,但是它具有类似的功能.它只是一个测试程序,因此非常简单,但是您可以根据需要进行更改.

我有一个带有面板(Panel1)的Windows窗体(Form1),并且在面板中放置了两个按钮(Button1和Button2).加载表单时,我将设置面板的背景色.然后,颜色会根据单击的按钮而变化.当然,可以直接在Button.Click事件中处理颜色更改,但是我们什么也学不到!

I''m currently learning about raising events and remembered your question. This isn''t exactly what you asked but it has similar functionality. It''s just a test program so is very simple but you could change it to suit your needs.

I have a Windows form (Form1) with a panel (Panel1) and two buttons (Button1 and Button2) placed in the panel. When the form loads I set the panel background colour. Then the colour changes depending on which button is clicked. Of course the colour changes could be handled directly in the Button.Click events, but then we wouldn''t learn anything!

Imports System.Drawing

Public Class Form1

    Public WithEvents mColour As tstChangeColour


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Panel1.BackColor = Color.Azure  ' Start off with this colour
        mColour = New tstChangeColour   ' Create new instance
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        mColour.selectColour(Color.DarkTurquoise)  ' Uses delegate to raise event
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        mColour.selectColour(Color.DarkSalmon)     ' Uses delegate to raise event
    End Sub

    Private Sub mColour_toggleColour(ByVal newCol As System.Drawing.Color) Handles mColour.toggleColour  ' Handles our event
        Me.Panel1.BackColor = newCol
    End Sub

End Class





Public Class tstChangeColour

    Public Event toggleColour(ByVal newColour As System.Drawing.Color)

    Public Sub selectColour(ByVal setColour As System.Drawing.Color)
        RaiseEvent toggleColour(setColour)
    End Sub

End Class



希望这对您有所帮助.



Hope this helps.


为什么不引发另一个事件,为什么不将功能放到一个通用方法中,并从两个click事件处理程序中调用该通用方法. >
有时候,最简单的方法就是-最简单的方法.
Instead of raising another event, why don''t you just put the functionality into a common method, and call that common method from both click event handlers.

Sometimes, the simplest approach is - well - the simplest approach.


这篇关于如何举办活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆