Can anybody straighten me out on accessing one form's controls from another form 
Author Message
 Can anybody straighten me out on accessing one form's controls from another form

I've isolated what my problem down to the simplest example.  I've got two
forms: Form1 and Form2.  Form1 has a text box TextBox1 and shows Form2 when it
loads.  Form 2 has a button.  When you press the button Form2, all I want to do
is increment the value in the text box on Form 1.

The book I'm staring at and some posts suggest the following code should wor

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim a As Integer
Dim n As New Form1() 'this is the problem I suspect
a = n.text1.Text
a = a + 1
n.text1.Text = a
End Sub

However, what really seems to happen is the New creates a new instance of Form1
and I update that form.  What I want to do is get a reference to the original
Form1.  I've noticed that in the book I'm using, the code actually reads "Dim n
as New Form1" but when I enter this the IDE adds "()".  

For the moment I got around this by defining a public form variable in a
module, setting it to "me" when Form1 loads, and using that everywhere.  I'm
not finding this a satisfying solution and don't understand how original code
would ever work.

Obviously I'm missing something simple and it is literally keeping me awake at
nights.

Any ideas?

Thanks, Bill



Sun, 05 Dec 2004 12:37:04 GMT  
 Can anybody straighten me out on accessing one form's controls from another form
Well, it looks like I answered my own question, but I can't believe there isn't
a better way.  

This microsoft article below suggests you take the approach I took and define a
global variable or define some ugly shared variables and a shared property in
the form.  UGLY.  Isn't there some way to just find my initial form by name or
something?

While overall I really like .net and they have cleaned up vast tracks of
inconsistency, a few simple things are now a pain in the butt.

Anybody got a better way to do this?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_v...
art/html/vbtchWorkingWithMultipleFormsInVisualBasicNETUpgradingToNET.asp



Sun, 05 Dec 2004 12:57:53 GMT  
 Can anybody straighten me out on accessing one form's controls from another form
Overload new() on form2 to

public sub new(f as form1)

create form2 from form 1 with

dim f2 as new form2(me)


Quote:
> Well, it looks like I answered my own question, but I can't believe there
isn't
> a better way.

> This microsoft article below suggests you take the approach I took and
define a
> global variable or define some ugly shared variables and a shared property
in
> the form.  UGLY.  Isn't there some way to just find my initial form by
name or
> something?

> While overall I really like .net and they have cleaned up vast tracks of
> inconsistency, a few simple things are now a pain in the butt.

> Anybody got a better way to do this?

> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_v...
> art/html/vbtchWorkingWithMultipleFormsInVisualBasicNETUpgradingToNET.asp



Sun, 05 Dec 2004 13:27:33 GMT  
 Can anybody straighten me out on accessing one form's controls from another form
Hi Benahl,

