Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hmmm

 

hadle the event form.keypress or form.keydown and check if its the escape key, just make sure that any long loops you have, have application.doevents in them. and when you exit the program (escape key) make sure to set the exit conditions for all your loops, example if the loop is

do while running

.....

loop

then under keypress event

if e.keyvalue = keys.esc.esc then

running = false

exit

end if

Posted (edited)

Maybe this little example helps ? the private sub could be also _KeyPress or _KeyDown.

 

What kind of running code would you like to halt ?

 

[Vb]Private Sub MyLovelyClass_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp

 

If e.KeyCode = Keys.Escape Then

 

'Put the running "code" stopper inside here

 

End If

 

End Sub[/code]

Edited by DR00ME

"Everything should be made as simple as possible, but not simpler."

"It's not that I'm so smart , it's just that I stay with problems longer ."

- Albert Einstein

Posted (edited)

I'm not sure but this is what I came up fast....

 

 


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

           If e.KeyCode = Keys.Escape Then
    
               MyBooleanVariable = False

           End If

       End Sub


Do While (MyBooleanVariable=True)

  'This is your loop

Enddo

 

ack... I think this doesnt work because it is busy in the loop maybe... damn my head is mixed up because I used to code in Atmel and it was kind of statemachine coding...

Edited by DR00ME

"Everything should be made as simple as possible, but not simpler."

"It's not that I'm so smart , it's just that I stay with problems longer ."

- Albert Einstein

Posted

I think this will work but i need to have it work from a button.

This is what i was using to goto the old routine but it no longer functions?

 

Private Sub Update1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Update1.Click

MyLovelyClass_KeyUp()

End Sub

phillip Restall
Posted

Umm, I'm not sure if I understand what you want but erm....

If you press ESC it will press btnStop on your form...

 

 

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

           If e.KeyCode = Keys.Escape Then
    
                btnStop.PerformClick()

           End If

       End Sub

 

how is this ?

"Everything should be made as simple as possible, but not simpler."

"It's not that I'm so smart , it's just that I stay with problems longer ."

- Albert Einstein

Posted

Maybe im better of explaining what im doing again.

 

I have a button that when clicked goes to my SUB RUNMYCODE

 

Using RUNMYCODE()

 

I then have an Array of names

 

I use

 

For each myname in names

 

'Write a macro file and execute it in a Software package

 

Next myname

 

 

I want to be able to exit the Code by using the escape key

in the for each loop because the operation can take up to an hour.

phillip Restall
Posted (edited)

Dim flag as boolean = false

For each myname in names

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

           If e.KeyCode = Keys.Escape Then
    
                flag = true

           End If

       End Sub

if flag = true then

  Exit

end if


Next myname

 

 

lol try this.... I have no idea if it works.. now I wonder if it exits the IF END IF or the for loop... ack...

 

You could also try to put the private sub outside of the for each loop....

 

I got an new idea... how about if you cause an exception and exit the code with it ?

Edited by DR00ME

"Everything should be made as simple as possible, but not simpler."

"It's not that I'm so smart , it's just that I stay with problems longer ."

- Albert Einstein

Posted (edited)

Dim lol as byte '0-255


Try

For Each myname In names

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

           If e.KeyCode = Keys.Escape Then
    
                lol = 1337 'causes an overflow exception

           End If

       End Sub




Next myname

catch ex as exception

end try

 

I think my ideas are mad atm... can someone help maybe ?

Edited by DR00ME

"Everything should be made as simple as possible, but not simpler."

"It's not that I'm so smart , it's just that I stay with problems longer ."

- Albert Einstein

Posted

I looked through some of my old vb6 code and found thid

 

Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

Function GetPressedKey() As String

 

For cnt = 32 To 180

'Get the keystate of a specified key

If GetAsyncKeyState(cnt) <> 0 Then

GetPressedKey = Chr$(cnt)

Exit For

End If

Next cnt

End Function

 

Sub Main()

 

KeyPressed = GetPressedKey

If KeyPressed <> "" Then

Select Case KeyPressed

Case "Q", "q"

GoTo Quit

end select

 

I cant get it to work in vb.net though

phillip Restall
Posted

Have you considered to add the "application.doevents" line to your loop ?

 

Possibly your application grabs up 100% CPU power, and no other processes can do anything, especially the system. This will result in a "deaf" program, because KeyPress events will takes ages until they are handled.

.nerd

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...