Setting Control on other form 
Author Message
 Setting Control on other form

I have one form opened as Modal.  I am opening another form as Modal
from that one.  I am trying to set a textbox in form1 from form2, then
close form2.  I can read the value of the textbox in form1 from form2,
but I can't seem to set it.  Anybody know why, or how I can set the
textbox in the first form?

Thanks,
Kevin



Sat, 17 Nov 2001 03:00:00 GMT  
 Setting Control on other form
This works for me:

'Form1 code
Private Sub Command1_Click()
    Form2.Show vbModal
End Sub

'Form2 code
Private Sub Command1_Click()
    Text1.Text = Form1.Text1.Text
End Sub

Private Sub Command2_Click()
    Form1.Text1.Text = Text1.Text
End Sub


Quote:

>I have one form opened as Modal.  I am opening another form as Modal
>from that one.  I am trying to set a textbox in form1 from form2, then
>close form2.  I can read the value of the textbox in form1 from form2,
>but I can't seem to set it.  Anybody know why, or how I can set the
>textbox in the first form?

>Thanks,
>Kevin

Paul Hewson  paulh(a)vif.*Delete*This*.com
-------------------------------------------
  e-mail altered to foil auto-spammers  

-------------------------------------------
Blessed are the censors; they shall inhibit the earth.


Sat, 17 Nov 2001 03:00:00 GMT  
 Setting Control on other form
    In these situations, what I tend to do is to create a flag in the modal
form, something like

Public CanUnload As Boolean

    I then show the form as modal and do whatever I need to do in the form.
When the user exits the form and when the form unloads, I check this value.
If it's false (the default value), then instead of the form unloading
itself, it just hides itself (and cancels the unload).  The calling form or
function will be returned to and can then read whatever values were set in
the modal form and do whatever it wants to do with them.  It then sets the
CanUnload flag to True and unloads the form by hand.  This time, the form
will actually be unloaded.

    In other words, have all the logic of what needs to be done with the
second form's data driven by the caller instead of having the second form
presume that it knows what form is calling it.  This also allows you to call
this dialog from somewhere else in your app.

--
Paul Parkhurst
Software Engineer


t>...

Quote:
>I have one form opened as Modal.  I am opening another form as Modal
>from that one.  I am trying to set a textbox in form1 from form2, then
>close form2.  I can read the value of the textbox in form1 from form2,
>but I can't seem to set it.  Anybody know why, or how I can set the
>textbox in the first form?

>Thanks,
>Kevin



Sat, 17 Nov 2001 03:00:00 GMT  
 Setting Control on other form
Ahhhh... Silly, silly me.  In the form activate on form1 I was setting
the value back to the default value.  To get around that, I used a
public boolean variable that I set from Form2, and then check it in
the Form1_Activate to see if I should load the deault value in the
textbox.  Thanks for the replies Paul and Paul.  Sorry to take up your
valuable time.


Quote:

>I have one form opened as Modal.  I am opening another form as Modal
>from that one.  I am trying to set a textbox in form1 from form2, then
>close form2.  I can read the value of the textbox in form1 from form2,
>but I can't seem to set it.  Anybody know why, or how I can set the
>textbox in the first form?

>Thanks,
>Kevin



Sat, 17 Nov 2001 03:00:00 GMT  
 Setting Control on other form

Quote:

>This works for me:

>'Form1 code
>Private Sub Command1_Click()
>    Form2.Show vbModal
>End Sub

>'Form2 code
>Private Sub Command1_Click()
>    Text1.Text = Form1.Text1.Text
>End Sub

>Private Sub Command2_Click()
>    Form1.Text1.Text = Text1.Text
>End Sub

Paul... I'm not trying to start an argument (and I won't argue with anybody
about it) but consider not writing inter-object communication this way.

One of the "key" app development concepts that has been proven to work well is
the concept of loose coupling.  Your form2 knows the name of form1 and the
name of a text control on it.

Exposing everything isn't the "most horrible thing on Earth" but it definitely
isn't the "greatest thing since sliced bread" either.  It should be avoided if
it can be avoided.

Tom



Sun, 18 Nov 2001 03:00:00 GMT  
 Setting Control on other form

Quote:

>Ahhhh... Silly, silly me.  In the form activate on form1 I was setting
>the value back to the default value.  To get around that, I used a
>public boolean variable that I set from Form2, and then check it in
>the Form1_Activate to see if I should load the deault value in the
>textbox.  Thanks for the replies Paul and Paul.  Sorry to take up your
>valuable time.

Pardon my butting in but it sounds like "activate" isn't the event that you
want to handle initialization in.  It is possible that you need a boolean flag
set in another form but the description of the "mechanism" just sounds too
complicated.

It is just a habit of mine to encourage people to seek out "better" solutions
even when "this works" happens to come along.

Form1 should ask Form2 for the information when it needs it (through a
public method) and probably should not ask Form2 if it is time now or not
through some public flag.

Tom



Sun, 18 Nov 2001 03:00:00 GMT  
 Setting Control on other form


I don't want an argument either, but I would like to clarify.  I am
not advocating that this is the best way to pass information to and
from routines.  I prefer passing through arguments (loose coupling).
However, the guy asked a question, said it couldn't be done, so I
tried it and it works.



Quote:
>Paul... I'm not trying to start an argument (and I won't argue with anybody
>about it) but consider not writing inter-object communication this way.

>One of the "key" app development concepts that has been proven to work well is
>the concept of loose coupling.  Your form2 knows the name of form1 and the
>name of a text control on it.

>Exposing everything isn't the "most horrible thing on Earth" but it definitely
>isn't the "greatest thing since sliced bread" either.  It should be avoided if
>it can be avoided.

>Tom



Sun, 18 Nov 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. passing forms and controls so I can set focus back to a form from a module

2. passing forms and controls so I can set focus back to a form from a module

3. Making Form appear on top of others.

4. Tax Forms & others

5. Problem: Two forms swap each others taskbar buttons

6. How Distinguish controls from others in UserForm

7. RichTextBox control and others !

8. Creating controls at run-time and others...

9. How Distinguish controls from others in UserForm

10. PROBLEM: getting a new control in front of others during runtime

11. Data Control Refresh works sometimes, others Not?!

12. help - ntsp3 killed tree control fonts, others

 

 
Powered by phpBB® Forum Software