PDA

Click to See Complete Forum and Search --> : Multiple Form Data Sharing (or control manipulation)


Bucho
Aug 30th, 2002, 07:07 AM
I've been doing VB6 for about 5 mo's casually, and VB.net for about 3 days hardcore, so this may be a basic question but...
I have a question about controlling forms from within other forms in VB.NET. First of all I've found that in the old VB6.0 you could cycle through forms and change the controls with a statement like:

Dim frm As frmName 'Your forms name here
For Each frm In Forms
frm.caption = "test"
Next

This this not doable in .NET. How can this be achieved in VB.net? And is there a simple way. I've done lots of looking through books, and have found a way to communicate and control one form with another form via raising events. What i've found though is that the event can only be raised in your active form and maybe in the form/class that the event is defined in, but it will NOT cycle through all of your forms and trigger the the very same event.

I also make the instances of my forms through code. I do not have a separate design form for each form I open, and this is where the problem really lies because I can not reference each newly created(from code) form and their controls. And I have many forms to manage.

I'd REALLY appreciate any help with this.. just a point in the right direction even.

And arent' those splitter bars and docking/anchoring functionality GREAT!!

Bucho
Aug 30th, 2002, 08:22 AM
I've seen probs like this talked about in this forum and did not find a solution.. I'll probably give up on it, in the forums at least, if i get a few replys from people with a few thousands of posts who don't know the answer.
then if i find the answer i'll post it here.
What do ya say Cander, do ya know? :D

Cander
Aug 30th, 2002, 08:38 AM
What you need to do is instantiate a a copy of that form


Dim a As New formname()

then you can access it like you would before.

also preferred is passing a reference of controls you may need to access from another from to the new class/form via a prameter.

Magiaus
Aug 30th, 2002, 10:22 AM
There is away to do something simular to a Forms collection but i don't think it is good practice. You could create a collection and instance your forms through this global collection. That would give you complete access to all forms via the collection but you would also need to write clean up code and resort code if you wanted to have optimal mem usage.

I read something about ActiveForm but this only returns the active form not the active form of that form class. It seems like MS would have cover this some how.

Cander try to help this guy, please. http://www.vbforums.com/showthread.php?s=&threadid=195138

I can't think of a good way to explain it and when he create an instance of a form he says the changes do not take. I remember the other explenation in the forums about a method to change properties on another form but i never implamented it so......

Bucho
Aug 30th, 2002, 01:05 PM
Ok the thing is that I instantiate many copies of forms named the same thing.. so lets say i do a:
Dim a As New formname()

every time someone pressess a button.. making many instances of a form called "a" from the type of form called "formname"

how would i update all of those "a" forms created with the button, from within just one of those "a" forms

I guess another thing would be if I could change "a" to something else in the code each time the number is pressed.. so that instead of "a" it would be "b", if "a" already exists.

I'll try something out I just thought of a bit later

thanks for your help though, I might just need to be thinking of how to dynamically change the new form names of the form by pressing the same button to make the same form type again, but with a different name, all in the same section of code.

Hell, I could probably just instantiate 20 forms with different names in a module.. then I could probably reference them all individually, but i was thinking this is a crude way to solve the problem.. if i did it this way, i'm sure it would take up a lot of unneccessary memory. if you have Dimmed 20 forms but the user only needed 3. Know what i mean?

Magiaus
Aug 30th, 2002, 03:30 PM
you could give the form a tag like in vb6 to unikely id it. I don't know though i have never done anything that required that kind of form creation.

Bucho
Aug 30th, 2002, 04:03 PM
yup, i already did that.. but the tag does not help the situation unless you can call on the form from within another form by its tag.. if that is a feature let me know :)
Kind of like an index or something

formname.tag("tagname").Textbox1.text = "hello"


would be nice..

Bucho
Aug 30th, 2002, 05:21 PM
Well, i sort of solved my own problem, or found out something was not a problem.
Previously I did not want to Instantiate 25+ different forms in a module because the user probably would not need taht many forms to work with, and i thought it would be highly memory inefficient. Well, I looked on the taskbar and after instantiating 25+ different forms in a module, it took up no more memory than not instantiating any in that module. This is good news, and it means that somehow the forms just take up memory when they are Showed and not when they are just instantiated. Anyway, this does solve my issue... even though instantiating 20-30 forms in a module just seems like an odd thing to do.