A better way would be to create an event delegate on form 2 (OnGetValue
(byref value as integer).
Assign this eventhandler to a method of form1 (addhandler) and call the
event (raiseevent) in form2.

This way, when you need form2 in another application, you just have to set
the eventhandler to some other method, without having to change anything in
form2.

Wim


Quote:
> Well, it looks like I answered my own question, but I can't believe there
isn't
> a better way.

> This microsoft article below suggests you take the approach I took and
define a
> global variable or define some ugly shared variables and a shared property
in
> the form.  UGLY.  Isn't there some way to just find my initial form by
name or
> something?

> While overall I really like .net and they have cleaned up vast tracks of
> inconsistency, a few simple things are now a pain in the butt.

> Anybody got a better way to do this?

> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_v...
> art/html/vbtchWorkingWithMultipleFormsInVisualBasicNETUpgradingToNET.asp



Sun, 05 Dec 2004 13:30:04 GMT  
 Can anybody straighten me out on accessing one form's controls from another form
The original code doesn't cover what you are looking for and may have been
written from .Net beta.

If your Form1 is your startup object and calls Form2 then Form1 has a
reference to Form2. Now Form2 wants a reference to Form1.

My suggestion is to add "Public mfrmParent As Form1" to Form2 class. After
creating an instance of Form2 "aForm2.frmParent = me" records a reference
for Form1 to Form2.
.
The reason for doing so on Form2 is it acts like a property (which you could
code it as if you wished) and allows Form2 to address anything in Form1.
Also the variable name is instructive and doesn't need a seperate module to
work.

Alternatively in Form2 add an overload New procedure:

Public mfrmParent As Form1

Public Sub New(ByVal frmParent As Form)
    Me.new()
    mfrmParent = frmParent
End Sub

Now if you use "dim n as new Form2(Me)" you have created a single instance
of Form2 with knowledge of it's parent Form1.


Quote:
> I've isolated what my problem down to the simplest example.  I've got two
> forms: Form1 and Form2.  Form1 has a text box TextBox1 and shows Form2
when it
> loads.  Form 2 has a button.  When you press the button Form2, all I want
to do
> is increment the value in the text box on Form 1.

> The book I'm staring at and some posts suggest the following code should
wor

> Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click

> Dim a As Integer
> Dim n As New Form1() 'this is the problem I suspect
> a = n.text1.Text
> a = a + 1
> n.text1.Text = a
> End Sub

> However, what really seems to happen is the New creates a new instance of
Form1
> and I update that form.  What I want to do is get a reference to the
original
> Form1.  I've noticed that in the book I'm using, the code actually reads
"Dim n
> as New Form1" but when I enter this the IDE adds "()".

> For the moment I got around this by defining a public form variable in a
> module, setting it to "me" when Form1 loads, and using that everywhere.
I'm
> not finding this a satisfying solution and don't understand how original
code
> would ever work.

> Obviously I'm missing something simple and it is literally keeping me
awake at
> nights.

> Any ideas?

> Thanks, Bill



Sun, 05 Dec 2004 13:33:12 GMT  
 Can anybody straighten me out on accessing one form's controls from another form
Phillip, wil, Robert,

Excellent suggestions all.  Thanks for helping me get up to speed here.

For anyone searching in the future, it seems that Mansfield's "Visual Basic
.Net Weekend Crash Course" has a problem with the session 6 example having
been written for the beta release and not quite working in the commercial
release.

- Bill



Quote:
> The original code doesn't cover what you are looking for and may have been
> written from .Net beta.

> If your Form1 is your startup object and calls Form2 then Form1 has a
> reference to Form2. Now Form2 wants a reference to Form1.

> My suggestion is to add "Public mfrmParent As Form1" to Form2 class. After
> creating an instance of Form2 "aForm2.frmParent = me" records a reference
> for Form1 to Form2.
> .
> The reason for doing so on Form2 is it acts like a property (which you
could
> code it as if you wished) and allows Form2 to address anything in Form1.
> Also the variable name is instructive and doesn't need a seperate module
to
> work.

> Alternatively in Form2 add an overload New procedure:

> Public mfrmParent As Form1

> Public Sub New(ByVal frmParent As Form)
>     Me.new()
>     mfrmParent = frmParent
> End Sub

> Now if you use "dim n as new Form2(Me)" you have created a single instance
> of Form2 with knowledge of it's parent Form1.



> > I've isolated what my problem down to the simplest example.  I've got
two
> > forms: Form1 and Form2.  Form1 has a text box TextBox1 and shows Form2
> when it
> > loads.  Form 2 has a button.  When you press the button Form2, all I
want
> to do
> > is increment the value in the text box on Form 1.

> > The book I'm staring at and some posts suggest the following code should
> wor

> > Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As
> > System.EventArgs) Handles Button1.Click

> > Dim a As Integer
> > Dim n As New Form1() 'this is the problem I suspect
> > a = n.text1.Text
> > a = a + 1
> > n.text1.Text = a
> > End Sub

> > However, what really seems to happen is the New creates a new instance
of
> Form1
> > and I update that form.  What I want to do is get a reference to the
> original
> > Form1.  I've noticed that in the book I'm using, the code actually reads
> "Dim n
> > as New Form1" but when I enter this the IDE adds "()".

