CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2003
    Location
    Tokyo, Japan
    Posts
    93

    loops and Close() error

    Hello guys

    I am trying to close a form from a loop, but for some reason it doesn't work

    basically I use the this.Close() from inside the loop and the loop continues, why??

    Thanks
    Favourite Quotes

    "In the first place, God made idiots. That was for practice. Then he made developers."

    "Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done."

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: loops and Close() error

    The best way to do this should be to break from the loop and then close the Form.

  3. #3
    Join Date
    Feb 2003
    Location
    Tokyo, Japan
    Posts
    93

    Re: loops and Close() error

    Hello again Shuja

    well, I break from the loop and then I return to the main method with a false, in the main method I check the return value and if is false then this.close()

    but even so, the application continues running, :S

    I am trully puzzle about this.

    Why is that happening? isn't this.Close() meant to terminate the application by closing the main form?

    this is the code
    Code:
    private void btnStart_Click(object sender, System.EventArgs e)
    		{
    			if(!getNumbers())
    				this.Close();
          
    			displayComparison();
    
    
    		}
    Thanks
    Favourite Quotes

    "In the first place, God made idiots. That was for practice. Then he made developers."

    "Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done."

  4. #4
    Join Date
    Feb 2003
    Location
    Tokyo, Japan
    Posts
    93

    Re: loops and Close() error

    Hi,

    I have sorted, basically the problem was that apparently, it doesn't just close the form, first of all executes till the end of the method, why is that?

    well, what I did was to include the rest of the method on the if statement

    Code:
    		private void btnStart_Click(object sender, System.EventArgs e)
    		{
    			if(!getNumbers())
    				this.Close();
                            else
    				displayComparison();
    		}
    Although I have fixed, I would like to know why is acting like that, and what methods could be used instead of Close() for terminating applications.

    Thanks
    Favourite Quotes

    "In the first place, God made idiots. That was for practice. Then he made developers."

    "Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done."

  5. #5
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    Re: loops and Close() error

    Place a return statement immediately after the call to Close if it may not be the last statement in the method.
    Tutorials: Home & Learn | Start VB.NET | Learn VB.NET | C# Station | GotDotNet | Games in VB.NET 101 Samples: 2002 | 2003 | 2005 | More .NET 2.0 (VB.NET, C#) Articles: VB.NET | C# | ASP.NET | MoreFree Components: WFC | XPCC | ElementsEx | VBPP | Mentalis | ADO.NET/MySQL | VisualStyles | Charting (NPlot, ZedGraph) | iTextSharp (PDF) | SDF (CF) ● Free Literature: VB 2005 (eBook) | VB6 to VB.NET (eBook) | MSDN Magazine (CHM format) ● Bookmarks: MSDN | WinForms .NET | ASP.NET | WinForms FAQ | WebForms FAQ | GotDotNet | Code Project | DevBuzz (CF) ● Code Converter: C#/VB.NET | VB.NET/C# | VS 2005 add-in

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured