2012-03-19 82 views
0

我遇到一個問題,我在網上找到的vb.net代碼轉換爲C#。試圖將VB.Net轉換爲C#

vb.net

Protected Overridable Sub OnRowClicked(ByVal e As GridViewRowClickedEventArgs) 
    'raise the RowClicked event 
    RaiseEvent RowClicked(Me, e) 
End Sub 

'setup our EventHandler for RowClicked 
Public Custom Event RowClicked As EventHandler(Of GridViewRowClickedEventArgs) 
    AddHandler(ByVal value As EventHandler(Of GridViewRowClickedEventArgs)) 
     Events.AddHandler(RowClickedEventKey, value) 
    End AddHandler 

    RemoveHandler(ByVal value As EventHandler(Of GridViewRowClickedEventArgs)) 
     Events.RemoveHandler(RowClickedEventKey, value) 
    End RemoveHandler 

    RaiseEvent(ByVal sender As Object, ByVal e As GridViewRowClickedEventArgs) 
     Dim ev As EventHandler(Of GridViewRowClickedEventArgs) = TryCast(Events(RowClickedEventKey), EventHandler(Of GridViewRowClickedEventArgs)) 
     If Not IsNothing(ev) Then 
      ev(sender, e) 
     End If 
    End RaiseEvent 
End Event 

C#

protected virtual void OnRowClicked(GridViewRowClickedEventArgs e) 
    { 
     //raise the RowClicked event 
     if (RowClicked != null) 
     { 
      RowClicked(this, e); 
     } 
    } 

    //setup our EventHandler for RowClicked 
    public event EventHandler<GridViewRowClickedEventArgs> RowClicked 
    { 
     add { Events.AddHandler(RowClickedEventKey, value); } 

     remove { Events.RemoveHandler(RowClickedEventKey, value); } 

    } 

我曾經在網絡上的代碼轉換器。問題是在線

//raise the RowClicked event 
    if (RowClicked != null) 
    { 
     RowClicked(this, e); 
    } 

我得到的錯誤是:「RowClickGridView.RowClicked」事件只能出現在的+ =或左邊 - =

任何想法?

回答

0

只是刪除添加/刪除 - 部分:

public event EventHandler<GridViewRowClickedEventArgs> RowClicked; 
+0

不遵守?但我需要那個事件處理程序。 – MindGame 2012-03-19 16:47:16

0

我沒有看到特別參選這裏。我只是使用標準的事件結構,並忘記這個自定義的東西。

+0

我該怎麼做? – MindGame 2012-03-19 16:50:22

+0

C#中的標準事件處理程序接線語法爲Object.Event + = EventHandlerFunction; – 2012-03-19 20:38:35

+0

http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx – 2012-03-19 20:38:59

0
protected virtual void OnRowClicked(GridViewRowClickedEventArgs e) 
{ 
    EventHandler<GridViewRowClickedEventArgs> temp = RowClicked ; 
    //raise the RowClicked event 
    if (temp != null) 
    { 
     temp(this, e); 
    } 
} 


public event EventHandler<GridViewRowClickedEventArgs> RowClicked; 

CF http://msdn.microsoft.com/en-us/library/db0etb8x.aspx

+0

I只是嘗試了這一點,我得到了同樣的錯誤消息在這一行「EventHandler temp = RowClicked;」 – MindGame 2012-03-19 16:52:45

+0

我不相信......你是否按照我所描述的那樣宣佈活動並刪除添加和刪除行?你不需要那些模板化的版本。 – Hogan 2012-03-19 16:55:58

+0

我在asp.net中擴展了GridView控件,我添加了公共事件EventHandler RowClicked;「它說它已經聲明瞭。嗯 – MindGame 2012-03-19 16:56:15