|
-
Feb 11th, 2003, 10:31 AM
#1
Thread Starter
Member
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:
'-----------------------
' Form B
'-----------------------
Dim Answer As String = MsgBox("Are you sure you want to remove ALL images from the transfer list?", MsgBoxStyle.YesNo)
If Answer = vbYes Then
frmMain.UploadArray = myImgArray
Call frmMain.RemoveAllImages()
End If
Answer = Nothing
VB Code:
' -------------------------------------------------------
' Form A - Referred to in Form B as frmMain
' -------------------------------------------------------
Public Sub RemoveAllImages()
If Not UploadArray Is Nothing Then
'loop througn all controls in Panel1 and set bgcolor to transparent
Me.Focus()
Dim pnlCtl As Control
For Each pnlCtl In Me.Panel1.Controls
pnlCtl.BackColor = Color.Transparent
pnlCtl.ForeColor = ForeColor.Black
pnlCtl.Controls(0).ForeColor = ForeColor.Black
Next
' Clear the upload Array
UploadArray = Nothing
'Update View Selected Images Form
If Not frmViewSelected Is Nothing Then
frmViewSelected.myImgArray = UploadArray
frmViewSelected.DisplayImageArray(UploadArray)
End If
stBarSelImgs.Text = "Selected Images: 0"
Me.Refresh()
End If
End Sub
Thanks for any suggestions.
-
Feb 11th, 2003, 01:12 PM
#2
Hyperactive Member
Try this:
VB Code:
dim FRM as new frmMain
FRM.RemoveAllImages()
You have to make instances of the forms before you can use them on other forms.
-
Feb 11th, 2003, 01:55 PM
#3
Thread Starter
Member
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.
-
Feb 11th, 2003, 02:05 PM
#4
Hyperactive Member
The only other thing I can suggest is to make sure Panel1 is public.
-
Feb 11th, 2003, 02:26 PM
#5
Sleep mode
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 .
-
Feb 11th, 2003, 02:40 PM
#6
Thread Starter
Member
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.
-
Feb 11th, 2003, 02:52 PM
#7
Sleep mode
Did you try Focus() Property ?I've not tried thought
-
Feb 11th, 2003, 03:40 PM
#8
Thread Starter
Member
Yeah, I tried Focus, Activate, Refresh ...
For whatever reason the controls in the panel don't seem to be there - or are not accessable.
-
Feb 12th, 2003, 10:18 AM
#9
Thread Starter
Member
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:
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:
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?
-
Feb 12th, 2003, 11:08 AM
#10
Hyperactive Member
put your
VB Code:
Dim frmMain as New Form1()
on top, globally.
-
Feb 12th, 2003, 11:31 AM
#11
Thread Starter
Member
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:
Module Module1
Public frmMain As Form1 ' referred to in Posts above as Form A
Public frmView As frmViewSelImgs ' Referred to in posts above as Form B
End Module
Then in the load events of each of the forms I set the variables in the module:
VB Code:
'In Form A's Load Event
frmMain = Me
' ---------------------------------
'In Form B's Load Event
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.
-
Feb 12th, 2003, 12:03 PM
#12
Hyperactive Member
Mastering Visual Basic .NET
It's a huge 1200 page book, full of useful information.
-
Feb 12th, 2003, 01:12 PM
#13
Thread Starter
Member
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.
-
Feb 12th, 2003, 02:10 PM
#14
Sleep mode
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:
Module Module1
Public frmMain As Form1 ' referred to in Posts above as Form A
Public frmView As frmViewSelImgs ' Referred to in posts above as Form B
End Module
Then in the load events of each of the forms I set the variables in the module:
VB Code:
'In Form A's Load Event
frmMain = Me
' ---------------------------------
'In Form B's Load Event
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|