本文介绍了将代码从C#转换为VB时会感到困惑...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好,我正在将一些C#代码转换为VB并且我几乎已经完成了b $ b,但这最后一点让我感到困惑。 我正在尝试转换以下C#代码... 公共事件EventHandler< EnterKeyEventArgsEnterKeyEvent; private void Body_KeyDown(object sender,HtmlElementEventArgs e) { if(e.KeyPressedCode == 13&&!e.ShiftKeyPressed) { //处理输入代码取消 bool cancel = false; if(EnterKeyEvent!= null) { EnterKeyEventArgs args = new EnterKeyEventArgs(); EnterKeyEvent(this,args); cancel = args.Cancel; } e.ReturnValue =!取消; } } 进入VB,到目前为止我有... 公共事件EnterKeyEvent As EventHandler(Of EnterKeyEventArgs) 私有子Body_KeyDown(ByVal发送者为Ob ject,ByVal e As HtmlElementEventArgs) 如果e.KeyPressedCode = 13而且也不是e.ShiftKeyPressed那么 Dim取消As Boolean = False 如果不是IsNothing(EnterKeyEvent)那么''问题行 Dim args As New EnterKeyEventArgs() RaiseEvent EnterKeyEvent(Me, args) cancel = args.Cancel 结束如果 e.ReturnValue =不取消 结束如果 结束子 上面的VB部分有一行注释PROBLEM LINE 在末尾。 Visual Studio给出了以下错误... " ''公共事件EnterKeyEvent(发送者为对象,e为EnterKeyEventArgs)'' 是一个事件,不能直接调用。使用''RaiseEvent'' 语句来举办活动 " 我以为这是在检查活动而不是试图筹集一个? 如果有人知道我应该怎么做,我将非常感激! Karl 解决方案 " Karl Rhodes" < go ********** @ cortexa.co.ukschrieb: If Not IsNothing(EnterKeyEvent)Then''PROBLEM LINE Dim args As New EnterKeyEventArgs() RaiseEvent EnterKeyEvent(Me,args) cancel = args.Cancel 结束If 只需删除无的检查。如果代表被附加,''RaiseEvent''只会提高 事件,遗憾的是没有在C#中完成。 - MS Herfried K. Wagner MVP< URL:http://dotnet.mvps.org/> VB< URL:http:// dotnet.mvps.org/dotnet/faqs/> 8月13日,22:43,Herfried K. Wagner [MVP]" < hirf-spam-me- h ... @ gmx.atwrote: " Karl Rhodes" < googlegro ... @ cortexa.co.ukschrieb: If Not IsNothing(EnterKeyEvent)Then''PROBLEM LINE Dim args作为New EnterKeyEventArgs() RaiseEvent EnterKeyEvent(Me,args) cancel = args.Cancel 结束如果 只需删除没有的检查。如果代表被附加,''RaiseEvent''只会提高 事件,遗憾的是没有在C#中完成。 - MS Herfried K. Wagner MVP< URL:http://dotnet.mvps.org/> VB< URL:http:// dotnet.mvps.org/dotnet/faqs/> 嗨Herfried, 我担心它没有任何区别。我把这行改为 两个如果EnterKeyEvent然后和If(EnterKeyEvent)然后我仍然 有同样的错误... 您是否完全删除了if语句? Karl " Karl Rhodes" < go ********** @ cortexa.co.ukschrieb: If Not IsNothing(EnterKeyEvent)然后''问题行 Dim args As New EnterKeyEventArgs() RaiseEvent EnterKeyEvent(Me,args) cancel = args.Cancel 结束如果 只需删除没有的检查。如果代表被附加,''RaiseEvent''只会引发事件,遗憾的是,这不会在C#中完成。 我担心它没有任何区别。我把这行改为 两个如果EnterKeyEvent然后和If(EnterKeyEvent)然后我仍然 有同样的错误... 您是否完全删除了if语句? 是的,VB中根本不需要它。 - MS Herfried K. Wagner MVP< URL:http://dotnet.mvps.org/> VB< URL:http:// dotnet。 mvps.org/dotnet/faqs/> Hi all, I''m in the middle of converting some C# code to VB and I''malmost done, but this last little bit is confusing me. I''m trying to convert the following C# code... public event EventHandler<EnterKeyEventArgsEnterKeyEvent; private void Body_KeyDown(object sender, HtmlElementEventArgs e){if (e.KeyPressedCode == 13 && !e.ShiftKeyPressed){// handle enter code cancellationbool cancel = false;if (EnterKeyEvent != null){EnterKeyEventArgs args = new EnterKeyEventArgs();EnterKeyEvent(this, args);cancel = args.Cancel;}e.ReturnValue = !cancel;}} into VB, and so far I have... Public Event EnterKeyEvent As EventHandler(Of EnterKeyEventArgs) Private Sub Body_KeyDown(ByVal sender As Object, ByVal e AsHtmlElementEventArgs) If e.KeyPressedCode = 13 AndAlso Not e.ShiftKeyPressed ThenDim cancel As Boolean = FalseIf Not IsNothing(EnterKeyEvent) Then ''PROBLEM LINEDim args As New EnterKeyEventArgs()RaiseEvent EnterKeyEvent(Me, args)cancel = args.CancelEnd Ife.ReturnValue = Not cancelEnd If End Sub There is a line in the VB section above with a comment "PROBLEM LINE"at the end. Visual Studio gives me the following error... "''Public Event EnterKeyEvent(sender as Object, e as EnterKeyEventArgs)''is an event, and cannot be called directly. Use a ''RaiseEvent''statement to raise an event" I thought this was checking for an event and not trying to raise one?If anyone know''s how I should do this I would be most grateful! Karl 解决方案 "Karl Rhodes" <go**********@cortexa.co.ukschrieb: If Not IsNothing(EnterKeyEvent) Then ''PROBLEM LINE Dim args As New EnterKeyEventArgs() RaiseEvent EnterKeyEvent(Me, args) cancel = args.Cancel End IfSimply remove the check for ''Nothing''. ''RaiseEvent'' will only raise theevent if delegates are attached, which is unfortunately not done in C#. --M S Herfried K. WagnerM V P <URL:http://dotnet.mvps.org/>V B <URL:http://dotnet.mvps.org/dotnet/faqs/> On 13 Aug, 22:43, "Herfried K. Wagner [MVP]" <[email protected]:"Karl Rhodes" <[email protected]: If Not IsNothing(EnterKeyEvent) Then ''PROBLEM LINE Dim args As New EnterKeyEventArgs() RaiseEvent EnterKeyEvent(Me, args) cancel = args.Cancel End If Simply remove the check for ''Nothing''. ''RaiseEvent'' will only raise theevent if delegates are attached, which is unfortunately not done in C#.-- M S Herfried K. WagnerM V P <URL:http://dotnet.mvps.org/> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>Hi Herfried, I''m afraid it hasn''t made any difference. I changed the line to readboth "If EnterKeyEvent Then" and "If (EnterKeyEvent) Then" and I stillhave the same error... Did you mean for me to remove the if statement altogether? Karl "Karl Rhodes" <go**********@cortexa.co.ukschrieb: If Not IsNothing(EnterKeyEvent) Then ''PROBLEM LINE Dim args As New EnterKeyEventArgs() RaiseEvent EnterKeyEvent(Me, args) cancel = args.Cancel End If Simply remove the check for ''Nothing''. ''RaiseEvent'' will only raise theevent if delegates are attached, which is unfortunately not done in C#. I''m afraid it hasn''t made any difference. I changed the line to readboth "If EnterKeyEvent Then" and "If (EnterKeyEvent) Then" and I stillhave the same error...Did you mean for me to remove the if statement altogether?Yes, it''s not required at all in VB. --M S Herfried K. WagnerM V P <URL:http://dotnet.mvps.org/>V B <URL:http://dotnet.mvps.org/dotnet/faqs/> 这篇关于将代码从C#转换为VB时会感到困惑...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-25 05:00