我的代码是否正确地从C#翻译为Vb.Net?

时间:2015-12-16 22:33:16

标签: c# vb.net

我在这个答案中找到了创建类似向导(下一个/上一个)表单的可能解决方案:Creating Wizards for Windows Forms in C#

class WizardPages : TabControl
{
    protected override void WndProc(ref Message m)
    {
        // Hide tabs by trapping the TCM_ADJUSTRECT message
        if (m.Msg == 0x1328 && !DesignMode) m.Result = (IntPtr)1;
        else base.WndProc(ref m);
    }        

    protected override void OnKeyDown(KeyEventArgs ke)
    {
        // Block Ctrl+Tab and Ctrl+Shift+Tab hotkeys
        if (ke.Control && ke.KeyCode == Keys.Tab) 
            return;
        base.OnKeyDown(ke);
    }
}

该解决方案允许我在Designer中创建选项卡,并在运行时隐藏它们。我试图将其翻译成VB.NET,并使用:

Imports System
Imports System.Windows.Forms

Public Class WizardPages
    Inherits TabControl

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If (m.Msg = 4904 And Not DesignMode) Then '4904 is Dec of 0x1328 Hex
            m.Result = IntPtr.Zero 'IntPtr1
        Else
            MyBase.WndProc(m)
        End If

    End Sub

End Class

我没有翻译(但仍然有效)的唯一部分是来自C#代码的m.Result = (IntPtr)1;。如您所见,我尝试使用m.Result = IntPtr.Zero

目前我不知道如果我这样做会发生什么。

2 个答案:

答案 0 :(得分:3)

将你的答案与@ Usman's结合起来产生以下结果。为了获得1作为IntPtr,我使用了new IntPtr(1)语法,它应该起作用。或者CType(1, IntPtr)也应该有效。但是,我还没有测试过。

进口系统     Imports System.Windows.Forms

Public Class WizardPages
  Inherits TabControl

  Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    If m.Msg = &H1328 AndAlso Not DesignMode Then
      m.Result = new IntPtr(1)
    Else
      MyBase.WndProc(m)
    End If

  End Sub

  Protected Overrides Sub OnKeyDown(ke As KeyEventArgs)
    ' Block Ctrl+Tab and Ctrl+Shift+Tab hotkeys
    If ke.Control AndAlso ke.KeyCode = Keys.Tab Then Return
    MyBase.OnKeyDown(ke)
  End Sub
End Class

答案 1 :(得分:-2)

使用Telerik's Converter

这是将C#转换为VB后的输出

Class WizardPages Inherits TabControl
    Protected Overrides Sub WndProc(ByRef m As Message)

        If m.Msg = &H1328 AndAlso Not DesignMode Then
            m.Result = DirectCast(1, IntPtr)
        Else
            MyBase.WndProc(m)
        End If

    End Sub

    Protected Overrides Sub OnKeyDown(ke As KeyEventArgs)

        If ke.Control AndAlso ke.KeyCode = Keys.Tab Then
            Return
        End If
        MyBase.OnKeyDown(ke)
    End Sub
End Class