|
-
Jul 28th, 2003, 05:57 PM
#1
Thread Starter
Addicted Member
Form closing (save changes?) code...? (RESOLVED)
I'm using the following code:
VB Code:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If Text.EndsWith("*") = False Then Text = Text & "*"
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
If Text.EndsWith("*") = False Then Text = Text & "*"
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If Text.EndsWith("*") Then
Select Case MsgBox("Would you like to save the changes?", MsgBoxStyle.Question + MsgBoxStyle.YesNoCancel + MsgBoxStyle.DefaultButton3)
Case MsgBoxResult.Yes : SaveChangesSub
Case MsgBoxResult.Cancel : e.Cancel = True
End Select
End If
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.
-
Jul 29th, 2003, 03:16 PM
#2
Hyperactive Member
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.
-
Jul 29th, 2003, 03:38 PM
#3
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.
-
Aug 7th, 2003, 07:17 AM
#4
Thread Starter
Addicted Member
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.
-
Aug 8th, 2003, 10:34 AM
#5
New Member
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.
-
Aug 10th, 2003, 08:17 PM
#6
Thread Starter
Addicted Member
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.
-
Aug 19th, 2003, 03:38 PM
#7
Thread Starter
Addicted Member
-
Aug 19th, 2003, 05:13 PM
#8
is this what you are trying to achieve?...
VB Code:
[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
KeyPreview = [COLOR=BLUE]True
[/COLOR] [COLOR=BLUE]End[/COLOR] [COLOR=BLUE]Sub
[/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
[/COLOR] [COLOR=BLUE]Select[/COLOR] [COLOR=BLUE]Case[/COLOR] m.WParam.ToInt32
[COLOR=BLUE]Case[/COLOR] Keys.Shift
MessageBox.Show("shift key clicked!")
[COLOR=BLUE]Case[/COLOR] Keys.CapsLock
MessageBox.Show("caps lock clicked!")
[COLOR=BLUE]Case[/COLOR] Keys.Alt
MessageBox.Show("Alt key clicked!")
[COLOR=GREEN]'/// and so on for all the keys you want to handle.
[/COLOR] [COLOR=BLUE]End[/COLOR] [COLOR=BLUE]Select
[/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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|