Results 1 to 14 of 14

Thread: Calling Functions in other Forms (Vb.Net)...

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2003
    Location
    Ft. Lauderdale, FL
    Posts
    33

    Calling Functions in other Forms (Vb.Net)...

    Hello,

    I have a project with multiple forms. One form (Form A) contains a panel which contains many other controls. I have another form (Form B) that calls a function in Form A. The function in Form A uses a For Each loop which loops through the controls in Form A and does stuff to them.

    Unfortunitely, this isn't working. Any suggestions?

    VB Code:
    1. '-----------------------
    2. ' Form B
    3. '-----------------------
    4. Dim Answer As String = MsgBox("Are you sure you want to remove ALL images from the transfer list?", MsgBoxStyle.YesNo)
    5.                 If Answer = vbYes Then
    6.                     frmMain.UploadArray = myImgArray
    7.                     Call frmMain.RemoveAllImages()
    8.                 End If
    9.                 Answer = Nothing
    VB Code:
    1. ' -------------------------------------------------------
    2. ' Form A - Referred to in Form B as frmMain
    3. ' -------------------------------------------------------
    4. Public Sub RemoveAllImages()
    5.         If Not UploadArray Is Nothing Then
    6.             'loop througn all controls in Panel1 and set bgcolor to transparent
    7.             Me.Focus()
    8.             Dim pnlCtl As Control
    9.             For Each pnlCtl In Me.Panel1.Controls
    10.                 pnlCtl.BackColor = Color.Transparent
    11.                 pnlCtl.ForeColor = ForeColor.Black
    12.                 pnlCtl.Controls(0).ForeColor = ForeColor.Black
    13.             Next
    14.             ' Clear the upload Array
    15.             UploadArray = Nothing
    16.             'Update View Selected Images Form
    17.             If Not frmViewSelected Is Nothing Then
    18.                 frmViewSelected.myImgArray = UploadArray
    19.                 frmViewSelected.DisplayImageArray(UploadArray)
    20.             End If
    21.             stBarSelImgs.Text = "Selected Images: 0"
    22.             Me.Refresh()
    23.         End If
    24.     End Sub

    Thanks for any suggestions.
    Rich J Defilippo

  2. #2
    Hyperactive Member wolfrose's Avatar
    Join Date
    Aug 2002
    Location
    Indiana
    Posts
    309
    Try this:

    VB Code:
    1. dim FRM as new frmMain
    2. FRM.RemoveAllImages()

    You have to make instances of the forms before you can use them on other forms.

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2003
    Location
    Ft. Lauderdale, FL
    Posts
    33
    The forms are dimensioned as you suggested and I can follow the code right into the function via the debugger. The strange thing is that the Panel1 on control on Form A is always Nothing - that is -- when user causes the event on Form B to call the function on Form A.
    Rich J Defilippo

  4. #4
    Hyperactive Member wolfrose's Avatar
    Join Date
    Aug 2002
    Location
    Indiana
    Posts
    309
    The only other thing I can suggest is to make sure Panel1 is public.

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by wolfrose
    The only other thing I can suggest is to make sure Panel1 is public.
    default Access Modifier of controls is Friend which is Global to all your proje .

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2003
    Location
    Ft. Lauderdale, FL
    Posts
    33
    I'm trying stuff like Activating Form A and setting focus to it etc ... nothing has worked so far.

    I put a msgbox in Form A's activated event and I try to activate form a from Form B and the msgbox dosen't appear.

    I dosen't look like I can activate a form from another form.
    Rich J Defilippo

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Did you try Focus() Property ?I've not tried thought

  8. #8

    Thread Starter
    Member
    Join Date
    Jan 2003
    Location
    Ft. Lauderdale, FL
    Posts
    33
    Yeah, I tried Focus, Activate, Refresh ...

    For whatever reason the controls in the panel don't seem to be there - or are not accessable.
    Rich J Defilippo

  9. #9

    Thread Starter
    Member
    Join Date
    Jan 2003
    Location
    Ft. Lauderdale, FL
    Posts
    33
    I realized that my second Form was referring to a NEW instance of my main form. This instance is not visible.

    Form B has this Code :

    VB Code:
    1. Dim frmMain as New Form1()

    This created a new instance of form1 - it does not refer to the original instance of form1!!

    How can I refer to the original instance of Form1 from a second form? I tried this:

    VB Code:
    1. Dim frmMain As Form1()
    However, this causes the following to appear whenever I refer to frmMain ex frmMain.Show() ---> 'Show' is not a member of System.Array.

    ?????

    Any ideas?
    Rich J Defilippo

  10. #10
    Hyperactive Member wolfrose's Avatar
    Join Date
    Aug 2002
    Location
    Indiana
    Posts
    309
    put your
    VB Code:
    1. Dim frmMain as New Form1()
    on top, globally.

  11. #11

    Thread Starter
    Member
    Join Date
    Jan 2003
    Location
    Ft. Lauderdale, FL
    Posts
    33
    Humm ....

    After searching through this forum ... This solution seemed the easiest for me to implement:

    I added a module to the project and placed this code in it:
    VB Code:
    1. Module Module1
    2.     Public frmMain As Form1     ' referred to in Posts above as Form A
    3.     Public frmView As frmViewSelImgs  ' Referred to in posts above as Form B
    4. End Module

    Then in the load events of each of the forms I set the variables in the module:
    VB Code:
    1. 'In Form A's Load Event
    2. frmMain = Me
    3.  
    4. ' ---------------------------------
    5.  
    6. 'In Form B's Load Event
    7. FrmView = Me

    This allowed Form B to access all Public functions and variables in Form A (the Main start up form).

    I'm glad this works ... but I'm not sure I fully under stand why - can any of you recommend any resources that would help one understand the OOP principles involved in this problem? Any good books on OOP in general?

    Thanks.
    Rich J Defilippo

  12. #12
    Hyperactive Member wolfrose's Avatar
    Join Date
    Aug 2002
    Location
    Indiana
    Posts
    309
    Mastering Visual Basic .NET

    It's a huge 1200 page book, full of useful information.

  13. #13

    Thread Starter
    Member
    Join Date
    Jan 2003
    Location
    Ft. Lauderdale, FL
    Posts
    33
    Thanks!

    I've been using "Visual Basic .NET Black Book" but I don't like it that much. It covers basic stuff but not in great detail.
    Rich J Defilippo

  14. #14
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Richdef
    Humm ....

    After searching through this forum ... This solution seemed the easiest for me to implement:

    I added a module to the project and placed this code in it:
    VB Code:
    1. Module Module1
    2.     Public frmMain As Form1     ' referred to in Posts above as Form A
    3.     Public frmView As frmViewSelImgs  ' Referred to in posts above as Form B
    4. End Module

    Then in the load events of each of the forms I set the variables in the module:
    VB Code:
    1. 'In Form A's Load Event
    2. frmMain = Me
    3.  
    4. ' ---------------------------------
    5.  
    6. 'In Form B's Load Event
    7. FrmView = Me

    This allowed Form B to access all Public functions and variables in Form A (the Main start up form).

    I'm glad this works ... but I'm not sure I fully under stand why - can any of you recommend any resources that would help one understand the OOP principles involved in this problem? Any good books on OOP in general?
    Thanks.
    I love this site
    MS website


    I recommend reading online tutorials that explain OOP in different ways or technques !not like reading one book .just a thought !

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