开发者

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 Stu开发者_JAVA百科dio 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          


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


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.


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.


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