Cannot convert code from 6 to .NET

T

Tanzim Saqib

I've been trying to learn VB.NET. I know VB6. I can not convert the following VB6 code segment to VB.NET


In VB 6:

' In a module starts here:
Public Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Public Cnt As Long, sSave As String, sOld As String, Ret As String

Public Function GetPressedKey() As String
For Cnt = 32 To 128
If GetAsyncKeyState(Cnt) <> 0 Then
GetPressedKey = Chr$(Cnt)
Exit For
End If
Next Cnt
End Function

Public Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
Ret = GetPressedKey
If Ret <> sOld Then
sOld = Ret
sSave = sSave + sOld
End If
End Sub
' ends here

' In the code window of form1 starts here:
Private Sub Form_Load()
SetTimer Me.hwnd, 0, 1, AddressOf TimerProc
End Sub

Private Sub Form_Unload(Cancel As Integer)
KillTimer Me.hwnd, 0
MsgBox sSave
End Sub
' ends here


I wrote in VB.NET:

' In a form
Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Dim Cnt As Long, sSave As String, sOld As String, Ret As String

Function GetPressedKey() As String
For Cnt = 32 To 128
If GetAsyncKeyState(Cnt) <> 0 Then
GetPressedKey = Chr(Cnt)
Exit For
End If
Next Cnt
End Function

Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
Ret = GetPressedKey()
If Ret <> sOld Then
sOld = Ret
sSave = sSave + sOld
End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' following 2 lines produces this error:
' D:\WindowsApplication1\Form1.vb(69): 'hwnd' is not a member of 'WindowsApplication1.Form1'

SetTimer(Me.hwnd, 0, 1, AddressOf TimerProc)
KillTimer(Me.hwnd, 0)
MsgBox(sSave)
End Sub


What can I write instead of Me.hwnd ??

Thanks in advance!


Tanzim Saqib

Problemsetter, ACM Valladolid Online Judge, Spain.
Website: http://tanzim.tk


This posting is provided "AS IS" with no warranties.
 
C

Cor

Hi Tanzim,

You try to use unmanaged code where probably is enough managed code for.
Have a look at the events for keydown, keyup, keypressed and the classes system.timers.timer and the more simple forms.form.timer.

I think it would not be good if someone would help you on this way that is not VB.net.

If you have a problem, describe it, then I am sure someone will help you if it is not me.

Cor
 
O