Magiaus
Aug 31st, 2002, 01:03 PM
make a method to create a new form and set its tag. then you can use the forms in a collection and loop through the collection and grab the one you need by its tag. like you would do with an onj in vb6.

i don't know if this is correct or not

For Each Frm In Col
If Frm.Tag = "The one i passed in to the method" Then
useIt
End If
Next


That is basicly how i would work with it if i was doing this that way you can have three methods AddForm, GetForm and RemoveForm Get Form would return a ref to a form object and the others manipulate the collection and you pass in a tag or something to id the form by... thats what i would do its cleaner to me than having all those Dim i as form, ii as form, iii as form

Magiaus
Aug 31st, 2002, 02:35 PM
here is a form collection very basic very working

this is the main procedure that starts the app
Module intMain
Private mFrmz As New Magaus.Collections.Forms()
Public Sub Main()
Dim Frm As Form = New FormA()
Dim i As Integer = 0
mFrmz.AddForm(Frm, CStr(i))
For i = 1 To 10
Frm = New FormA()
mFrmz.AddForm(Frm, CStr(i))
Next

For i = 0 To 10
mFrmz.GetForm(CStr(i)).show()
mFrmz.GetForm(CStr(i)).text = CStr(i)
Next


End Sub
End Module

this is the forms collection class
Option Explicit On
Option Strict On

'Imports System.Collections

Namespace Magaus.Collections
Public Class Forms
Private col As New Collection()
Private mFrm As Object

Public Sub AddForm(ByVal Frm As Object, ByVal Key As String)
mFrm = Frm
col.Add(mFrm, Key)
Frm = Nothing
mFrm = Nothing
End Sub

Public Function GetForm(ByVal key As String) As Object
GetForm = col(key)
End Function

Public Sub Remove(ByVal key As String)
col.Remove(key)
End Sub
End Class
End Namespace

this works all you have to do is use the collection to manage your forms makes a global instance and there you go. the code could be imporved but it works.

sorry i didn't do this sooner but i haven't had a chance to code on it any....

let me know if this will work better than the other way

Bucho
Sep 2nd, 2002, 10:42 PM
Thanks a lot Magiaus, i just saw this. I"m going to try this immediately and let you know if it worked.

hellswraith
Sep 2nd, 2002, 11:01 PM
Here is the msdn library answer to your problem.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vxconformscollectionchangesinvisualbasicnet.asp

I think that when I get the time, I will create a static class that will handle all this for you. Hopefully I will get this within the week. When I am done, I will post it in the codebank area of the vb-world.com site.

Bucho
Sep 2nd, 2002, 11:19 PM
Thanks a lot, this is exactly what I was looking for and it works perfectly. I just saw no documentation on collections for VB in my book, thanks :)

hellswraith, i'll be looking for your code, thank you too..

Magiaus
Sep 2nd, 2002, 11:21 PM
i was going to be all cool and use inheratns and all that but with oprion strict on it is tricky at the list.add lvl.... i still might

Magiaus
Sep 2nd, 2002, 11:22 PM
i can't even spell inheratince umm inherits CollectionBase yeah

Magiaus
Sep 2nd, 2002, 11:31 PM
I worked on it a little more the other day but never posted it.
here

i spelled my namespace right as well

#Region "Options"
Option Compare Binary
Option Explicit On
Option Strict On
#End Region

Namespace Magiaus.Collections
#Region "Example"
' Note: The forms used in this example are created at runtime and
' do not exist in a project. Also I am using a public instance
' of Magiaus.Collections.Forms decalared in a module.
'
' Module Cmn
' Public Forms As New Magiaus.Collections.Forms()
' End Module
'
' Adding Forms
' ****************************************************************************
' Dim i As System.Int32, frm As New System.Windows.Forms.Form()
' For i = 1 To 20
' Forms.Add(frm, CStr(i))
' With Forms(CStr(i))
' .Text = "I am runtime form #" & CStr(i)
' .Tag = CStr(i)
' .size = New System.Drawing.Size(375, 0)
' .show()
' End With
' If i = 20 Then
' frm = Nothing
' Else
' frm = New System.Windows.Forms.Form()
' End If
' Next
' ****************************************************************************
'
' Removing Forms
' ****************************************************************************
' Dim i As System.Int32
' For i = 1 To 20
' Forms(CStr(i)).dispose() 'alternatly you could place the
' Forms.Remove(CStr(i)) 'remove code in a forms dispose method
' Next
' ****************************************************************************
'
' Manipulating Forms once they are in the collection
' ****************************************************************************
' To access a form that you have put ino the collection you must use
' either the Key or the index of the form. If you add a form and do not
' specify a key the forms name will be used.
'
' Forms("FormKey).Show()
'
' Note: at this point the forms properies are not exposed to intellsense
#End Region
Public Class Forms