> > For the moment I got around this by defining a public form variable in a
> > module, setting it to "me" when Form1 loads, and using that everywhere.
> I'm
> > not finding this a satisfying solution and don't understand how original
> code
> > would ever work.

> > Obviously I'm missing something simple and it is literally keeping me
> awake at
> > nights.

> > Any ideas?

> > Thanks, Bill



Sun, 05 Dec 2004 14:30:57 GMT  
 Can anybody straighten me out on accessing one form's controls from another form
I have now Learnt with threading it is important to use Invoke and Delegate
when accessing objects controlled from another thread.

Have a look at http://www.codenotes.com/do/articles/article?articleID=515.
Note text line wrapping has to be fixed when pasting the code and the second
source replaces several procedures in the first.


Quote:
> Phillip, wil, Robert,

> Excellent suggestions all.  Thanks for helping me get up to speed here.

> For anyone searching in the future, it seems that Mansfield's "Visual
Basic
> .Net Weekend Crash Course" has a problem with the session 6 example having
> been written for the beta release and not quite working in the commercial
> release.

> - Bill



> > The original code doesn't cover what you are looking for and may have
been
> > written from .Net beta.

> > If your Form1 is your startup object and calls Form2 then Form1 has a
> > reference to Form2. Now Form2 wants a reference to Form1.

> > My suggestion is to add "Public mfrmParent As Form1" to Form2 class.
After
> > creating an instance of Form2 "aForm2.frmParent = me" records a
reference
> > for Form1 to Form2.
> > .
> > The reason for doing so on Form2 is it acts like a property (which you
> could
> > code it as if you wished) and allows Form2 to address anything in Form1.
> > Also the variable name is instructive and doesn't need a seperate module
> to
> > work.

> > Alternatively in Form2 add an overload New procedure:

> > Public mfrmParent As Form1

> > Public Sub New(ByVal frmParent As Form)
> >     Me.new()
> >     mfrmParent = frmParent
> > End Sub

> > Now if you use "dim n as new Form2(Me)" you have created a single
instance
> > of Form2 with knowledge of it's parent Form1.



> > > I've isolated what my problem down to the simplest example.  I've got
> two
> > > forms: Form1 and Form2.  Form1 has a text box TextBox1 and shows Form2
> > when it
> > > loads.  Form 2 has a button.  When you press the button Form2, all I
> want
> > to do
> > > is increment the value in the text box on Form 1.

> > > The book I'm staring at and some posts suggest the following code
should
> > wor

> > > Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As
> > > System.EventArgs) Handles Button1.Click

> > > Dim a As Integer
> > > Dim n As New Form1() 'this is the problem I suspect
> > > a = n.text1.Text
> > > a = a + 1
> > > n.text1.Text = a
> > > End Sub

> > > However, what really seems to happen is the New creates a new instance
> of
> > Form1
> > > and I update that form.  What I want to do is get a reference to the
> > original
> > > Form1.  I've noticed that in the book I'm using, the code actually
reads
> > "Dim n
> > > as New Form1" but when I enter this the IDE adds "()".

> > > For the moment I got around this by defining a public form variable in
a
> > > module, setting it to "me" when Form1 loads, and using that
everywhere.
> > I'm
> > > not finding this a satisfying solution and don't understand how
original
> > code
> > > would ever work.

> > > Obviously I'm missing something simple and it is literally keeping me
> > awake at
> > > nights.

> > > Any ideas?

> > > Thanks, Bill



Mon, 06 Dec 2004 11:43:48 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Anybody know of a control which will simulate access combo, i.e more than one column

2. Anybody know of a control which will simulate access combo, i.e more than one column

3. 'Canned' data in VB program

4. Two Access 2000 db's on one VB6 form

5. Activex control doesn't work when there is more then one on a form

6. License Error with OCX's anybody, anybody

7. How to access a DB's form's control from w/i VB module

8. How do I create a canned form?

9. Cans access2.0 engine access btrieve files?

10. PRB: One (just one) control in a form

11. One one form based on a control value in another

12. Looking 4 Canned Control

 

 
Powered by phpBB® Forum Software