title image


Smiley Re: Bestätigung vor Schließen einer UserForm?
Hallo NorbertHhmm.. hast Du das Gefühl mir hat's das geträumt ?!Die Lösung stammt ürsprünglich von Bill Coan.Intressanterweise wurde auch er, wenn auch etwas feiner als Du es tust, nach der Logik des Codes gefragt.Meiner Meinung nach ist seine Erklärung weit weg von Gut und Böse (wir nennen das birnenweich). Das einzige wirklich Gute an der Sache - und dies soll man schätzen - der Code scheint zu funktionieren.Einem geschenkten Gaul schaut man nichts ins Maul.Gerne stelle ich den kompletten Diskussionsfaden zur Verfügung: (microsoft.public.word.vba)Is there a method or an event by which you can detect that a form is beingclosed by user?Richard L. ChenHi Richard,Here's the help topic from the QueryClose event.The following code forces the user to click the UserForm's client area toclose it. If the user tries to use the Close box in the title bar, theCancel parameter is set to a nonzero value, preventing termination. However,if the user has clicked the client area, CloseMode has the value 1 and theUnload Me is completed.Private Sub UserForm_Activate() UserForm1.Caption = "You must Click me to kill me!"End SubPrivate Sub UserForm_Click() Unload MeEnd SubPrivate Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) 'Prevent user from closing with the Close box in the title bar. If CloseMode 1 Then Cancel = 1 UserForm1.Caption = "The Close box won't work! Click me!"End Sub--Bill CoanMicrosoft Word MVPNeenah, Wisconsin USAhttp://www.wordmacros.comHi Bill,Thanks for the information. It answered my question.As I am new to the VBA world, I would like to ask a more detailed question.If I want to use a Cancel button to emulate the Close box in the title bar,can I code something like:Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) If CloseMode = 0 Then CancelButton_ClickEnd SubI tried it and it seemed to work. Is there any possibility of evil sideeffects?Also, what would be the best policy to know which button (at least OK orCancel) has been clicked when you return from a form. A public variabledeclared in the current module, or ...?Richard L. ChenHi Richard,Command buttons have an attribute called Cancel. By default, this attributeis false for all buttons but you can set it to true for one button. Thiswill cause the button to act as though it has been clicked when the userhits escape.Your other question, regarding knowing which button was clicked to close theuserform, has at least three possible answers:1. Use a public variable, such as you contemplated. Most programmersdiscourage this approach because such variables are vulnerable to changesfrom other sources and are harder to track for the developer.2. Place all code into your userform module, so that the calling codedoesn't need to know how the userform was closed. This is my preferredmethod, although I admit that others think this is crazy. I'm so used to it,I guess, that it hasn't seemed to be a problem for me.3. Take advantage of the fact that a userform module is a class module andcreate a property that reflects how the userform was closed. I haven't donethis myself. David Foster, among others, has recommended it, and may weighin with some ideas for you. Check out class modules and the let, set, andget statements in the help file for more info.HTH--Bill CoanMicrosoft Word MVPNeenah, Wisconsin USAHi Bill,>Command buttons have an attribute called Cancel. By default, this attribute>is false for all buttons but you can set it to true for one button. This>will cause the button to act as though it has been clicked when the user>hits escape.This certainly is a buried treasure for me. Thanks.>Your other question, regarding knowing which button was clicked to closethe>userform, has at least three possible answers:>>1. Use a public variable, such as you contemplated. Most programmers>discourage this approach because such variables are vulnerable to changes>from other sources and are harder to track for the developer.Agreed.>2. Place all code into your userform module, so that the calling code>doesn't need to know how the userform was closed. This is my preferred>method, although I admit that others think this is crazy. I'm so used toit,>I guess, that it hasn't seemed to be a problem for me.If the form is merely for getting inputs from the user, leaving code in yourmain procedure should be a more reasonable choice. However, if your formdoes a lot of things, encapsulating everything inside it is certainly a moreobject-oriented solution.>3. Take advantage of the fact that a userform module is a class module and>create a property that reflects how the userform was closed. I haven't done>this myself. David Foster, among others, has recommended it, and may weigh>in with some ideas for you. Check out class modules and the let, set, and>get statements in the help file for more info.Who is this gentleman, David Foster? AltaVista returned more than 7,000pages and at least the first few pages do not show a programmer or guru.You are right. I can cook up a function to wrap the whole form and let itreturn the code I need.Richard L. Chen>Who is this gentleman, David Foster?He's that masked man over there, riding away in a cloud of dust, atop thatwhite stallion.

geschrieben von

Login

E-Mail:
  

Passwort:
  

Beitrag anfügen

Symbol:
 
 
 
 
 
 
 
 
 
 
 
 
 

Überschrift: