Search code examples
c#vb.netcode-conversion

Convert this VB code to C#?


I was wondering if anyone would be able to help me convert the below code to c#? From what I have read it seems to be similar however I am not sure if my 'case' statements are still able to be used?

Public Class Form1

    Dim dteStart As Date
    Dim dteFinish As Date
    Dim span As TimeSpan

    Public Sub KeyDown(ByVal Sender As System.Object, ByVal e As  _
    System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

        Select Case e.KeyCode
            Case Keys.Q
                Label1.BackColor = Color.Green
                dteStart = Now()
            Case Keys.W
                Label2.BackColor = Color.Green
            Case Keys.E
                Label3.BackColor = Color.Green
            Case Keys.R
                Label4.BackColor = Color.Green
                dteFinish = Now()
                span = dteFinish.Subtract(dteStart)
                Label5.Text = span.ToString

        End Select

    End Sub

    Public Sub KeyUp(ByVal Sender As System.Object, ByVal e As  _
   System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp

        Select Case e.KeyCode
            Case Keys.Q
                Label1.BackColor = Color.Red
            Case Keys.W
                Label2.BackColor = Color.Red
            Case Keys.E
                Label3.BackColor = Color.Red
            Case Keys.R
                Label4.BackColor = Color.Red
        End Select

    End Sub
End Class

Solution

  • You can use online tools for converting C# to Vb or vice versa

    http://www.developerfusion.com/tools/convert/vb-to-csharp/