Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a Cancel Button 2

Status
Not open for further replies.

pds

Programmer
Jul 2, 2001
9
US
I have two forms, a main one and a sub one. You click a button on the main to launch the sub. Now, on the sub is a textbox and two command buttons, OK and Cancel. When I type "blah" into the textbox and hit OK, the sub form closes. If I launch it again, the textbox displays "blah". I type "foo" into the textbox and hit Cancel, the sub form closes.

What I want to happen when I launch the sub form again is to see "blah" typed in the textbox, not "foo", just like how the Cancel button works in Windows. How do I accomplish this?
 
if the cancel button has the cancel property set to True, then this gives you an alternate way out of the form. It should behave as you mentioned, e.g., keeping the changes there unless you change them back.

If you're unloading the form, then the properties will not be carried forward from .show to .show. However, if you just .hide the form and .show it again, the properties of the text box will be preserved, whether you .hide it from the OK or the Cancel button's click event.
-Bob
 
I do have the cancel property set to True. I have set the Cancel button's click event to Unload the form, while the OK button's click event is to .hide the form. The textbox is initially blank. I type "blah", hit OK. Load the form, then I delete "blah" and type in its place "foo". If I hit Cancel, then load the form again, the textbox is blank instead of having "blah" in it. What am I doing wrong?

pds
 
You might try using a global variable (strPreviousText) to hold the previous content of the textbox. In the cmdOK_Click event put the following:

Code:
strPreviousText = Text1

In the Form_Activate event put a statement like:

Code:
Text1 = strPreviousText
 
The only difference is that you're unloading the form in the cancel case, which is causing you to lose the values. If you want to keep the values, .hide it in the cancel as well.

Don't use form_activate, else every time your app gets focus it's going to fire and hose your values. (Form_Load would do what you want, but I wouldn't encourage that either.)

If you're wanting to leave the values, just hide the form. Keep the form's reference variable around (in scope) so the form doesn't go away, e.g.,

Form1 declarations
Code:
public m_frmForm2 as Form2

Sub command1_click
  If m_frmForm2 is nothing then
    Set m_frmForm2 = New Form2
  end if

  m_frmForm2.Show vbModal ' modal or not?
  do stuff 'at this point, the form is hidden, keeping 
           'its  values for you for the next time.

End Sub


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top