Results 1 to 8 of 8

Thread: Form closing (save changes?) code...? (RESOLVED)

  1. #1

    Thread Starter
    Addicted Member AlvaroF1's Avatar
    Join Date
    Sep 2002
    Location
    SP - Brazil
    Posts
    200

    Question Form closing (save changes?) code...? (RESOLVED)

    I'm using the following code:

    VB Code:
    1. Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    2.     If Text.EndsWith("*") = False Then Text = Text & "*"
    3. End Sub
    4.  
    5. Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
    6.     If Text.EndsWith("*") = False Then Text = Text & "*"
    7. End Sub
    8.  
    9. Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    10.     If Text.EndsWith("*") Then
    11.         Select Case MsgBox("Would you like to save the changes?", MsgBoxStyle.Question + MsgBoxStyle.YesNoCancel + MsgBoxStyle.DefaultButton3)
    12.             Case MsgBoxResult.Yes : SaveChangesSub
    13.             Case MsgBoxResult.Cancel : e.Cancel = True
    14.         End Select
    15.     End If
    16. End Sub
    The problems are:

    - When we press the control or Alt or CapsLock or Shift button the Form1_KeyDown is fired;
    - When we click on a button it isn't fired;
    - When we change a combo's item isn't fired.

    How to improve it? Is there a way to write a simple code or I'll have to add the "*" cade in every single control?


    Thanks.
    Last edited by AlvaroF1; Aug 20th, 2003 at 08:02 AM.

  2. #2
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    Yes, I hit the similar problems early on, my remedy is not elegant but it works, I took away control box by setting it to false, that way the only way that my users can close a form is by selecting a button on the form.

    As I said not elegant.

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You can set the KeyPreview property of the form to true so that all keypress type events are handled by the form first. Then you don't have to use every controls events.

  4. #4

    Thread Starter
    Addicted Member AlvaroF1's Avatar
    Join Date
    Sep 2002
    Location
    SP - Brazil
    Posts
    200
    You can set the KeyPreview property of the form to true so that all keypress type events are handled by the form first. Then you don't have to use every controls events.
    Thanks but I'm using this property. And as I said, the problems are:

    - When we press the control or Alt or CapsLock or Shift button the Form1_KeyDown is fired;
    - When we click on a button it isn't fired;
    - When we change a combo's item isn't fired.

  5. #5
    New Member sphudson's Avatar
    Join Date
    Aug 2003
    Location
    Planet Earth - Lincoln Nebraska
    Posts
    5
    Are you just trying to ask the user if they want to save every time they exit? If so the following works really well.

    Code:
        Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs)
            ExitApplication()
        End Sub
    
        Public Sub ExitApplication()
            ' Display a message box asking users if they 
            ' want to exit the application.
            If MessageBox.Show("Do you want to Save before Exiting?", "My Application", _
                  MessageBoxButtons.YesNo, MessageBoxIcon.Warning) _
                  = DialogResult.Yes Then
                SaveData()
            Else
                Application.Exit()
            End If
    
        End Sub
    Hope this helps!

    In the land of the blind the one eyed man is king.

  6. #6

    Thread Starter
    Addicted Member AlvaroF1's Avatar
    Join Date
    Sep 2002
    Location
    SP - Brazil
    Posts
    200
    Thanks, but I'm using the code above.

    What I'm looking for is some way to control this things:

    - When we press the control or Alt or CapsLock or Shift button the Form1_KeyDown is fired;
    - When we click on a button it isn't fired;
    - When we change a combo's item isn't fired.
    Last edited by AlvaroF1; Aug 19th, 2003 at 03:37 PM.

  7. #7

    Thread Starter
    Addicted Member AlvaroF1's Avatar
    Join Date
    Sep 2002
    Location
    SP - Brazil
    Posts
    200

  8. #8
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    is this what you are trying to achieve?...
    VB Code:
    1. [COLOR=BLUE]Private[/COLOR] [COLOR=BLUE]Sub[/COLOR] Form1_Load([COLOR=BLUE]ByVal[/COLOR] sender [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]Object[/COLOR], [COLOR=BLUE]ByVal[/COLOR] e [COLOR=BLUE]As[/COLOR] System.EventArgs) [COLOR=BLUE]Handles[/COLOR] [COLOR=BLUE]MyBase[/COLOR].Load
    2.         KeyPreview = [COLOR=BLUE]True
    3. [/COLOR]    [COLOR=BLUE]End[/COLOR] [COLOR=BLUE]Sub
    4.  
    5. [/COLOR]    [COLOR=BLUE]Protected[/COLOR] [COLOR=BLUE]Overrides[/COLOR] [COLOR=BLUE]Function[/COLOR] ProcessKeyPreview([COLOR=BLUE]ByRef[/COLOR] m [COLOR=BLUE]As[/COLOR] System.Windows.Forms.Message) [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]Boolean
    6. [/COLOR]        [COLOR=BLUE]Select[/COLOR] [COLOR=BLUE]Case[/COLOR] m.WParam.ToInt32
    7.             [COLOR=BLUE]Case[/COLOR] Keys.Shift
    8.                 MessageBox.Show("shift key clicked!")
    9.             [COLOR=BLUE]Case[/COLOR] Keys.CapsLock
    10.                 MessageBox.Show("caps lock clicked!")
    11.             [COLOR=BLUE]Case[/COLOR] Keys.Alt
    12.                 MessageBox.Show("Alt key clicked!")
    13.                 [COLOR=GREEN]'/// and so on for all the keys you want to handle.
    14. [/COLOR]        [COLOR=BLUE]End[/COLOR] [COLOR=BLUE]Select
    15.  
    16. [/COLOR]    [COLOR=BLUE]End[/COLOR] [COLOR=BLUE]Function[/COLOR]
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

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