"Tom McLaughlin" <tb***@cwnet.com> schrieb
I have been working on projects in vb5 and I am now trying to
get started with .net as you can see I am not doing very well.
I have a program that has two forms. On form1 I have a
TextBox1. From form2 I would like to write text to
form1.TextBox1.text but this does not work in .net. What changes
do I need to make this work.
If you want to access an object you need a reference. If you don't
have a reference it must be made available. This is usually done by
passing it using a public property or as a procedure argument.
OK, I do want to access the TextBox1 on form1 so how would I
reference it so that form2 can write to it?
It depends on the structure of the application.
- Is it an MDI application?
- Can there be multiple instances of the same Form?
- Are the Forms shown modally or modeless?
- Where are the Forms created?
- What's the relation between the Forms?
- Which types of Forms can be shown at the same time?
I assume that Form1 is the startup object and Form2 is shown modally:
Code in Form2:
Private m_Textbox As TextBox
Public Overloads Function ShowDialog( _
ByVal owner As System.Windows.Forms.IWin32Window, _
ByVal TextBox As TextBox) As DialogResult
m_Textbox = TextBox 'store reference to the textbox
Return MyBase.ShowDialog(owner) 'call procedure in base class
End Function
Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
m_Textbox.Text = "test"
End Sub
In Form1:
Dim f as new form2
f.Showdialog(me, Me.Textbox1)
That's only an example. You could also pass the textbox in the constructor
of Form2.
I prefer not passing controls to other Forms. You could pass the information
back from Form2 to Form1, so Form2 doesn't need a reference to Form1. This
is also the "better" OO way because it makes Form2 more abstract. I usually
do this with modal Forms.
Another way is to raise an event in Form2 and have Form1 catch it. Information
can also be passed when raising the event. This strategy I usually use with
modeless Forms.
--
Armin
http://learn.to/quote http://www.plig.net/nnq/nquote.html