-
Nov 14th, 2013, 03:05 PM
#1
Thread Starter
Hyperactive Member
Access control which was created programatically from other form
I created control programatically from Form1 like this.
Private With Events Text1 As VB.TextBox
Set Text1 = Controls.Add(VB.TextBox", "Text1")
I want to have Form2 access Form1.Text1.
How could do this from Form2?
-
Nov 14th, 2013, 03:16 PM
#2
Re: Access control which was created programatically from other form
In general it is not a good idea to make specific references to controls on other forms as that limits your ability to re-use the code in other projects if needed. It is better to pass the control as a parameter to a user created sub and work with it there
Of course you can work with a control from another form but not if you make the control private as you have above. Change that to public and you will see different results
-
Nov 14th, 2013, 03:38 PM
#3
Re: Access control which was created programatically from other form
Originally Posted by DataMiser
Of course you can work with a control from another form but not if you make the control private as you have above. Change that to public and you will see different results
The object variable Text1 may be Private but the dynamically-created control Text1 is not. The following lines proves that:
Code:
Form1.Controls!Text1 = "I can see you!"
Form1!Text1.Text = "I can see you!"
EDIT
Here's another way assuming the index of the control is known beforehand:
Code:
'Assuming the TextBox was the first control created, its index will be 0
'If it was the last control added to the Form, its index will be Controls.Count - 1
Form1(0).Text = "I can see you!"
Last edited by Bonnie West; Nov 14th, 2013 at 04:31 PM.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Nov 14th, 2013, 04:49 PM
#4
Thread Starter
Hyperactive Member
Re: Access control which was created programatically from other form
Originally Posted by DataMiser
In general it is not a good idea to make specific references to controls on other forms as that limits your ability to re-use the code in other projects if needed. It is better to pass the control as a parameter to a user created sub and work with it there
Of course you can work with a control from another form but not if you make the control private as you have above. Change that to public and you will see different results
Yes, I mistakely used Private.
It works fine after I change it to Public.
Thanks!
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
|