PC Review


Reply
Thread Tools Rate Thread

Cannot convert code from 6 to .NET

 
 
Tanzim Saqib
Guest
Posts: n/a
 
      13th Dec 2003
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.



 
Reply With Quote
 
 
 
 
Cor
Guest
Posts: n/a
 
      13th Dec 2003
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
 
Reply With Quote
 
One Handed Man [ OHM# ]
Guest
Posts: n/a
 
      13th Dec 2003
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 Removed)
"Tanzim Saqib" <(E-Mail Removed)> wrote in message news:(E-Mail 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.


 
Reply With Quote
 
Tanzim Saqib
Guest
Posts: n/a
 
      13th Dec 2003
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.



 
Reply With Quote
 
Tanzim Saqib
Guest
Posts: n/a
 
      13th Dec 2003
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.



 
Reply With Quote
 
One Handed Man [ OHM# ]
Guest
Posts: n/a
 
      13th Dec 2003
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 Removed)
"Tanzim Saqib" <(E-Mail Removed)> wrote in message news:(E-Mail 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.


 
Reply With Quote
 
Tanzim Saqib
Guest
Posts: n/a
 
      13th Dec 2003
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.



 
Reply With Quote
 
One Handed Man [ OHM# ]
Guest
Posts: n/a
 
      13th Dec 2003
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 Removed)
"Tanzim Saqib" <(E-Mail Removed)> wrote in message news:u%(E-Mail 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.


 
Reply With Quote
 
Tanzim Saqib
Guest
Posts: n/a
 
      14th Dec 2003
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.



 
Reply With Quote
 
Cor
Guest
Posts: n/a
 
      14th Dec 2003
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linq to XML--Are there code examples that make Linq as easy as SQL? Or how can I convert ths simple pseudo code into real code? Reece Microsoft C# .NET 4 10th Dec 2008 03:13 AM
Excel code convert to Access code - Concat & eliminate duplicates italia Microsoft Access Queries 1 12th Sep 2006 07:22 PM
Excel code convert to Access code - Concat & eliminate duplicates italia Microsoft Access 2 12th Sep 2006 06:13 PM
Excel code convert to Access code - Concat & eliminate duplicates italia Microsoft Access Macros 0 12th Sep 2006 05:04 PM
Excel code convert to Access code - Concat & eliminate duplicates italia Microsoft Excel Programming 1 12th Sep 2006 12:14 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:02 AM.