
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