|
-
Nov 3rd, 2003, 06:09 PM
#1
Thread Starter
Junior Member
Passing properties between forms
Actually it has for a long time been considered bad practice to be able to reach the properties and method of controls from an other form.
A control should always be considered to be private.
In VB.Net this has been enforced so all controls are private to the form that host them.
So if you need to pass values between forms you should write your own wrapper property procedures.
For example: You have a TextBox on Form1 and you need to get the value of that textbox in Form2.
Then you could add the following to Form1:
visual basic code:
Public Property TextBoxContent() As String
Get
TextBoxContent = TextBox1.Text
End Get
Set(ByVal Value As String)
TextBox1.Text = Value
End Set
End Property
Since this property is Public it can be reached from Form2
visual basic code:
MsgBox Form1.TextBoxContent
Best regards
I've tried this and it did not work for me.
An unhandled exception of type 'System.NullReferenceException' occurred in 6CUL1763.exe
Additional information: Object reference not set to an instance of an object.
is the error that I got. otherwise the only thing that I changed was the property name.
-
Nov 3rd, 2003, 06:25 PM
#2
ok, when loading Form2 ( from a button / sub in Form1 ) ...
VB Code:
[COLOR=blue]Dim[/color] frm2 [COLOR=blue]As New[/COLOR] Form2()
[COLOR=blue]MyBase[/color].AddOwnedForm(frm2)
frm2.Show()
then in Form2 ( to access the controls on Form1 , again in a button click / sub ) ...
VB Code:
[COLOR=blue]Dim[/color] frmMain [COLOR=blue]As[/color] Form1 = [COLOR=blue]DirectCast[/color]([COLOR=blue]Me[/color].Owner,Form)
TextBox.Text = frmMain.TextBox1.Text
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Nov 4th, 2003, 12:52 PM
#3
Addicted Member
Whats up with the "DirectCast"?
Ive always just used :
Dim frm as Form1= Me.Owner
What is the difference, if any?
-
Nov 4th, 2003, 01:00 PM
#4
try switching Option Strict On and see
Dim frm as Form1= Me.Owner will throw an error , as it's a bad cast , the correct vb.net syntax is using DirectCast
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Nov 5th, 2003, 08:23 AM
#5
Addicted Member
Thanks For the Explanation
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|