Results 1 to 4 of 4

Thread: Access control which was created programatically from other form

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2012
    Posts
    433

    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?

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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

  3. #3
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Access control which was created programatically from other form

    Quote Originally Posted by DataMiser View Post
    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)

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2012
    Posts
    433

    Re: Access control which was created programatically from other form

    Quote Originally Posted by DataMiser View Post
    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
  •  



Click Here to Expand Forum to Full Width