ThienZ Posted February 21, 2005 Posted February 21, 2005 Is there any better way to close a form with escape key than making a button "Close", setting it as CancelButton, and then write inside a code to close like this : private void buttonClose_Click(object sender, System.EventArgs e) { this.Close(); }? thx in advance :) Quote
AlexCode Posted February 22, 2005 Posted February 22, 2005 Sure there is... :p You have a solution that even works with no button. Every Form have a property called: KeyPreview. Setting this to TRUE will cause the Form to grab every imput from the user before it's processed to the actual control. This means that, even if the focus is on a TextBox, the KeyPress Event of the parent form will raise before the TextBox's do. So, on the KeyPress Event of the Form, just validate if the Key is ESCAPE, if so, close it! :) Alex :p Quote Software bugs are impossible to detect by anybody except the end user.
ThienZ Posted February 22, 2005 Author Posted February 22, 2005 wow.. thx AlexCode... that's even better! Usually i make an extra button and set the width to 0 :lol: Am i writing this right? cause this isn't working ^^;;; (I'm new to C# so i still need to get used to it...) private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if(e.Equals(Keys.Escape)) this.Close(); } Quote
AlexCode Posted February 22, 2005 Posted February 22, 2005 wow.. thx AlexCode... that's even better! Usually i make an extra button and set the width to 0 :lol: Am i writing this right? cause this isn't working ^^;;; (I'm new to C# so i still need to get used to it...) private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if(e.Equals(Keys.Escape)) this.Close(); } This will do, use the KeyDown event instead: Private Void Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) { If e.KeyValue = Keys.Escape Then this.Close(); } Quote Software bugs are impossible to detect by anybody except the end user.
ThienZ Posted February 22, 2005 Author Posted February 22, 2005 thx Alex, i tried to write like you did, but it didn't work... after some tries this code worked : private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if((Keys) e.KeyValue == Keys.Escape) this.Close(); } Should i cast the int into Keys or are there better ways? Because .net can't compare int and keys... thx :) ps: how do make a quote with "C# Code"? i tried [C# Code][/[C# Code]] but it won't do :p Quote
AlexCode Posted February 22, 2005 Posted February 22, 2005 Sorry for the bad translation, I was writing it from VB. And yeah, you must cast, specially because you seem to have Option Strict set to TRUE... right? Alex :p Quote Software bugs are impossible to detect by anybody except the end user.
ThienZ Posted February 22, 2005 Author Posted February 22, 2005 no problem Alex :) i don't know about Option Strict, maybe it's an Option in VB? I guess this option is set to TRUE by default in C#... Quote
thenerd Posted February 22, 2005 Posted February 22, 2005 i think it's [ cs ] Option strict is something you turn on at the top of your code you can type Option Strict on, or true I don't know for c#. It makes the compiler stricter, but it results in faster programs. Quote
Administrators PlausiblyDamp Posted February 22, 2005 Administrators Posted February 22, 2005 In C# all conversions / casts are strict - there is no way to turn it on or off. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
IngisKahn Posted February 23, 2005 Posted February 23, 2005 And thank the gods for that. :) Quote "Who is John Galt?"
AlexCode Posted February 23, 2005 Posted February 23, 2005 Yep... tho we're not forced to in VB, the truth is that we end up having to... :D Alex :p Quote Software bugs are impossible to detect by anybody except the end user.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.