|
-
Dec 6th, 2008, 08:06 PM
#1
Thread Starter
Frenzied Member
VB.net convert to Csh
Hi,
Can somebody please help convert this simple program. when I use the converter it says that the Public Declare Function is not supported.
Any help with code translation?
Code:
Public Class Form1
Public Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal vKey As Integer) As Integer
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If GetAsyncKeyState(17) And e.KeyCode = Keys.X Or GetAsyncKeyState(18) And e.KeyCode = Keys.X Then
MsgBox("ALT+X pressed")
Me.Close()
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim num1, num2 As Integer
num1 = CInt(TextBox1.Text)
num2 = CInt(TextBox2.Text)
Label2.Text = num1 + num2
End Sub
End Class
------------------------------------------------------------------------
If an answer to your question has been helpful, then please, Rate it! 
-
Dec 6th, 2008, 08:37 PM
#2
Re: VB.net convert to Csh
That 'Declare' key word is a holdover from VB6. C# requires you to use the DllImport attribute to declare external functions. VB.NET supports the DllImport attribute too. I suggest that you use it for all API declarations, regardless of language.
If you go to www.pinvoke.net you'll find C# declarations for the GetAsyncKeyState function.
-
Dec 7th, 2008, 02:07 PM
#3
Thread Starter
Frenzied Member
Re: VB.net convert to Csh
hi .
Have some syntax errors and Im stuck in its conversion. Can someone pls help.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
public class Form1
{
[DllImport("user32", EntryPoint = "GetAsyncKeyState", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int GetAsyncKeyState(int vKey);
}
static void Main()
{
Application.Run(new Form1());
}
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (GetAsyncKeyState(17) & e.KeyCode == Keys.X | GetAsyncKeyState(18) & e.KeyCode == Keys.X)
{
Interaction.MsgBox("ALT+X pressed");
this.Close();
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void Button2_Click(object sender, System.EventArgs e)
{
int num1 = 0;
int num2 = 0;
num1 = (int)TextBox1.Text;
num2 = (int)TextBox2.Text;
Label2.Text = num1 + num2;
}
}
)
------------------------------------------------------------------------
If an answer to your question has been helpful, then please, Rate it! 
-
Dec 7th, 2008, 04:06 PM
#4
Re: VB.net convert to Csh
First of all, to do logical checks in an 'if' in C#, you need to use the logical operators && for AND, and || for OR, and not the single & and | characters.
Secondly, it appears that the GetAsyncKeyState function return an integer value (an int or a short, depending on the docs you read.) Therefore I would suggest checking the value of this return is what you expect it to be, in conjunction to the Keycode check, otherwise the 'if' will be attempting to compare an int with a bool, which it complains at.
To this end, try the 'if' like this:
Code:
if (((GetAsyncKeyState(17) > 0) && (e.KeyCode == Keys.X)) || ((GetAsyncKeyState(18) > 0) && (e.KeyCode == Keys.X)))
I should point out that I am in no way an expert at call external functions like this, and I cannot give any guarantee that a return of anything greater than zero from the GetAsyncKeystate function is a valid one!
Also, to get the KeyCode methods to work, you need to set the KeyPreview property of the form to true.
Hope that helps.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|