Results 1 to 22 of 22

Thread: Multiple Form Data Sharing (or control manipulation)

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Posts
    15

    Question Multiple Form Data Sharing (or control manipulation)

    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!!
    Last edited by Bucho; Aug 30th, 2002 at 07:56 AM.

  2. #2

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Posts
    15
    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?

  3. #3
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    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.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  4. #4
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    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.p...hreadid=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......
    Magiaus

    If I helped give me some points.

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Posts
    15
    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?

  6. #6
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    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.
    Magiaus

    If I helped give me some points.

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Posts
    15
    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..
    Last edited by Bucho; Aug 30th, 2002 at 04:08 PM.

  8. #8

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Posts
    15
    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.

  9. #9
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    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
    VB Code:
    1. For Each Frm In Col
    2.     If Frm.Tag = "The one i passed in to the method" Then
    3.           useIt
    4.     End If
    5. 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

    If I helped give me some points.

  10. #10
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    ok this is basic

    here is a form collection very basic very working

    this is the main procedure that starts the app
    VB Code:
    1. Module intMain
    2.     Private mFrmz As New Magaus.Collections.Forms()
    3.     Public Sub Main()
    4.         Dim Frm As Form = New FormA()
    5.         Dim i As Integer = 0
    6.         mFrmz.AddForm(Frm, CStr(i))
    7.         For i = 1 To 10
    8.             Frm = New FormA()
    9.             mFrmz.AddForm(Frm, CStr(i))
    10.         Next
    11.  
    12.         For i = 0 To 10
    13.             mFrmz.GetForm(CStr(i)).show()
    14.             mFrmz.GetForm(CStr(i)).text = CStr(i)
    15.         Next
    16.  
    17.  
    18.     End Sub
    19. End Module

    this is the forms collection class
    VB Code:
    1. Option Explicit On
    2. Option Strict On
    3.  
    4. 'Imports System.Collections
    5.  
    6. Namespace Magaus.Collections
    7.     Public Class Forms
    8.         Private col As New Collection()
    9.         Private mFrm As Object
    10.  
    11.         Public Sub AddForm(ByVal Frm As Object, ByVal Key As String)
    12.             mFrm = Frm
    13.             col.Add(mFrm, Key)
    14.             Frm = Nothing
    15.             mFrm = Nothing
    16.         End Sub
    17.  
    18.         Public Function GetForm(ByVal key As String) As Object
    19.             GetForm = col(key)
    20.         End Function
    21.  
    22.         Public Sub Remove(ByVal key As String)
    23.             col.Remove(key)
    24.         End Sub
    25.     End Class
    26. 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
    Magiaus

    If I helped give me some points.

  11. #11

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Posts
    15
    Thanks a lot Magiaus, i just saw this. I"m going to try this immediately and let you know if it worked.

  12. #12
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Here is the msdn library answer to your problem.

    http://msdn.microsoft.com/library/de...albasicnet.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.

  13. #13

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Posts
    15
    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..

  14. #14
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    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

    If I helped give me some points.

  15. #15
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    i can't even spell inheratince umm inherits CollectionBase yeah
    Magiaus

    If I helped give me some points.

  16. #16
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Cool oh

    I worked on it a little more the other day but never posted it.
    here

    i spelled my namespace right as well

    VB Code:
    1. #Region "Options"
    2. Option Compare Binary
    3. Option Explicit On
    4. Option Strict On
    5. #End Region
    6.  
    7. Namespace Magiaus.Collections
    8. #Region "Example"
    9.     '   Note:   The forms used in this example are created at runtime and
    10.     '           do not exist in a project. Also I am using a public instance
    11.     '           of Magiaus.Collections.Forms decalared in a module.
    12.     '
    13.     '   Module Cmn
    14.     '       Public Forms As New Magiaus.Collections.Forms()
    15.     '   End Module
    16.     '
    17.     '   Adding Forms
    18.     '   ****************************************************************************
    19.     '           Dim i As System.Int32, frm As New System.Windows.Forms.Form()
    20.     '           For i = 1 To 20
    21.     '               Forms.Add(frm, CStr(i))
    22.     '               With Forms(CStr(i))
    23.     '                   .Text = "I am runtime form #" & CStr(i)
    24.     '                   .Tag = CStr(i)
    25.     '                   .size = New System.Drawing.Size(375, 0)
    26.     '                   .show()
    27.     '               End With
    28.     '               If i = 20 Then
    29.     '                   frm = Nothing
    30.     '               Else
    31.     '                   frm = New System.Windows.Forms.Form()
    32.     '               End If
    33.     '           Next
    34.     '   ****************************************************************************
    35.     '
    36.     '   Removing Forms
    37.     '   ****************************************************************************
    38.     '       Dim i As System.Int32
    39.     '       For i = 1 To 20
    40.     '           Forms(CStr(i)).dispose()    'alternatly you could place the
    41.     '           Forms.Remove(CStr(i))       'remove code in a forms dispose method
    42.     '       Next
    43.     '   ****************************************************************************
    44.     '
    45.     '   Manipulating Forms once they are in the collection
    46.     '   ****************************************************************************
    47.     '       To access a form that you have put ino the collection you must use
    48.     '       either the Key or the index of the form. If you add a form and do not
    49.     '       specify a key the forms name will be used.
    50.     '      
    51.     '       Forms("FormKey).Show()
    52.     '
    53.     '       Note: at this point the forms properies are not exposed to intellsense
    54. #End Region
    55.     Public Class Forms
    56.  
    57.         Private Frms As New Collection()
    58.  
    59.  
    60.         Public Sub Add(ByVal Frm As System.Windows.Forms.Form)
    61.             Frms.Add(Frm, Frm.Name)
    62.         End Sub
    63.  
    64.  
    65.         Public Sub Add(ByVal Frm As System.Windows.Forms.Form, ByVal Key As System.String)
    66.             Frms.Add(Frm, Key)
    67.         End Sub
    68.  
    69.  
    70.         Public ReadOnly Property Count() As System.Int32
    71.             Get
    72.                 Count = Frms.Count
    73.             End Get
    74.         End Property
    75.  
    76.  
    77.         Public Sub Remove(ByVal Key As System.String)
    78.             Frms.Remove(Key)
    79.         End Sub
    80.  
    81.  
    82.         Public Sub Remove(ByVal Index As System.Int32)
    83.             Frms.Remove(Index)
    84.         End Sub
    85.  
    86.  
    87.         Default Public ReadOnly Property Form(ByVal Index As System.Int32) As Object
    88.             Get
    89.                 Form = Frms.Item(Index)
    90.             End Get
    91.         End Property
    92.  
    93.  
    94.         Default Public ReadOnly Property Form(ByVal Key As System.String) As Object
    95.             Get
    96.                 Form = Frms.Item(Key)
    97.             End Get
    98.         End Property
    99.     End Class
    100. End Namespace

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

    If I helped give me some points.

  17. #17

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Posts
    15
    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

  18. #18
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    hey

    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.
    Magiaus

    If I helped give me some points.

  19. #19

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Posts
    15
    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
    Last edited by Bucho; Sep 3rd, 2002 at 05:18 AM.

  20. #20

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Posts
    15
    http://msdn.microsoft.com/library/de...typatterns.asp


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

  21. #21
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    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
    Magiaus

    If I helped give me some points.

  22. #22

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Posts
    15
    Let me know what ya get

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