Private Frms As New Collection()


Public Sub Add(ByVal Frm As System.Windows.Forms.Form)
Frms.Add(Frm, Frm.Name)
End Sub


Public Sub Add(ByVal Frm As System.Windows.Forms.Form, ByVal Key As System.String)
Frms.Add(Frm, Key)
End Sub


Public ReadOnly Property Count() As System.Int32
Get
Count = Frms.Count
End Get
End Property


Public Sub Remove(ByVal Key As System.String)
Frms.Remove(Key)
End Sub


Public Sub Remove(ByVal Index As System.Int32)
Frms.Remove(Index)
End Sub


Default Public ReadOnly Property Form(ByVal Index As System.Int32) As Object
Get
Form = Frms.Item(Index)
End Get
End Property


Default Public ReadOnly Property Form(ByVal Key As System.String) As Object
Get
Form = Frms.Item(Key)
End Get
End Property
End Class
End Namespace

collections rock you can make class that expose collections of objects that expose collections of objects that.... well anyway

Bucho
Sep 3rd, 2002, 04:44 AM
Well this cut my code down at least 90%, i knew it was bad for me to go the other direction.. thanks a lot!
I also ended up inheriting the collectionbase.. its a nice feature

Magiaus
Sep 3rd, 2002, 04:57 AM
post the version that you implamented the collectionbase in i want to see if you tried to work it the sameway i did or if you took another route. I was getting errors because of form to object conversion and some latebinding errors as well.

Bucho
Sep 3rd, 2002, 05:09 AM
Ok, I actually made a couple of different collections but i'll give you just one as an example..

Here is the Class:
----------------------------------
Option Compare Binary
Option Explicit On
Option Strict On


Namespace Database.Collections

Public Class LeftDataPanels

Inherits System.Collections.CollectionBase

Public Sub Add(ByVal Frm As frmData)
List.Add(Frm)
End Sub


Public Sub Remove(ByVal index As Integer)

If index > Count - 1 Or index < 0 Then

System.Windows.Forms.MessageBox.Show("Index not valid!")
Else
' Invokes the RemoveAt method of the List object.
List.RemoveAt(index)
End If
End Sub

Public ReadOnly Property Item(ByVal index As Integer) As frmData
Get
Return CType(List.Item(index), frmData)
End Get
End Property

End Class
End Namespace


----------------------------
Then I use this in a module:
-------------------------------

Module PublicModule

Public LeftPanels As New Database.Collections.LeftDataPanels()

End Module

-----------------------------------
Within a Subroutine here's how i use some of the functions:
------------------------------------------
'frmData is a form made in design mode

'To Add..
Dim FRM As New frmData()
FRM = New frmData()
LeftPanels.Add(FRM)

'To Remove..
LeftPanels.Remove(Me.Positioning)
'Positioning is just an index variable I used, Its a Short

'To set a property on one of the LeftPanel Collection Items..
LeftPanels.Item(i).Label1.Text = "Hello"
------------------------------------------------------

I had a few problems when trying to do it all without the Collection.CollectionBase

This will basically make my project 90% easier and more efficient
:D

Bucho
Sep 3rd, 2002, 05:19 AM
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconcollectionpropertypatterns.asp


here's where i got most of it, it's almost cut and paste

Magiaus
Sep 3rd, 2002, 05:23 AM
now i am going to have to rewrite it and see if i can get it to work the .net way instead of the vb6 way

Bucho
Sep 3rd, 2002, 05:44 AM
Let me know what ya get