将此VB代码转换为C#?

时间:2012-03-25 11:14:45

标签: c# vb.net code-conversion

我想知道是否有人能够帮助我将下面的代码转换为c#?从我所看到的看来它似乎是相似的但是我不确定我的'案例'陈述是否仍然可以使用?

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

4 个答案:

答案 0 :(得分:4)

这不是“为我做这个”请求的地方。为此,您可以聘请程序员。

在您已经尝试过自己的事情之后,这是寻求建议或指导的地方。

无论如何,我会告诉你如何将Select Case转换为它的C#等价物,你可以从那里取得它。

其中一个陈述的示例代码:

switch (e.KeyCode)
{
    case Keys.Q:
        Label1.BackColor = Color.Red;
        break;
    case Keys.W:
        Label2.BackColor = Color.Red;
        break;
    //.....
}

需要注意的事项:

  1. 与C#中的任何内容一样,大小写很重要 - 例如Case必须成为case
  2. 您必须在每个案例后添加break

答案 1 :(得分:1)

您可以使用在线工具将C#转换为Vb,反之亦然

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

答案 2 :(得分:0)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;

public class Form1
{

    System.DateTime dateStart;
    System.DateTime dteFinish;

    TimeSpan span;

    public void KeyDown(System.Object Sender, System.Windows.Forms.KeyEventArgs e)
    {
        switch (e.KeyCode) {
            case Keys.Q:
                Label1.BackColor = Color.Green;
                dteStart = DateAndTime.Now();
                break;
            case Keys.W:
                Label2.BackColor = Color.Green;
                break;
            case Keys.E:
                Label3.BackColor = Color.Green;
                break;
            case Keys.R:
                Label4.BackColor = Color.Green;
                dteFinish = DateAndTime.Now();
                span = dteFinish.Subtract(dteStart);
                Label5.Text = span.ToString();

                break;
        }

    }


    public void KeyUp(System.Object Sender, System.Windows.Forms.KeyEventArgs e)
    {
        switch (e.KeyCode) {
            case Keys.Q:
                Label1.BackColor = Color.Red;
                break;
            case Keys.W:
                Label2.BackColor = Color.Red;
                break;
            case Keys.E:
                Label3.BackColor = Color.Red;
                break;
            case Keys.R:
                Label4.BackColor = Color.Red;
                break;
        }

    }
    public Form1()
    {
        KeyUp += KeyUp;
        KeyDown += KeyDown;
    }
}

答案 3 :(得分:0)

使用此online tool转换代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class Form1
{

    System.DateTime dteStart;
    System.DateTime dteFinish;

    TimeSpan span;

    public void KeyDown(System.Object Sender, System.Windows.Forms.KeyEventArgs e)
    {
        switch (e.KeyCode) {
            case Keys.Q:
                Label1.BackColor = Color.Green;
                dteStart = DateAndTime.Now();
                break;
            case Keys.W:
                Label2.BackColor = Color.Green;
                break;
            case Keys.E:
                Label3.BackColor = Color.Green;
                break;
            case Keys.R:
                Label4.BackColor = Color.Green;
                dteFinish = DateAndTime.Now();
                span = dteFinish.Subtract(dteStart);
                Label5.Text = span.ToString();

                break;
        }

    }


    public void KeyUp(System.Object Sender, System.Windows.Forms.KeyEventArgs e)
    {
        switch (e.KeyCode) {
            case Keys.Q:
                Label1.BackColor = Color.Red;
                break;
            case Keys.W:
                Label2.BackColor = Color.Red;
                break;
            case Keys.E:
                Label3.BackColor = Color.Red;
                break;
            case Keys.R:
                Label4.BackColor = Color.Red;
                break;
        }

    }
    public Form1()
    {
        KeyUp += KeyUp;
        KeyDown += KeyDown;
    }
}