One Handed Man [ OHM# ]

Firstly, I would not really bother using a timer definition by decaring the API. You can add a timer to your code by dragging it onto the form from the ToolBox. Then I suggest you look at the help for this.

But to answer your question it's 'Me.Handle' but you need to change the declaration of the SetTimer function window handle to to 'System.IntPtr' not Long. This is a system dependent integer value Type.
Regards - OHM#


(e-mail address removed)
I've been trying to learn VB.NET. I know VB6. I can not convert the following VB6 code segment to VB.NET


In VB 6:

' In a module starts here:
Public Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Public Cnt As Long, sSave As String, sOld As String, Ret As String

Public Function GetPressedKey() As String
For Cnt = 32 To 128
If GetAsyncKeyState(Cnt) <> 0 Then
GetPressedKey = Chr$(Cnt)
Exit For
End If
Next Cnt
End Function

Public Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
Ret = GetPressedKey
If Ret <> sOld Then
sOld = Ret
sSave = sSave + sOld
End If
End Sub
' ends here

' In the code window of form1 starts here:
Private Sub Form_Load()
SetTimer Me.hwnd, 0, 1, AddressOf TimerProc
End Sub

Private Sub Form_Unload(Cancel As Integer)
KillTimer Me.hwnd, 0
MsgBox sSave
End Sub
' ends here


I wrote in VB.NET:

' In a form
Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Dim Cnt As Long, sSave As String, sOld As String, Ret As String

Function GetPressedKey() As String
For Cnt = 32 To 128
If GetAsyncKeyState(Cnt) <> 0 Then
GetPressedKey = Chr(Cnt)
Exit For
End If
Next Cnt
End Function

Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
Ret = GetPressedKey()
If Ret <> sOld Then
sOld = Ret
sSave = sSave + sOld
End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' following 2 lines produces this error:
' D:\WindowsApplication1\Form1.vb(69): 'hwnd' is not a member of 'WindowsApplication1.Form1'

SetTimer(Me.hwnd, 0, 1, AddressOf TimerProc)
KillTimer(Me.hwnd, 0)
MsgBox(sSave)
End Sub


What can I write instead of Me.hwnd ??

Thanks in advance!


Tanzim Saqib

Problemsetter, ACM Valladolid Online Judge, Spain.
Website: http://tanzim.tk


This posting is provided "AS IS" with no warranties.
 
T

Tanzim Saqib

Dear OHM#

Thanks for replying. I know there is a timer control. As I said before I am very new to VB.NET, but know about VB6. Not that I wanted to learn how Timer can be used. This was just a test how APIs can be called in ..NET. I have made those changes as you have told, but there is a new error:

D:\Project1.NET\Form1.vb(84): 'AddressOf' expression cannot be converted to 'Integer' because 'Integer' is not a delegate type.

I am not totally familiar with the delegate type, may be I have to study more. Can you please tell me the replacement of GetAsyncKeyState() API in .NET??

Thanks again!


Tanzim Saqib

Problemsetter, ACM Valladolid Online Judge, Spain.
Website: http://tanzim.tk


This posting is provided "AS IS" with no warranties.
 
T

Tanzim Saqib

Thank you so much Cor for your nice reply. In fact I have installed VB.NET couple of hours ago and tried to code like VB6 and encountered the problem, trying to go through the MSDN to learn more about .NET. And I was trying to learn how to use Win32 API in .NET so that I did call timer and key related APIs. Can you please tell me the replacement of GetAsyncKeyState() API in .NET??Anyway, thanks again!

Tanzim Saqib

Problemsetter, ACM Valladolid Online Judge, Spain.
Website: http://tanzim.tk


This posting is provided "AS IS" with no warranties.
 
O

One Handed Man [ OHM# ]

Tanzim,
Rather than get into this unmangaed code. I would rather try and help you determine a .NET way to acheive what you require. Please explain what it is you are trying to do and I will give you an answer.

Delegates are used to handle the passing of information to and from events and or between other objects and are used extensively in .NET. It is essential that you get to grips with the concepts.


Regards - OHM#


(e-mail address removed)
Dear OHM#

Thanks for replying. I know there is a timer control. As I said before I am very new to VB.NET, but know about VB6. Not that I wanted to learn how Timer can be used. This was just a test how APIs can be called in ..NET. I have made those changes as you have told, but there is a new error:

D:\Project1.NET\Form1.vb(84): 'AddressOf' expression cannot be converted to 'Integer' because 'Integer' is not a delegate type.

I am not totally familiar with the delegate type, may be I have to study more. Can you please tell me the replacement of GetAsyncKeyState() API in .NET??

Thanks again!


Tanzim Saqib

Problemsetter, ACM Valladolid Online Judge, Spain.
Website: http://tanzim.tk


This posting is provided "AS IS" with no warranties.
 
T

Tanzim Saqib

OHM#
Thanks again for replying. I want to log key press out of my application. I have coded this one in VB6, but can't do in .NET. Hope to hear from you soon.
Tanzim Saqib

Problemsetter, ACM Valladolid Online Judge, Spain.
Website: http://tanzim.tk


This posting is provided "AS IS" with no warranties.
 
O

One Handed Man [ OHM# ]

Set the KeyPreview to True on the form and then use the Form's KeyPress event to trap the characters beings sent from the keyboard. This way, the form will see the keystrokes before any of the controls receive them.

'Set the Forms KeyPreview
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Me.KeyPreview = True

End Sub



'Now trap the kepress Event

Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress

MsgBox(e.KeyChar)

End Sub

Regards - OHM#


(e-mail address removed)
OHM#
Thanks again for replying. I want to log key press out of my application. I have coded this one in VB6, but can't do in .NET. Hope to hear from you soon.
Tanzim Saqib

Problemsetter, ACM Valladolid Online Judge, Spain.
Website: http://tanzim.tk


This posting is provided "AS IS" with no warranties.
 
T

Tanzim Saqib

Sorry OHM#, the answer is wrong. I guess you couldn't understand what I wanted to know. I have told you that I want to log key strokes out of my application. Suppose you are running your application on tray and you have opned notepad and typed something, these key strokes would be recorded by your application. I have coded this thing on my VB6 2 years ago. Since I am new to .net my first attempt was to convert this task in to VB.Net. Hope it clears. Thanks again for your reply.

Tanzim Saqib

Problemsetter, ACM Valladolid Online Judge, Spain.
Website: http://tanzim.tk


This posting is provided "AS IS" with no warranties.
 
C

Cor

Hi Tanzim,

I tested this code from OHM and did exstend it a little bit.

For me it did exact as you said and because I extended it even more.

\\\
Private a As Integer
Private b As Integer
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim bbb As String = Me.TextBox1.Text
MessageBox.Show(TextBox1.Text & " keystrokes= " & _
a.ToString & " backstrokes= " & b.ToString)
End Sub
'Now trap the kepress Event
Private Sub Form1_KeyPress(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
a += 1
If e.KeyChar = Chr(8) Then
b += 1
End If
End Sub
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.KeyPreview = True
Me.TextBox1.Text = ""
End Sub
////

Next time I would check it better, you are so young and now already using "always done" habbits.

Cor


Sorry OHM#, the answer is wrong. I guess you couldn't understand what I wanted to know. I have told you that I want to log key strokes out of my application. Suppose you are running your application on tray and you have opned notepad and typed something, these key strokes would be recorded by your application. I have coded this thing on my VB6 2 years ago. Since I am new to .net my first attempt was to convert this task in to VB.Net. Hope it clears. Thanks again for your reply.
 
O

One Handed Man [ OHM# ]

This article is a comprehensive discussion with code for what you are trying to achevie.

http://www.developer.com/net/net/article.php/11087_2193301_1
Regards - OHM#


(e-mail address removed)
Sorry OHM#, the answer is wrong. I guess you couldn't understand what I wanted to know. I have told you that I want to log key strokes out of my application. Suppose you are running your application on tray and you have opned notepad and typed something, these key strokes would be recorded by your application. I have coded this thing on my VB6 2 years ago. Since I am new to .net my first attempt was to convert this task in to VB.Net. Hope it clears. Thanks again for your reply.

Tanzim Saqib

Problemsetter, ACM Valladolid Online Judge, Spain.
Website: http://tanzim.tk


This posting is provided "AS IS" with no warranties.
 
T

Tanzim Saqib

KOOL! OHM#. That's what I wanted to konw! Kool article for me. And thanks for giving me such a nice link. Have a nice day! :D

Tanzim Saqib

Problemsetter, ACM Valladolid Online Judge, Spain.
Website: http://tanzim.tk


This posting is provided "AS IS" with no warranties.
 
T

Tanzim Saqib

Hey Cor

Thanks for the code. But you couldn't understand what I wanted to say. When people don't understand what I want to say, I confess my weakness that I couldn't make them understand. I am sorry. I wanted to know the replacement of GetAsyncKeyState() API. May be you are not familiar with the Win32 APIs so that you answered wrong. I wanted to trap key strokes out of my application by keyboard hooking which is very easy in VB6, but in .net seems difficult to me, although I am very new to .net, you know. And one thing >>...and now already using "always done" habbits.<< It happens when you are switching to the newer version of any programming language by comparing previous coding style and newer. I am a VB6 programmer who loves to work with Win32 APIs and now trying to learn ..net. I AM NOT USING "ALWAYS DONE" HABBITS. MIND IT.

Anyway, thanks for replying. Stay always sweet.
Tanzim Saqib

Problemsetter, ACM Valladolid Online Judge, Spain.
Website: http://tanzim.tk


This posting is provided "AS IS" with no warranties.
 
C

Cor

Hi Tanzim,

When I had written it, I understand that you did not want to trap the keystrokes from your application, but all keystrokes from the user whatever application or operating software he is using. Therefore my sample is not the right one.

Working with Net is with managed code or unmanaged code. For managed code is VB.net and C#.
For unmanaged code is C++.

You can use unmanaged code in VB.net, but the managed code toolbox is so full that you mostly can do all business problems with it (not all you will need some things to do with references to API's).

When you want to do things you want, C++ seems more the route to go. Probably for this VB.net has to much overhead to do it in an efficient way and then suddenly people are starting to tell that it is so slow.

But feel free to go the route your going.
It was just a free advice.

Cor
 
A

Armin Zingler

Manivannan said:
'AddressOf' expression cannot be converted to 'Integer' because
'Integer' is not a delegate type.



Handles clause requires a WithEvents variable defined in the
containing type or one of its base types.

If you wouldn' reply to a 5+ years old thread we could know what you want.


Armin
 
H

Herfried K. Wagner [MVP]

Manivannan said:
'AddressOf' expression cannot be converted to 'Integer' because 'Integer'
is not a delegate type.

F1 + 'Delegate' statement.
Handles clause requires a WithEvents variable defined in the containing
type or one of its base types.

F1 + 'WithEvents'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top