Nate Koppenhaver
Nate Koppenhaver

Reputation: 1702

Windows Forms problem in VB.NET

I'm making a very simple game application. The main form (Form1) has a single button on it labeled Button1. (very creative naming, eh?) The point of the application is to move the button in random directions across the form. My problem is that when I start debugging (I'm using Visual Studio 2010) the form never shows up, and when I open Windows Task Manager the name of my .exe never shows up in the process list. I was wondering if the code I'm using would have something to do with that. Here's my code:

Class Form1  
    Private Sub Form1_Load(...)  
       InitializeComponent()  
       While True
           MoveIt()  
       End While
    End Sub  
    Sub MoveIt()  
       Dim rand As Short  
       Randomize()  
       rand = (Rnd() * 5)  
       Select Case rand  
           Case 0  
               'move button up 5px  
               Button1.Top -= 5  
           Case 1  
               'move button up 5px  
               Button1.Top -= 5  
           Case 2  
               'move button left 5px  
               Button1.Left -= 5  
           Case 3  
               'move button right 5px  
               Button1.Left += 5  
           Case 4  
               'move button down 5px  
               Button1.Top += 5  
           Case 5  
               'move button down 5px  
               Button1.Top += 5  
       End Select  
    End Sub  
End Class          

Upvotes: 0

Views: 1352

Answers (4)

sajoshi
sajoshi

Reputation: 2763

If you want to move the button, you button's render or prerender... Using a infinite loop on Form load is what makes it hang...

Upvotes: 0

SLaks
SLaks

Reputation: 888077

You cannot write an infinite loop in WinForms.

If you do, your program will never get a chance to process messages, so it won't work.

Instead, you should use a Timer.

Upvotes: 1

william
william

Reputation: 7694

If it is in the debug mode, there won't be an exe in the task manager.

For the form problem, u can put the break point and debug slowly..

try put the form1 load below initialize components.

Upvotes: 0

MusiGenesis
MusiGenesis

Reputation: 75376

Yes, your code has something to do with this. You have an endless While loop in your form's Load event, which means the Load event never returns, which means your form basically never finishes loading.

Instead of calling this from your Load event, trigger the start of the random-button-movement process by using BeginInvoke from your Load event:

http://msdn.microsoft.com/en-us/library/a06c0dc2.aspx

Upvotes: 2

Related Questions