Results 1 to 11 of 11

Thread: Access another form's objects

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Question Access another form's objects

    Is there a way I can access another form's objects or instances without passing them through a function or sub routien? Here's my situation.

    I have a frmMain form that contains all my objects: buttons, panels, text boxes, datagrid, dataset and dataadapter etc.. Then I added a another class component, clsEvent, to my project. In this class, I want to be able to access all those objects in the frmMain form without passing them through a public shared sub. Is there a way? If there is way, mind give me a hand on this issue?

    Any help is greatly appreciated!

    ljCharlie

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You should pass the current instance of frmMain to clsEvent, preferably in the constructor.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    Many thanks for your help. But how do I do that?

    ljCharlie

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    VB Code:
    1. 'in clsEvent
    2. 'store a local reference to a frmMain type
    3. Private _frm As frmMain
    4.  
    5. 'make constructor for passing it in
    6. Public Sub New(form as frmMain)
    7.   'assign instance to local reference
    8.   _frm=form
    9.   'now you can refer to anything on frmMain by using _frm inside the class
    10. End Sub
    11.  
    12. 'example
    13. Public Sub ShowText()
    14.   Msgbox(_frm.TextBox1.Text)
    15. End Sub
    16.  
    17. 'in frmMain when creating clsEvent
    18. Dim ce As New clsEvent(Me)

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Thumbs up

    Again, many thanks for the help.

    Where in frmMain do I put this line: Dim ce As New clsEvent(Me)

    And what is it for?

    ljCharlie

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    That should be wherever you are creating the clsEvent object, which I guess doesn't have to be in frmMain but if its not then use the current instance of frmMain instead of ME in the constructor.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    Thanks! Any variable or instance I declared as Private in the frmMain can not be accessed through this constructor, correct? Another words in frmMain I declared:

    Private tableStyle1 As New DataGridTableSytle()

    Can not be accessed in clsEvent as _frm.tableStyle1 right?

    ljCharlie

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    That is correct since it has private scope then it can't be accessed outside of the form.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Thumbs up

    That is it! Thank you very much for the time you spent helping me.

    ljCharlie

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Question

    Sorry to bother you again, but I encounter a problem. In the clsEvent class, it seemed that I have to declared my routine as Private Sub and not Private Shared Sub for the constructor to work. So the question is, how do I call the sub routine in the frmMain form since it is not Shared?

    ljCharlie

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    I think I got it.

    ljCharlie

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