Results 1 to 8 of 8

Thread: Close form

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    78

    Resolved Close form

    Hi all i have a form that the user must enter some important information and the only way of leaving of that form is to enter the correct info.

    By mistake i close that form from the X on the upper right side realizing that my code restrictions on that form was not enough.

    How can i disable close (X) of that form?

    I checked the form properties but i couldn`t find anything about that just to disable maximize and minimize....

    Any ideas???

  2. #2
    Addicted Member
    Join Date
    Jun 2009
    Posts
    245

    Re: Close form

    You might want to try this out:

    vb.net Code:
    1. Private Sub closingForm(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
    2.         If e.CloseReason = CloseReason.UserClosing Then
    3.             e.Cancel = True
    4.         End If
    5.     End Sub

    If the user is trying to close the form, it will cancel the close operation. I think you still have the opportunity to close it using CTRL+ALT+DEL or something, wich might be a good idea if you by any means don't know what to type in, clicked wrong or something...

  3. #3
    Addicted Member
    Join Date
    Oct 2009
    Posts
    212

    Re: Close form

    You could override wndproc and handle all all these events manually.
    Have you tried Google?

  4. #4
    Addicted Member
    Join Date
    Oct 2009
    Location
    Clive, IA in America!!!!
    Posts
    204

    Re: Close form

    Something else to consider is the Task Manager closing the form... I tried using e.Cancel without the if-then, but Task Manager still closes it.

  5. #5
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Close form

    Why not simply notify the user that the information he entered is incorrect, then give him the option of closing the form, and if he decides to close anyway, you simply don't process the information.

    I wouldn't like it if I couldn't close a form until I enter the correct information. What if I don't know it? I'll be stuck there forever!

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Close form

    I agree with Nick, this sounds like bad design to me. If you want to show a form and only execute some code if the user enters valid input and clicks OK then either do that validation etc in the OK button's click event handler or have the form return DialogResult.OK (or whatever its called) and then have the calling form check to see if DialogResult.OK was returned and if it was then do the code in the calling form.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Close form

    An example is an OpenFileDialog. If the user doesn't want to select a file and presses Cancel, or closes the form, then the DialogResult will not be OK. You then only process the information from the DialogForm if the DialogResult is OK:
    Code:
    If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
       'do something with the result
    Else
       'do nothing, or do something that makes sense like stopping a service or whatever you need
    End If
    You can do the same thing with regular forms. It's easiest to add a new DialogForm instead of a regular Form (although the DialogForm is just a template you can just as easily create manually), and you'll get the same behaviour.

  8. #8
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    540

    Re: Close form

    Code:
    How can i disable close (X) of that form?
    While I would definitely listen to what everyone here has to say, if you want to remove the X from your form just set the control box property to false in the designer. Note this will also remove the minimize and maximize buttons as well.
    Where I'm from we only have one bit of advice for new comers: "If you hear banjos, turn and run".


    VS 2008 .NetFW 2.0

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