Results 1 to 4 of 4

Thread: Accessing Form Controls via a sub

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2002
    Posts
    16

    Accessing Form Controls via a sub

    Does anyone know how to access a windows form control e.g. a text box from a sub procedure within a module. For example, I would like to call a sub in the form load event to prefill a text box or a listbox. However, I cannot access the active form's controls.

  2. #2
    New Member
    Join Date
    Aug 2002
    Location
    Columbus, Ohio
    Posts
    1
    Use Form Activate event. Controls may not be available when the load event fires.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2002
    Posts
    16

    Exclamation solution

    Passing the form control to the sub works as others had suggested in other posts.

    'Form 1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Call mySub(Me.TextBox1)

    End Sub



    'Module 1
    Module Module1
    Public Sub mySub(ByRef mytextBox As TextBox)
    mytextBox.Text = "test"

    End Sub
    End Module

  4. #4
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by Teacher
    Use Form Activate event. Controls may not be available when the load event fires.
    I believe controls are accesible after the Form.New is done, so it should be available in Form_Load (I THINK!)

    I'm not sure what exactly you want to do... you say you want to access the control through a module? or you want to access it in Form_Load?!

    umm and if you want to prefill some controls when you load the form, you could just modify the New constructor to receive the controls....

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