Problems with SendKeys and System.Windows.Forms

G

Guest

Hi,

I am relatively inexperienced with VB.Net, but I am familiar with many
concepts from programming; I have some basic level of competence with C/C++
and Perl, but I just started using VB this morning.

I am trying to write an .exe file that is essentially a script. It will
open an application, send it some commands via the keyboard, then wait for
the results and close the application.

I know exactly how to do this in Perl, unfortunately, the Perl module for
sending the {ALT} key is broken and broadcasts the keystroke to all windows.

Now that you have the background, here is the code I have:

Imports Microsoft.VisualBasic
Imports System.Windows.Forms

Module Module1

Sub Main()
Dim ProcID As Integer
' Start the Calculator application, and store the process id.
ProcID = Shell("CALC.EXE", AppWinStyle.NormalFocus)
' Activate the Calculator application.
AppActivate(ProcID)
SendKeys.SendWait("22")
SendKeys.SendWait("*")
SendKeys.SendWait("44")
SendKeys.SendWait("=")
End Sub
End Module

You'll notice I essentially copy and pasted the example from MSDN's
documentation. Here's the problem I have; when I go to build I get the
following error messages:

1.
C:\Documents and Settings\Administrator\My Documents\Visual Studio
Projects\WinScripting\Module1.vb(2): Namespace or type 'Forms' for the
Imports 'System.Windows.Forms' cannot be found.

2.
C:\Documents and Settings\Administrator\My Documents\Visual Studio
Projects\WinScripting\Module1.vb(12): Name 'SendKeys' is not declared.

I actually get Error #2 for lines 12-16, but that is hardly a surprise.

From what I understand, the problem is that in my header, I am not
successfully using the System.Windows.Forms namespace, hence the SendKeys
keyword is meaningless garbage to the compiler. The IDE does not seem to
acknowledge that such a library exists, which is terribly frustrating.

I have the following programs installed:
MS Visual Studio .Net Enterprise Architect 2003
MSDN Library October 2004
MS Visual J# .Net Redistributable Package 1.1
Microsoft Data Access Componenets

Anyway, if someone could give me a clue how to get everything to work (which
libraries I need, how to import them etc.) I would really appreciate it.

Thanks in advance,

David Kanter
 
K

Ken Tucker [MVP]

Hi,

Add a reference to system.windows.forms.dll in your app.

Ken
-------------
Hi,

I am relatively inexperienced with VB.Net, but I am familiar with many
concepts from programming; I have some basic level of competence with C/C++
and Perl, but I just started using VB this morning.

I am trying to write an .exe file that is essentially a script. It will
open an application, send it some commands via the keyboard, then wait for
the results and close the application.

I know exactly how to do this in Perl, unfortunately, the Perl module for
sending the {ALT} key is broken and broadcasts the keystroke to all windows.

Now that you have the background, here is the code I have:

Imports Microsoft.VisualBasic
Imports System.Windows.Forms

Module Module1

Sub Main()
Dim ProcID As Integer
' Start the Calculator application, and store the process id.
ProcID = Shell("CALC.EXE", AppWinStyle.NormalFocus)
' Activate the Calculator application.
AppActivate(ProcID)
SendKeys.SendWait("22")
SendKeys.SendWait("*")
SendKeys.SendWait("44")
SendKeys.SendWait("=")
End Sub
End Module

You'll notice I essentially copy and pasted the example from MSDN's
documentation. Here's the problem I have; when I go to build I get the
following error messages:

1.
C:\Documents and Settings\Administrator\My Documents\Visual Studio
Projects\WinScripting\Module1.vb(2): Namespace or type 'Forms' for the
Imports 'System.Windows.Forms' cannot be found.

2.
C:\Documents and Settings\Administrator\My Documents\Visual Studio
Projects\WinScripting\Module1.vb(12): Name 'SendKeys' is not declared.

I actually get Error #2 for lines 12-16, but that is hardly a surprise.

From what I understand, the problem is that in my header, I am not
successfully using the System.Windows.Forms namespace, hence the SendKeys
keyword is meaningless garbage to the compiler. The IDE does not seem to
acknowledge that such a library exists, which is terribly frustrating.

I have the following programs installed:
MS Visual Studio .Net Enterprise Architect 2003
MSDN Library October 2004
MS Visual J# .Net Redistributable Package 1.1
Microsoft Data Access Componenets

Anyway, if someone could give me a clue how to get everything to work (which
libraries I need, how to import them etc.) I would really appreciate it.

Thanks in advance,

David Kanter
 

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