Accessing Controls on Other Forms

M

Mani

I have a windows form (FORM1) and in that I have a Richtextbox control.
I am loading another form (FORM2) from form 1. From FORM2 I would like
to access the richtexbox control on FORM1. So I tried doing the
following code in form 2.

Dim m_Form1 As New Form1
MessageBox.Show(m_Form1.RichTextBox1.Text)

But the above code returns me nothing.

Any suggestions???

Regards
Mani
 
P

Phill W.

Mani said:
From FORM2 I would like to access the richtexbox control on FORM1.
Dim m_Form1 As New Form1

Creates a new instance of the Class Form1 and store a reference to it in
the variable m_Form1.
MessageBox.Show(m_Form1.RichTextBox1.Text)

Retrieves the Text property of the Richtextbox control on that /new/
form - probably not a lot in it at this point ... ;-)

Forms are Classes, so to access a property of one, you have to have a
reference to that particular object. Pass a reference to Form1 to
Form2, as in

[Form1.vb]
Sub button_Click( ... )
Dim f2 as New Form2
f2.Master = Me
f2.ShowDialog() ' or whatever
End Sub

Class Form2
Public WriteOnly Property Master() as Form1
Set(value As Form1)
_Master = value
End Set
End Property

Private _Master As Form1 = Nothing

Sub otherButton_Click( ... )
MessageBox.Show( _Master.RichTextBox1.Text )
End Sub

End Class

HTH,
Phill W.
 
C

Cor Ligthert [MVP]

Phill,

Your "_Master" property exist already, the name for it is "Owner".

In my idea is that nicer to use and than you can forget to create that
property yourself.

Cor

Phill W. said:
Mani said:
From FORM2 I would like to access the richtexbox control on FORM1.
Dim m_Form1 As New Form1

Creates a new instance of the Class Form1 and store a reference to it in
the variable m_Form1.
MessageBox.Show(m_Form1.RichTextBox1.Text)

Retrieves the Text property of the Richtextbox control on that /new/
form - probably not a lot in it at this point ... ;-)

Forms are Classes, so to access a property of one, you have to have a
reference to that particular object. Pass a reference to Form1 to Form2,
as in

[Form1.vb]
Sub button_Click( ... )
Dim f2 as New Form2
f2.Master = Me
f2.ShowDialog() ' or whatever
End Sub

Class Form2
Public WriteOnly Property Master() as Form1
Set(value As Form1)
_Master = value
End Set
End Property

Private _Master As Form1 = Nothing

Sub otherButton_Click( ... )
MessageBox.Show( _Master.RichTextBox1.Text )
End Sub

End Class

HTH,
Phill W.
 
P

Phill W.

Cor said:
Phill,

Your "_Master" property exist already, the name for it is "Owner".

Cor,

Another day, another new property I've not met before - and it makes for
/much/ nicer code, too ...

Many Thanks,
Phill W.
[Form1.vb]
Sub button_Click( ... )
Dim f2 as New Form2
f2.Owner = Me
f2.ShowDialog() ' or whatever
End Sub

Class Form2
Sub otherButton_Click( ... )
MessageBox.Show( Me.Owner.RichTextBox1.Text )
End Sub

End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top