Accessing another form's fields

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

From FormA I am opening another form FormB using ShowDialog(). Is there a
way to access a text field on FormA (calling form) from FormB (called form)?

Thanks

Regards
 
John said:
Hi

From FormA I am opening another form FormB using ShowDialog(). Is
there a way to access a text field on FormA (calling form) from FormB
(called form)?

My default answer: If you want to access an object you need a reference. If
you don't have it make it available. How, depends on when you want to access
FormA and which information do you really need. IMO, from an OO point of
view, I favor writing as abstract code as possible, so I think passing a
Form reference is not necessary. In short, what's your goal?


Armin
 
Hi

From FormA I am opening another form FormB using ShowDialog(). Is there a
way to access a text field on FormA (calling form) from FormB (called form)?

Thanks

Regards

Simplest way I can think is to use the overload on ShowDialog() and
pass in the calling form as the owner. Then, from the called form, you
can access any public member on the calling form like so:

///////////
Dim owner = TryCast(Owner, Form1)

If owner IsNot Nothing Then
'// Do something cool with the form
End If
///////////

However, I would prefer that you do something along the lines of
constructor injection, feeding whatever the called form needs into the
constructor, and preventing the need for the above backreferencing
(not to mention it decouples the called form from the calling form).

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
John,

For a ShowDialog form I mostly use simple "friend" fields in the showdialog
form, that gives me the most flexibility.

For those who want to tell this is not correct, you are right, byt I do this
only by one time to use ShowDialog forms.

Cor
 
I would do this one of two ways:

1. Make another constructor for FormB that takes two string values:

Public Sub New(val1 As String, val2 As String)

2. Add a method to FormB that takes the two string values as
parameters and call it after you instantiate FormB but before you call
ShowDialog.
 
Something like this

\\\
Class ShowDialogFormA
....
friend A as string
.....
End Class

Class MainForm
....
private sub loadform
dim A as new ShowDialogFormA
A.A = "this I want to show"
A.ShowDialog
dim B as string
B = A.A
A.Dispose
...
end sub
....
end Class
///

Cor
 
What about making that reference a property?
Why everyone wants to complicate everything!

MH
 
Back
Top