A ver si puedo traducir de VB a C# estos códigos poco a poco.
Voy a probar a ver como queda poco a poco con este enlace.
http://www.developerfusion.com/tools/convert/csharp-to-vb/
Saludo.
EDITO:
Estoy haciendo pruebas y he convertido de VB a C# pero no funciona, al meno se ve el código para empezar a dar un pequeño paso.
Moyano. ¿Crees que te puede dar una idea para empezar desde cero con C# lo mismo que VB .net?
No tengo mucha idea de VB, aún intentaré por mi lado pasarlo desde cero a C# al menos empezando el Form1 y botones antes de poner códigos.
Código:
'UPGRADE_WARNING: Event Check1.CheckStateChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup2075"'
Private Sub Check1_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Check1.CheckStateChanged
Dim send_buf(64) As Byte
Dim receive_buf(64) As Byte
Dim RecvLength As Integer
OpenMPUSBDevice()
If myOutPipe <> INVALID_HANDLE_VALUE And myInPipe <> INVALID_HANDLE_VALUE Then
RecvLength = 1
send_buf(0) = 50 '0x32
send_buf(1) = 3
If Check1.CheckState = 1 Then
send_buf(2) = 1
ElseIf Check1.CheckState = 0 Then
send_buf(2) = 0
End If
If (SendReceivePacket(send_buf, 3, receive_buf, RecvLength, 1000, 1000) = 1) Then
If (RecvLength <> 1 Or receive_buf(0) <> 50) Then
MsgBox("Failed to update LED")
End If
End If
End If
CloseMPUSBDevice()
End Sub
'UPGRADE_WARNING: Event Check2.CheckStateChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup2075"'
Private Sub Check2_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Check2.CheckStateChanged
Dim send_buf(64) As Byte
Dim receive_buf(64) As Byte
Dim RecvLength As Integer
OpenMPUSBDevice()
If myOutPipe <> INVALID_HANDLE_VALUE And myInPipe <> INVALID_HANDLE_VALUE Then
RecvLength = 1
send_buf(0) = 50 '0x32
send_buf(1) = 4
If Check2.CheckState = 1 Then
send_buf(2) = 1
ElseIf Check2.CheckState = 0 Then
send_buf(2) = 0
End If
If (SendReceivePacket(send_buf, 3, receive_buf, RecvLength, 1000, 1000) = 1) Then
If (RecvLength <> 1 Or receive_buf(0) <> 50) Then
MsgBox("Failed to update LED")
End If
End If
End If
CloseMPUSBDevice()
End Sub
Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
Me.Close()
End Sub
Private Sub Command2_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command2.Click
End
End Sub
Private Sub Command3_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command3.Click
Dim send_buf(64) As Byte
Dim receive_buf(64) As Byte
Dim RecvLength As Integer
OpenMPUSBDevice()
If myOutPipe <> INVALID_HANDLE_VALUE And myInPipe <> INVALID_HANDLE_VALUE Then
RecvLength = 4
send_buf(0) = 0 '0x0 - READ_VERSION
send_buf(1) = 2
If (SendReceivePacket(send_buf, 2, receive_buf, RecvLength, 1000, 1000) = 1) Then
If (RecvLength <> 4 Or receive_buf(0) <> 0) Then
MsgBox("Failed to obtain version information.")
Else
Text1.Text = "Demo Version " & Str(receive_buf(3)) & "." & Str(receive_buf(2))
End If
End If
End If
CloseMPUSBDevice()
End Sub
Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
Initialize()
End Sub
End Class
http://www.developerfusion.com/tools/convert/csharp-to-vb/
Saludo.
EDITO:
Estoy haciendo pruebas y he convertido de VB a C# pero no funciona, al meno se ve el código para empezar a dar un pequeño paso.
Código:
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.Compatibility;
using System;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
// ERROR: Not supported in C#: OptionDeclaration
namespace Project1
{
//======================================================================================
// VB.NET Example code MPUSBAPI.DLL
// Free to use code supplied by www.comvcon.com
// Use this code at you own will and risk
// Please do not remove this line: www.comvcon.com
//======================================================================================
internal class Form1 : System.Windows.Forms.Form
{
#region "Windows Form Designer generated code "
public Form1() : base()
{
Load += Form1_Load;
if (m_vb6FormDefInstance == null) {
if (m_InitializingDefInstance) {
m_vb6FormDefInstance = this;
} else {
try {
//For the start-up form, the first instance created is the default instance.
if (object.ReferenceEquals(System.Reflection.Assembly.GetExecutingAssembly().EntryPoint.DeclaringType, this.GetType())) {
m_vb6FormDefInstance = this;
}
} catch {
}
}
}
//This call is required by the Windows Form Designer.
InitializeComponent();
}
//Form overrides dispose to clean up the component list.
protected override void Dispose(bool Disposing)
{
if (Disposing) {
if ((components != null)) {
components.Dispose();
}
}
base.Dispose(Disposing);
}
//Required by the Windows Form Designer
private System.ComponentModel.IContainer components;
public System.Windows.Forms.ToolTip ToolTip1;
public System.Windows.Forms.TextBox Text1;
private System.Windows.Forms.Button withEventsField_Command3;
public System.Windows.Forms.Button Command3 {
get { return withEventsField_Command3; }
set {
if (withEventsField_Command3 != null) {
withEventsField_Command3.Click -= Command3_Click;
}
withEventsField_Command3 = value;
if (withEventsField_Command3 != null) {
withEventsField_Command3.Click += Command3_Click;
}
}
}
private System.Windows.Forms.CheckBox withEventsField_Check2;
public System.Windows.Forms.CheckBox Check2 {
get { return withEventsField_Check2; }
set {
if (withEventsField_Check2 != null) {
withEventsField_Check2.CheckStateChanged -= Check2_CheckStateChanged;
}
withEventsField_Check2 = value;
if (withEventsField_Check2 != null) {
withEventsField_Check2.CheckStateChanged += Check2_CheckStateChanged;
}
}
}
private System.Windows.Forms.CheckBox withEventsField_Check1;
public System.Windows.Forms.CheckBox Check1 {
get { return withEventsField_Check1; }
set {
if (withEventsField_Check1 != null) {
withEventsField_Check1.CheckStateChanged -= Check1_CheckStateChanged;
}
withEventsField_Check1 = value;
if (withEventsField_Check1 != null) {
withEventsField_Check1.CheckStateChanged += Check1_CheckStateChanged;
}
}
}
private System.Windows.Forms.Button withEventsField_Command1;
public System.Windows.Forms.Button Command1 {
get { return withEventsField_Command1; }
set {
if (withEventsField_Command1 != null) {
withEventsField_Command1.Click -= Command1_Click;
}
withEventsField_Command1 = value;
if (withEventsField_Command1 != null) {
withEventsField_Command1.Click += Command1_Click;
}
}
}
private System.Windows.Forms.Button withEventsField_Command2;
public System.Windows.Forms.Button Command2 {
get { return withEventsField_Command2; }
set {
if (withEventsField_Command2 != null) {
withEventsField_Command2.Click -= Command2_Click;
}
withEventsField_Command2 = value;
if (withEventsField_Command2 != null) {
withEventsField_Command2.Click += Command2_Click;
}
}
}
//NOTE: The following procedure is required by the Windows Form Designer
//It can be modified using the Windows Form Designer.
//Do not modify it using the code editor.
[System.Diagnostics.DebuggerStepThrough()]
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.components = new System.ComponentModel.Container();
this.ToolTip1 = new System.Windows.Forms.ToolTip(components);
this.ToolTip1.Active = true;
this.Text1 = new System.Windows.Forms.TextBox();
this.Command3 = new System.Windows.Forms.Button();
this.Check2 = new System.Windows.Forms.CheckBox();
this.Check1 = new System.Windows.Forms.CheckBox();
this.Command1 = new System.Windows.Forms.Button();
this.Command2 = new System.Windows.Forms.Button();
this.Text = "VB MPUSBAPI Demo";
this.ClientSize = new System.Drawing.Size(340, 151);
this.Location = new System.Drawing.Point(4, 23);
this.StartPosition = System.Windows.Forms.FormStartPosition.WindowsDefaultLocation;
this.Font = new System.Drawing.Font("Arial", 8f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, Convert.ToByte(0));
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.SystemColors.Control;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
this.ControlBox = true;
this.Enabled = true;
this.KeyPreview = false;
this.MaximizeBox = true;
this.MinimizeBox = true;
this.Cursor = System.Windows.Forms.Cursors.Default;
this.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.ShowInTaskbar = true;
this.HelpButton = false;
this.WindowState = System.Windows.Forms.FormWindowState.Normal;
this.Name = "Form1";
this.Text1.AutoSize = false;
this.Text1.Size = new System.Drawing.Size(257, 25);
this.Text1.Location = new System.Drawing.Point(24, 104);
this.Text1.TabIndex = 5;
this.Text1.Text = "Text1";
this.Text1.Font = new System.Drawing.Font("Arial", 8f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, Convert.ToByte(0));
this.Text1.AcceptsReturn = true;
this.Text1.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;
this.Text1.BackColor = System.Drawing.SystemColors.Window;
this.Text1.CausesValidation = true;
this.Text1.Enabled = true;
this.Text1.ForeColor = System.Drawing.SystemColors.WindowText;
this.Text1.HideSelection = true;
this.Text1.ReadOnly = false;
this.Text1.MaxLength = 0;
this.Text1.Cursor = System.Windows.Forms.Cursors.IBeam;
this.Text1.Multiline = false;
this.Text1.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.Text1.ScrollBars = System.Windows.Forms.ScrollBars.None;
this.Text1.TabStop = true;
this.Text1.Visible = true;
this.Text1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.Text1.Name = "Text1";
this.Command3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.Command3.Text = "Get Version";
this.Command3.Size = new System.Drawing.Size(129, 25);
this.Command3.Location = new System.Drawing.Point(16, 72);
this.Command3.TabIndex = 4;
this.Command3.Font = new System.Drawing.Font("Arial", 8f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, Convert.ToByte(0));
this.Command3.BackColor = System.Drawing.SystemColors.Control;
this.Command3.CausesValidation = true;
this.Command3.Enabled = true;
this.Command3.ForeColor = System.Drawing.SystemColors.ControlText;
this.Command3.Cursor = System.Windows.Forms.Cursors.Default;
this.Command3.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.Command3.TabStop = true;
this.Command3.Name = "Command3";
this.Check2.Text = "LED4";
this.Check2.Size = new System.Drawing.Size(81, 17);
this.Check2.Location = new System.Drawing.Point(128, 24);
this.Check2.TabIndex = 3;
this.Check2.Font = new System.Drawing.Font("Arial", 8f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, Convert.ToByte(0));
this.Check2.CheckAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.Check2.BackColor = System.Drawing.SystemColors.Control;
this.Check2.CausesValidation = true;
this.Check2.Enabled = true;
this.Check2.ForeColor = System.Drawing.SystemColors.ControlText;
this.Check2.Cursor = System.Windows.Forms.Cursors.Default;
this.Check2.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.Check2.Appearance = System.Windows.Forms.Appearance.Normal;
this.Check2.TabStop = true;
this.Check2.CheckState = System.Windows.Forms.CheckState.Unchecked;
this.Check2.Visible = true;
this.Check2.Name = "Check2";
this.Check1.Text = "LED3";
this.Check1.Size = new System.Drawing.Size(81, 17);
this.Check1.Location = new System.Drawing.Point(40, 24);
this.Check1.TabIndex = 2;
this.Check1.Font = new System.Drawing.Font("Arial", 8f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, Convert.ToByte(0));
this.Check1.CheckAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.Check1.BackColor = System.Drawing.SystemColors.Control;
this.Check1.CausesValidation = true;
this.Check1.Enabled = true;
this.Check1.ForeColor = System.Drawing.SystemColors.ControlText;
this.Check1.Cursor = System.Windows.Forms.Cursors.Default;
this.Check1.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.Check1.Appearance = System.Windows.Forms.Appearance.Normal;
this.Check1.TabStop = true;
this.Check1.CheckState = System.Windows.Forms.CheckState.Unchecked;
this.Check1.Visible = true;
this.Check1.Name = "Check1";
this.Command1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.Command1.Text = "Ok";
this.AcceptButton = this.Command1;
this.Command1.Size = new System.Drawing.Size(81, 25);
this.Command1.Location = new System.Drawing.Point(256, 8);
this.Command1.TabIndex = 1;
this.Command1.Font = new System.Drawing.Font("Arial", 8f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, Convert.ToByte(0));
this.Command1.BackColor = System.Drawing.SystemColors.Control;
this.Command1.CausesValidation = true;
this.Command1.Enabled = true;
this.Command1.ForeColor = System.Drawing.SystemColors.ControlText;
this.Command1.Cursor = System.Windows.Forms.Cursors.Default;
this.Command1.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.Command1.TabStop = true;
this.Command1.Name = "Command1";
this.Command2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.CancelButton = this.Command2;
this.Command2.Text = "Cancel";
this.Command2.Size = new System.Drawing.Size(81, 25);
this.Command2.Location = new System.Drawing.Point(256, 40);
this.Command2.TabIndex = 0;
this.Command2.Font = new System.Drawing.Font("Arial", 8f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, Convert.ToByte(0));
this.Command2.BackColor = System.Drawing.SystemColors.Control;
this.Command2.CausesValidation = true;
this.Command2.Enabled = true;
this.Command2.ForeColor = System.Drawing.SystemColors.ControlText;
this.Command2.Cursor = System.Windows.Forms.Cursors.Default;
this.Command2.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.Command2.TabStop = true;
this.Command2.Name = "Command2";
this.Controls.Add(Text1);
this.Controls.Add(Command3);
this.Controls.Add(Check2);
this.Controls.Add(Check1);
this.Controls.Add(Command1);
this.Controls.Add(Command2);
}
#endregion
#region "Upgrade Support "
private static Form1 m_vb6FormDefInstance;
private static bool m_InitializingDefInstance;
public static Form1 DefInstance {
get {
if (m_vb6FormDefInstance == null || m_vb6FormDefInstance.IsDisposed) {
m_InitializingDefInstance = true;
m_vb6FormDefInstance = new Form1();
m_InitializingDefInstance = false;
}
DefInstance = m_vb6FormDefInstance;
}
set { m_vb6FormDefInstance = value; }
}
#endregion
//UPGRADE_WARNING: Event Check1.CheckStateChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup2075"'
private void Check1_CheckStateChanged(System.Object eventSender, System.EventArgs eventArgs)
{
byte[] send_buf = new byte[65];
byte[] receive_buf = new byte[65];
int RecvLength = 0;
VBMPUSBAPI.OpenMPUSBDevice();
if (VBMPUSBAPI.myOutPipe != VBMPUSBAPI.INVALID_HANDLE_VALUE & VBMPUSBAPI.myInPipe != VBMPUSBAPI.INVALID_HANDLE_VALUE) {
RecvLength = 1;
send_buf[0] = 50;
//0x32
send_buf[1] = 3;
if (Check1.CheckState == 1) {
send_buf[2] = 1;
} else if (Check1.CheckState == 0) {
send_buf[2] = 0;
}
if ((VBMPUSBAPI.SendReceivePacket(ref send_buf, ref 3, ref receive_buf, ref RecvLength, 1000, 1000) == 1)) {
if ((RecvLength != 1 | receive_buf[0] != 50)) {
Interaction.MsgBox("Failed to update LED");
}
}
}
VBMPUSBAPI.CloseMPUSBDevice();
}
//UPGRADE_WARNING: Event Check2.CheckStateChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup2075"'
private void Check2_CheckStateChanged(System.Object eventSender, System.EventArgs eventArgs)
{
byte[] send_buf = new byte[65];
byte[] receive_buf = new byte[65];
int RecvLength = 0;
VBMPUSBAPI.OpenMPUSBDevice();
if (VBMPUSBAPI.myOutPipe != VBMPUSBAPI.INVALID_HANDLE_VALUE & VBMPUSBAPI.myInPipe != VBMPUSBAPI.INVALID_HANDLE_VALUE) {
RecvLength = 1;
send_buf[0] = 50;
//0x32
send_buf[1] = 4;
if (Check2.CheckState == 1) {
send_buf[2] = 1;
} else if (Check2.CheckState == 0) {
send_buf[2] = 0;
}
if ((VBMPUSBAPI.SendReceivePacket(ref send_buf, ref 3, ref receive_buf, ref RecvLength, 1000, 1000) == 1)) {
if ((RecvLength != 1 | receive_buf[0] != 50)) {
Interaction.MsgBox("Failed to update LED");
}
}
}
VBMPUSBAPI.CloseMPUSBDevice();
}
private void Command1_Click(System.Object eventSender, System.EventArgs eventArgs)
{
this.Close();
}
private void Command2_Click(System.Object eventSender, System.EventArgs eventArgs)
{
System.Environment.Exit(0);
}
private void Command3_Click(System.Object eventSender, System.EventArgs eventArgs)
{
byte[] send_buf = new byte[65];
byte[] receive_buf = new byte[65];
int RecvLength = 0;
VBMPUSBAPI.OpenMPUSBDevice();
if (VBMPUSBAPI.myOutPipe != VBMPUSBAPI.INVALID_HANDLE_VALUE & VBMPUSBAPI.myInPipe != VBMPUSBAPI.INVALID_HANDLE_VALUE) {
RecvLength = 4;
send_buf[0] = 0;
//0x0 - READ_VERSION
send_buf[1] = 2;
if ((VBMPUSBAPI.SendReceivePacket(ref send_buf, ref 2, ref receive_buf, ref RecvLength, 1000, 1000) == 1)) {
if ((RecvLength != 4 | receive_buf[0] != 0)) {
Interaction.MsgBox("Failed to obtain version information.");
} else {
Text1.Text = "Demo Version " + Conversion.Str(receive_buf[3]) + "." + Conversion.Str(receive_buf[2]);
}
}
}
VBMPUSBAPI.CloseMPUSBDevice();
}
private void Form1_Load(System.Object eventSender, System.EventArgs eventArgs)
{
VBMPUSBAPI.Initialize();
}
}
}
Moyano. ¿Crees que te puede dar una idea para empezar desde cero con C# lo mismo que VB .net?
No tengo mucha idea de VB, aún intentaré por mi lado pasarlo desde cero a C# al menos empezando el Form1 y botones antes de poner códigos.
Última edición: