ss4steve

I have just finished writing my first VB Express program (Sub Search). I often found myself wanting to do an INKEY$ function as I used to do in Quick Basic.

I want the program to pause, and wait for me to hit a keyboard key... then to analyze that keystroke, and branch-off to various points.

I have seen references to such a capability (keydown & others) but have not been able to decode the directions to the point that I can apply it to my program.




Re: Visual Basic Express Edition VB Express (INKEY$) function.

ReneeC

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

'The following properties must be set thusly:

' Textbox1.Multiline = True

' Textbox1.AcceptsReturn = True

Select Case e.keychar

case Chr(Keys.A)
' Code to be executed goes here.
e.Handled = True

case Chr(Keys.b)
' Code to be executed goes here.
e.Handled = True

case Chr(Keys.C)
' Code to be executed goes here.
e.Handled = True

Case Else
e.Handled = False

End Sub






Re: Visual Basic Express Edition VB Express (INKEY$) function.

DMan1

ss4steve wrote:
I want the program to pause, and wait for me to hit a keyboard key... then to analyze that keystroke, and branch-off to various points.

Renee shows you how to do something when a key is pressed in a textbox...to explain a little further...

Quick Basic is a procedural languange and .NET is an event driven language....the difference being that with a procedural language you had to start at point "A" and then make accomodations(procedures) for every possible action that the user could do in your application...for example (like you suggest) if you wanted to analyze a keystroke you would have to pause the program and wait for a keystoke to be returned....in an event driven language you setup the application to start at point "A" and then program for the events that will take place in the application...in other words in an event driven app...once you start at point "A"....nothing happens in code until the user (or other code...such as a timer) causes an event to fire...and you the programmer programs that event!!! The plain and simple is: Event-driven applications execute code in response to an event.

http://msdn2.microsoft.com/en-us/library/1h12f09z.aspx

Hope that Helps!!






Re: Visual Basic Express Edition VB Express (INKEY$) function.

ReneeC

Wonderful clarification Dman !!!!!!!!!!!






Re: Visual Basic Express Edition VB Express (INKEY$) function.

Solitaire

In order to emulate QBasic commands, you might want to try a Console application instead of a Windows Form.


Instead Instead of
PRINT "Text goes here"
substitute:
Console.WriteLine("Text goes here")

Instead of:
PRINT "The number is "; num
substitute:
Console.WriteLine("The number is {0}", num)
or
Console.WriteLine("The number is " & num)

Instead of:
INPUT "Enter a number:  ", snum$
num = VAL(snum$)
substitute:
snum = Console.ReadLine()
Integer.TryParse(snum, num)

Console.ReadLine() will pause the program execution and wait for the user to type something and press Enter.  You can also use Console.ReadLine() without an assignment variable.  It will simply wait for the user to press the Enter key.

In order to capture a single keypress, you can do this:
ans = Console.ReadKey().KeyChar
This will pause the program execution, wait for the user to press any key,  and print the user's keypress to the screen.  Then you can analyse the value of ans.

In order not to print it:
ans = Console.ReadKey(True).KeyChar

If you don't need the return value of the keypress but just want the program to pause until any key is pressed, do this:
Console.ReadKey(True).KeyChar

To emulate the INKEY$ command:
        Do
            'Console.Write("Esc.....")  'or other statements to execute
            If Console.KeyAvailable() Then
                If Console.ReadKey(True).KeyChar = Chr(27) Then Exit Do
            End If
        Loop
That block will continue to execute the statements until the Esc key is pressed.

 






Re: Visual Basic Express Edition VB Express (INKEY$) function.

ReneeC

Why on earth would someone want to emulate QuickBasic in Dot Net It just so happens that I have a copy and it evens runs on Vista!






Re: Visual Basic Express Edition VB Express (INKEY$) function.

Solitaire

The point is, VB.NET is very versatile, and the 2005 edition includes enhanced Console features.

I teach a college course in programming for beginners and we are currently using VB.NET for Console applications. The GUI gets in the way of programming concepts. If a beginner can learn the elementary logic and structures, he can then go on to learn programming in GUI and in other languages.

By the way, Vista does not support full-screen mode for DOS programs, and will not run QBasic in anything other than SCREEN 0 mode. It also does not print the colors between 16-31 correctly.

Otherwise, we would still be using QBasic instead of VB.NET for the introductory course.






Re: Visual Basic Express Edition VB Express (INKEY$) function.

moimoiiomiom

Where can I find a full list of names for keys like the windows key and CTRL key

Can anyone help






Re: Visual Basic Express Edition VB Express (INKEY$) function.

Spidermans_DarkSide - VSIP

Solitaire wrote:

The point is, VB.NET is very versatile, and the 2005 edition includes enhanced Console features.

I teach a college course in programming for beginners and we are currently using VB.NET for Console applications. The GUI gets in the way of programming concepts. If a beginner can learn the elementary logic and structures, he can then go on to learn programming in GUI and in other languages.

By the way, Vista does not support full-screen mode for DOS programs, and will not run QBasic in anything other than SCREEN 0 mode. It also does not print the colors between 16-31 correctly.

Otherwise, we would still be using QBasic instead of VB.NET for the introductory course.

Hi Solitaire,

Do you introduce beginners to GUI based applications with say one button and one textox

I was just wondering.

I've not seen or ran QBASIC in years!! ( Showing my age now. )

Regards,

S_DS






Re: Visual Basic Express Edition VB Express (INKEY$) function.

Spidermans_DarkSide - VSIP

Solitaire wrote:

In order to emulate QBasic commands, you might want to try a Console application instead of a Windows Form.

Code Snippet


'Instead Instead of
'PRINT "Text goes here"
'substitute:

Console.WriteLine("Text goes here")

'Instead of:
'PRINT "The number is "; num
'substitute:
Console.WriteLine("The number is {0}", num)
'or
Console.WriteLine("The number is " & num)

'Instead of:
'INPUT "Enter a number: ", snum$
'num = VAL(snum$)
'substitute:
snum = Console.ReadLine()
Integer.TryParse(snum, num)

Code Snippet
Console.ReadLine()

will pause the program execution and wait for the user to type something and press Enter. You can also use

Code Snippet
Console.ReadLine()

without an assignment variable. It will simply wait for the user to press the Enter key.

In order to capture a single keypress, you can do this:

Code Snippet


ans = Console.ReadKey().KeyChar

This will pause the program execution, wait for the user to press any key, and print the user's keypress to the screen. Then you can analyse the value of ans.

In order not to print it:


Code Snippet

ans = Console.ReadKey(True).KeyChar

If you don't need the return value of the keypress but just want the program to pause until any key is pressed, do this:

Code Snippet


Console.ReadKey(True).KeyChar

'To emulate the INKEY$ command:
Do
'Console.Write("Esc.....") 'or other statements to execute
If Console.KeyAvailable() Then
If Console.ReadKey(True).KeyChar = Chr(27) Then Exit Do
End If
Loop

That block will continue to execute the statements until the Esc key is pressed.

Hi Solitaire,

Just thought i would re-post your post above as i think it looks easier to read as code-snippets.

Regards,

S_DS






Re: Visual Basic Express Edition VB Express (INKEY$) function.

Solitaire

The computer science ccordinator wants me to stay away from the GUI until the end of the course, but I managed to sneak in a few of them. I introduce the Windows forms, translating the Console code I use for the basic constructs, so the students can see how it looks side-by-side.

Right now I'm teaching a Visual Basic course to members of my users' group. The emphasis is on programming and I show them a few console apps, but I'm also giving them a lot of hands-on practice with Windows Forms.






Re: Visual Basic Express Edition VB Express (INKEY$) function.

Spidermans_DarkSide - VSIP

Solitaire wrote:

The computer science ccordinator wants me to stay away from the GUI until the end of the course, but I managed to sneak in a few of them. I introduce the Windows forms, translating the Console code I use for the basic constructs, so the students can see how it looks side-by-side.

Right now I'm teaching a Visual Basic course to members of my users' group. The emphasis is on programming and I show them a few console apps, but I'm also giving them a lot of hands-on practice with Windows Forms.

Hi Solitaire,

Keep up the good work.

See this thread for another "trick" i've recently discovered.>>

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1675868&SiteID=1

Regards,

S_DS