473,396 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Cannot convert code from 6 to .NET

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.

Nov 20 '05 #1
15 2778
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#
On**********@BTInternet.com
"Tanzim Saqib" <tz****@hotmail.com> wrote in message news:uS**************@TK2MSFTNGP09.phx.gbl...
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.


Nov 20 '05 #2
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
Nov 20 '05 #3
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.

Nov 20 '05 #4
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.

Nov 20 '05 #5
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#
On**********@BTInternet.com
"Tanzim Saqib" <tz****@hotmail.com> wrote in message news:eY**************@TK2MSFTNGP12.phx.gbl...
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.


Nov 20 '05 #6
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.

Nov 20 '05 #7
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#
On**********@BTInternet.com
"Tanzim Saqib" <tz****@hotmail.com> wrote in message news:u%****************@TK2MSFTNGP10.phx.gbl...
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.


Nov 20 '05 #8
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.

Nov 20 '05 #9
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.

Nov 20 '05 #10
This article is a comprehensive discussion with code for what you are trying to achevie.

http://www.developer.com/net/net/art...1087_2193301_1
Regards - OHM#
On**********@BTInternet.com
"Tanzim Saqib" <tz****@hotmail.com> wrote in message news:OQ**************@TK2MSFTNGP11.phx.gbl...
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.


Nov 20 '05 #11
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.

Nov 20 '05 #12
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.

Nov 20 '05 #13
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
Nov 20 '05 #14
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
Nov 20 '05 #15
Hell Cor
It was just a free advice.<< No. This is not just a free advice, seems exclusive for me at this moment. :D Thanks for help. 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.

Nov 20 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: BashiraInTrouble | last post by:
Hi Friends, I have tried almost everything but I cant seem to shrink the transaction log. Executing DBCC SQLPERF(LOGSPACE) gives me this info: Database Log Size (MB) Log Space Used (%) ...
22
by: Christoph Boget | last post by:
I am getting an error (a few among many) for the following lines of code: retval.BrokerName = (( curRow == System.DBNull.Value ) ? SqlString.Null : (string)curRow ); retval.BrokerGroupId = ((...
3
by: Laura T. | last post by:
The following code is driving me so crazy, that I'm thinking it's a "feature" or a.. ....dare I say it... a BUG. I'm using VS 2003. I've created a new value type (struct) called MyInt. MyInt has...
6
by: Tim Cartwright | last post by:
I have a page that has the login control on it, nothing else. This page inherits from a master page, neither page has any code in it. This page works perfectly when running on the WebDev debug web...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
2
by: Christophe | last post by:
class A {} class B {} interface MyInterface { void method(A a); void method(B b); }
7
by: groups | last post by:
This is my first foray into writing a generic method and maybe I've bitten off more than I can chew. My intent is to have a generic method that accepts a value name and that value will be...
6
by: John | last post by:
The following code: int test = 1; bool isTrue = (bool)test; results in a compiler error: Cannot convert type 'int' to 'bool' wtf, any ideas on how to work around this?
2
by: slizorn | last post by:
error is as stated in the topic above: error C2440: '=' : cannot convert from 'char *' to 'char' code is below void handleOneLine(string string1) { char * cstr, *p; int counter; string...
1
by: Cezus | last post by:
Hello, I cannot convert the following query in the dataset to a string. It says it cannot get more then 2034 chars long... the string just ends at 2034 characters... this is where it goes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.