将C#事件实现转换为VB.NET

时间:2012-04-11 11:08:47

标签: asp.net .net vb.net gridview c#-to-vb.net

我正在尝试使用一些扩展了VB.NET项目中GridView功能的C#代码。我正在使用的代码来自here

在C#代码中,有一个GroupHeader的事件定义:

/// <summary>
/// Event triggered after a row for group header be inserted
/// </summary>
public event GroupEvent GroupHeader;

在上述网站上通过示例进行了扩展:

protected void Page_Load(object sender, EventArgs e)
{
    GridViewHelper helper = new GridViewHelper(this.GridView1);
    helper.RegisterGroup("ShipRegion", true, true);
    helper.RegisterGroup("ShipName", true, true);
    helper.GroupHeader += new GroupEvent(helper_GroupHeader);
    helper.ApplyGroupSort();
}

private void helper_GroupHeader(string groupName, object[] values, GridViewRow row)
{
    if (groupName == "ShipRegion")
    {
        row.BackColor = Color.LightGray;
        row.Cells[0].Text = "&nbsp;&nbsp;" + row.Cells[0].Text;
    }
    else if (groupName == "ShipName")
    {
        row.BackColor = Color.FromArgb(236, 236, 236);
        row.Cells[0].Text = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + row.Cells[0].Text;
    }
}

我的问题是,如何将此代码转换为VB.NET?

我已经转换了事件实现,如下所示:

Private Sub helper_GroupHeader(ByVal groupName As String, ByVal values As Object(), ByVal row As GridViewRow)
    Try
        If groupName = "ITEM#" Then
            row.BackColor = Color.LightBlue
            row.Cells(0).Text = "&nbsp;&nbsp;" & row.Cells(0).Text
        End If
    Catch ex As Exception

    End Try
End Sub

如何用VB.NET调用(引发?)此事件?

2 个答案:

答案 0 :(得分:4)

您正在寻找AddHandler

Protected Sub Page_Load(sender As Object, e As EventArgs)
    Dim helper As New GridViewHelper(Me.GridView1)
    helper.RegisterGroup("ShipRegion", True, True)
    helper.RegisterGroup("ShipName", True, True)
    AddHandler helper.GroupHeader, AddressOf helper_GroupHeader
    helper.ApplyGroupSort()
End Sub

我认为该事件是从GridViewHelper隐式提出的。

答案 1 :(得分:1)

您可以使用AddHandler订阅该活动:

AddHandler GroupHeader, AddressOf helper_GroupHeader

使用RaiseEvent提升活动:

RaiseEvent GroupHeader()