处理和捕获VB.net中的事件和委托 [英] handling and capturing Events and delegates in VB.net

查看:83
本文介绍了处理和捕获VB.net中的事件和委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个用户控件,我有一个下拉菜单。我想在我的父页面中处理此下拉列表的选定索引更改。我尝试了一些测试应用程序,我成功地使用事件和委托来捕获按钮的单击,但这是在C#中。

现在我需要做同样的事情,但在VB.net中加上控制是下拉而不是用户控件中的按钮。

我成功创建了我的事件和委托,但是,我无法看到处理程序与我的用户控件的intellisense中的下拉相关联我将用于捕捉事件。以下是我的用户控件的代码:

Hi Guys,

I have a user control in which I have a drop down. I want to handle the selected index change of this drop down in my parent page. I tried with some test application and I was succesfull to capture the click of a button using events and delegates, however that was in C#.
Now I need to do the same but in VB.net plus the control is drop down instead of button in user control.
I succeeded so far for creating my events and delegates however, I am not able to see the handler accociated with my user control's drop down in the intellisense which i will make use for capturing the event. Here is my code for my User Control :

Public Delegate Sub IndexChangedEventHandler(strValue As String)
    Public Event IndexChanged As EventHandler

    Protected Sub ddlTemp_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlTemp.SelectedIndexChanged
        Dim str As String
        str = ddlTemp.SelectedItem.Text 
        RaiseEvent IndexChanged(Me, New EventArgs())
    End Sub 





这是我的父页面代码



Here is my Parent page code behind

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        uc1.IndexChanged += New WebUserControl1.IndexChangedEventHandler(hndle_index)

    End Sub

    Sub hndle_index(ByVal str As String)
        Response.Write(str)
    End Sub





我收到这些错误,我不知道如何继续:

1.

.

I am getting these errors and I am not sure how to proceed:
1.

Error   1   'Public Event IndexChanged(strValue As String)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event. D:\Taresh\POCS\ucInvokingVB\ucInvokingVB\Default.aspx.vb    6   9   ucInvokingVB



2.


2.

Error   2   Delegate 'ucInvokingVB.WebUserControl1.IndexChangedEventHandler' requires an 'AddressOf' expression or lambda expression as the only argument to its constructor.   D:\Taresh\POCS\ucInvokingVB\ucInvokingVB\Default.aspx.vb    6   74  ucInvokingVB







请帮忙! Thanx很多!




Please Help ! Thanx a lot !

推荐答案

你可以尝试如下调用父页面中的下拉列表更改事件



这就是你的用户控制代码看起来的样子



can you try like below to invoke your dropdown changed event in parent page

this is how your user control code look likes

Public Delegate Sub IndexChangedEventHandler(strValue As String)
    Public Event IndexChanged As EventHandler

    Protected Sub ddlTemp_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlTemp.SelectedIndexChanged
        Dim str As String
        str = ddlTemp.SelectedItem.Text
//Changed Line of Code
        RaiseEvent IndexChanged()
    End Sub





并在你的父控件中尝试这行代码



protected sub HandleEvent()处理uc1.IndexChanged



//将您的代码放在这里



End Sub



and try this line of code in your parent control

protected sub HandleEvent() handles uc1.IndexChanged

//Put your code here

End Sub


这篇关于处理和捕获VB.net中的事件和委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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