How do I do this in vb .Net?

  • Thread starter Thread starter piddie
  • Start date Start date
P

piddie

Below is in Delphi...

procedure TMyForm.AppIdle(Sender: TObject; var Done: Boolean);
begin
if OkToDoThings = true then begin
//do things
end;
end;

What this does is make a procedure for running things
in the background. It's very fast and is used in games.

How do I do this in VB .Net?
 
This will do what you want

Public Sub Application_Idle(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Application.Idle

If OkToDoThings Then
' do things
End If

End Sub
 
I don't know delphi, but in VB.NET you wont need the begin and end
statements, and you don't need the semicolon after the first end. I believe
the signature would be something like

AccessModifier(Public|Private|Friend etc) Sub TMyForm.AppIdle(ByVal TObject
as Object, ByVal Done as Boolean)

If there's no return type, use Sub, otherwise use Function and then use an
As ReturnType at the end of the declaration.


HTH,

Bill
 
* piddie said:
Below is in Delphi...

procedure TMyForm.AppIdle(Sender: TObject; var Done: Boolean);
begin
if OkToDoThings = true then begin
//do things
end;
end;

What this does is make a procedure for running things
in the background. It's very fast and is used in games.

You may want to add a handler to 'Application.Idle':

<http://msdn.microsoft.com/library/e...stemwindowsformsapplicationclassidletopic.asp>

You can use 'AddHandler' to add, and 'RemoveHandler' to remove the
handler.
 
Rob said:
This will do what you want

Public Sub Application_Idle(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Application.Idle

If OkToDoThings Then
' do things
End If

End Sub

I get an error "WithEvents" required.
 
Piddie,

* piddie said:
I get an error "WithEvents" required.

Use this code instead:

\\\
Public Class AppMain
Public Shared Sub Main()
AddHandler Application.Idle, AddressOf Application_Idle
Application.Run(New Form1())
RemoveHandler Application.Idle, AddressOf Application_Idle
End Sub

Public Shared Sub Application_Idle(ByVal sender As Object, ByVal e As EventArgs)
 
Herfried said:
Piddie,




Use this code instead:

\\\
Public Class AppMain
Public Shared Sub Main()
AddHandler Application.Idle, AddressOf Application_Idle
Application.Run(New Form1())
RemoveHandler Application.Idle, AddressOf Application_Idle
End Sub

Public Shared Sub Application_Idle(ByVal sender As Object, ByVal e As EventArgs)
.
.
.
End Sub
End Class
///

Set 'AppMain' as the project's startup object.

Ok, so how do I reach the controls on Form1?
Form1.label1.text = "hello" doesn't work, I get
"Reference to a non-shared member requires an object
reference."
 
* nobody <[email protected]> scripsit:
\\\
Public Class AppMain
Private Shared m_TheMainForm As Form1

Public Shared Sub Main()
AddHandler Application.Idle, AddressOf Application_Idle
m_TheMainForm = New Form1()
Application.Run(m_TheMainForm)
RemoveHandler Application.Idle, AddressOf Application_Idle
End Sub

Public Shared Sub Application_Idle(ByVal sender As Object, ByVal e As EventArgs)
 
Herfried said:
* nobody <[email protected]> scripsit:
\\\
Public Class AppMain
Private Shared m_TheMainForm As Form1

Public Shared Sub Main()
AddHandler Application.Idle, AddressOf Application_Idle
m_TheMainForm = New Form1()
Application.Run(m_TheMainForm)
RemoveHandler Application.Idle, AddressOf Application_Idle
End Sub

Public Shared Sub Application_Idle(ByVal sender As Object, ByVal e As EventArgs)
.
.
.
End Sub
End Class
///




See adapted code above, you can access controly by typing
'm_TheMainForm' inside 'Application_Idle'.

It works great now. I'm getting lightning fast, flicker
free pictureboxes running across the screen.

But why was a new class created for the Application_Idle
event? Couldn't these lines....

AddHandler Application.Idle, AddressOf Application_Idle
RemoveHandler Application.Idle, AddressOf Application_Idle

....be placed in the Form1_Load event? Then the Application_Idle
event placed within the Form1 Class?
 
Ooops, thanks for picking up my oversight!

Rob


Herfried K. Wagner said:
Piddie,



Use this code instead:

\\\
Public Class AppMain
Public Shared Sub Main()
AddHandler Application.Idle, AddressOf Application_Idle
Application.Run(New Form1())
RemoveHandler Application.Idle, AddressOf Application_Idle
End Sub

Public Shared Sub Application_Idle(ByVal sender As Object, ByVal e As EventArgs)
.
.
.
End Sub
End Class
///

Set 'AppMain' as the project's startup object.
 
* nobody said:
It works great now. I'm getting lightning fast, flicker
free pictureboxes running across the screen.

But why was a new class created for the Application_Idle
event? Couldn't these lines....

AddHandler Application.Idle, AddressOf Application_Idle
RemoveHandler Application.Idle, AddressOf Application_Idle

...be placed in the Form1_Load event? Then the Application_Idle
event placed within the Form1 Class?

That will work too, but I prefer to put all application-related stuff
into a separate class...
 
Back
Top