Results 1 to 27 of 27

Thread: [RESOLVED] Open Dynamic Child Form

  1. #1

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Resolved [RESOLVED] Open Dynamic Child Form

    Hi all,

    I've got a range of child forms which I want to open with a treeview.

    Now I get the name of the form I need to open from the following code:

    VB Code:
    1. Private Sub JMSTreeView_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles JMSTreeView.AfterSelect
    2.  
    3.         Me.lblTreeTitle.Text = "MAIN MENU | " & JMSTreeView.SelectedNode.FullPath
    4.  
    5.         '==========================================================================================
    6.         Dim FileToOpen As String
    7.         For intLoopIndex As Integer = 0 To (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows.Count) - 1
    8.             If TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(0) = e.Node.Tag Then
    9.                 FileToOpen = (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(6).ToString)
    10.             End If
    11.         Next intLoopIndex
    12.  
    13.         '=====================================================================================


    Now my mind is saying to me that this should work. To open the form, I used the following code:

    VB Code:
    1. Dim MdiFrm As FrmMAIN
    2.         Me.GlobalForm = Me
    3.         Dim FrmNewChild As New FileToOpen
    4.         FrmNewChild.MdiParent = MdiFrm
    5.         'Me.Hide()
    6.         FrmNewChild.Show()
    7.  
    8.     End Sub

    Now I am getting at line: Dim FrmNewChild As New FileToOpen
    The error: Type 'FileToOpen' is not defined

    How do I get the right form to be opened, or how do I get the code to accept "FileToOpen" as the form to be opened?

    Thanks

    Rudi

  2. #2

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Re: Open Dynamic Child Form

    Hey people,

    Does anyone know how to do this or anything related? I'm kinda stuck util I figure this out.

    Thanks

    Rudi

  3. #3
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Open Dynamic Child Form

    just create a class that loop through your mdiparent. Pass the name of your form.
    just like this one
    VB Code:
    1. Public Sub ShowChild(ByVal mdiparent As Form, ByVal FileToOpen As String)
    2.         Dim f As Form
    3.         For Each f In mdiparent.MdiChildren
    4.             If f.Name = FileToOpen Then
    5.                 f.MdiParent = mdiparent
    6.                 f.Show()
    7.                 Return
    8.             End If
    9.         Next
    10.     End Sub

  4. #4

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Re: Open Dynamic Child Form

    hi...

    Ok, um... I'm a bit stuck... how do I open the form in my treeview's click event?

    this is the current code:

    VB Code:
    1. Private Sub JMSTreeView_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles JMSTreeView.AfterSelect
    2.  
    3.         Me.lblTreeTitle.Text = "MAIN MENU | " & JMSTreeView.SelectedNode.FullPath
    4.  
    5.         '==========================================================================================
    6.         Dim FileToOpen As String
    7.         Dim FileDescription As String
    8.         For intLoopIndex As Integer = 0 To (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows.Count) - 1
    9.             If TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(0) = e.Node.Tag Then
    10.                 FileToOpen = (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(6).ToString)
    11.                 FileDescription = (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(7).ToString)
    12.             End If
    13.         Next intLoopIndex
    14.  
    15.         '=====================================================================================
    16.         Me.lblDescription.Text = FileDescription
    17.     End Sub
    18.     Public Sub ShowChild(ByVal mdiparent As Form, ByVal FileToOpen As String)
    19.         Dim f As Form
    20.         For Each f In mdiparent.MdiChildren
    21.             If f.Name = FileToOpen Then
    22.                 f.MdiParent = mdiparent
    23.                 f.Show()
    24.                 Return
    25.             End If
    26.         Next
    27.     End Sub

    Btw.... The treeview is in an mdi child form aswell.
    Last edited by Tjoppie; Aug 19th, 2005 at 07:46 AM.

  5. #5
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Open Dynamic Child Form

    ok a little bit changes in my code.
    You must pass an instance of your form.
    like this one.
    VB Code:
    1. Public Sub ShowChild(ByVal mdiparent As Form, ByVal name As String, ByVal child As Form)
    2.         Dim f As Form
    3.         For Each f In mdiparent.MdiChildren
    4.             If f.Name = name Then
    5.                 f.Activate()
    6.                 Return
    7.             End If
    8.         Next
    9.         child.MdiParent = mdiparent
    10.         child.Show()
    11.     End Sub
    12.  
    13. 'you must check on what form referring to a variable FileToOpen
    14. if FileToOpen="Form 1" then
    15. ShowChild(mdiForm,"name of your form",[b]New Form1()[/b])
    16. end if
    17.  
    18. 'see i give an instance of form1 depends on what correspand to the variable FileToOpen and you can't go directly pass the string.

  6. #6
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472

    Re: Open Dynamic Child Form

    hi, Tjoppie, take a look at this sample might helps to you i have learned it in this forum.

    what i want here is just to show only the forms dynamically selected through a string name of it, so i just used a listview control not the one you specify in your post.

    let us assume that:
    Form1 ' is mdiparent and run Form3 under of it as child form

    Form3:
    VB Code:
    1. Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
    2.         If (ListView1.SelectedItems.Count > 0) Then
    3.             Dim li As ListViewItem = ListView1.SelectedItems(0)
    4.             Dim f As Form
    5.             f = CType(ShowChild(li.SubItems(0).Text), Form) ' name of form selected in the listview and create instance of it
    6.             f.MdiParent = Me.MdiParent
    7.             f.Show()
    8.         End If
    9.     End Sub
    10.  
    11.     Public Function ShowChild(ByVal FormName As String) As System.Object
    12.         Dim s As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
    13.         Dim myTypes() As System.Type = s.GetTypes
    14.         For i As Integer = 0 To myTypes.Length - 1
    15.             If String.Compare(myTypes(i).Name, FormName) = 0 Then
    16.                 Return System.Activator.CreateInstance(myTypes(i))
    17.             End If
    18.         Next
    19.         Return Nothing
    20.     End Function

    hope that helps to you.

  7. #7

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Re: Open Dynamic Child Form

    Hi Fret,

    I'm still Battling. Please look at the following and gimme your views. This is the code that I've got:

    VB Code:
    1. Private Sub JMSTreeView_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles JMSTreeView.AfterSelect
    2.  
    3.         Me.lblTreeTitle.Text = "MAIN MENU | " & JMSTreeView.SelectedNode.FullPath
    4.  
    5.         '==========================================================================================
    6.         Dim FileToOpen As String
    7.         Dim FileDescription As String
    8.         Dim f As Form
    9.         For intLoopIndex As Integer = 0 To (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows.Count) - 1
    10.             If TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(0) = e.Node.Tag Then
    11.                 FileToOpen = (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(6).ToString)
    12.                 FileDescription = (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(7).ToString)
    13.             End If
    14.         Next intLoopIndex
    15.         '=====================================================================================
    16.         Me.lblDescription.Text = FileDescription
    17.     End Sub

    FileToOpen give me the name of the form, as the form name does not exist in the treeview Control. But that operation is shown as a string, and if I say summin like

    F = (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(6).ToString)

    Then is says it can't convert windows.forms. summin to string... What can be wrong?

    Thanks

    Rudi

  8. #8
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472

    Re: Open Dynamic Child Form

    if i'm not mistaken your trying to convert or instantiate a form through its name in the treeview? If that so you need to do some reflection here to create an instance of your form which corresponds to your treeview event, I have noticed that your trying to pass a string (Form name) and trying to show it.

  9. #9

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Re: Open Dynamic Child Form

    Hi fret, yeah that's what I'm trying to do, but the form name is not in the treeview. Only labels and an index which gives a "friendly" name for the form.

    FileToOpen = (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(6).ToString)


    Looks trough the database table and finds the name of the form in the database table, of the treenode that was clicked. I can view the form's name in a textbow if I assign the text property to FileToOpen. but I don't know how to create that instance, and how to show that file.

    PS: The menu the treeview is on is a child of the main MDI container. I want all the forms from my treeview to open on top of the treeview form.

    The treeview is like a main menu of my applicaition.

    Any ideas?

    Thanks,

    Rudi

    PS: Let me know if you need more background.

  10. #10
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472

    Re: Open Dynamic Child Form

    Quote Originally Posted by Tjoppie
    Hi fret, yeah that's what I'm trying to do, but the form name is not in the treeview. Only labels and an index which gives a "friendly" name for the form.

    FileToOpen = (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(6).ToString)


    Looks trough the database table and finds the name of the form in the database table, of the treenode that was clicked. I can view the form's name in a textbow if I assign the text property to FileToOpen. but I don't know how to create that instance, and how to show that file.

    PS: The menu the treeview is on is a child of the main MDI container. I want all the forms from my treeview to open on top of the treeview form.

    The treeview is like a main menu of my applicaition.

    Any ideas?

    Thanks,

    Rudi

    PS: Let me know if you need more background.
    Hi, sorry for my misunderstood, but just i said if your problem is only to instantiate that form through form's name you need to do this.
    Let say that if you have already the form's name by assigning this:
    FileToOpen = (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(6).ToString)

    Now you need to pass that string to create an instance of it.
    VB Code:
    1. Public Function ShowChild(ByVal FormName As String) As System.Object
    2.         Dim s As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
    3.         Dim myTypes() As System.Type = s.GetTypes
    4.         For i As Integer = 0 To myTypes.Length - 1
    5.             If String.Compare(myTypes(i).Name, FormName) = 0 Then
    6.                 Return System.Activator.CreateInstance(myTypes(i))
    7.             End If
    8.         Next
    9.         Return Nothing
    10.     End Function

    usage: this is from an mdi child form or where you said that the treeview is putted.
    VB Code:
    1. Dim f As Form
    2.             f = CType(ShowChild(FileToOpen), Form)
    3.             f.MdiParent = Me.MdiParent
    4.             f.Show()


  11. #11

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Re: Open Dynamic Child Form

    Hi Fret,

    Sorry the delay but I'm now ready to try and get this thing working.

    I have now set my tree control on the main form, shich is the mdi container. I' ve made a docking control, so the treeview now shows up if you move to the left of the form etc etc.

    I now still want to open the form that's stated in the fileToOpen string, but as it is now located on the main form, what will this look like.

    Then, I've got a value that's called, ArgumentType. I want to construct a case statement that when the argument type is 0 , then go to selection0. which will be for rguments sake be to open the form, if it's 1, then end the application etc etc etc

    this is what my code looks like thus far:

    VB Code:
    1. Private Sub JMSTreeView_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles JMSTreeView.AfterSelect
    2.         '==========================================================================================
    3.         Dim FileToOpen As String
    4.         Dim FileDescription As String
    5.         Dim ArgumentType As String
    6.         For intLoopIndex As Integer = 0 To (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows.Count) - 1
    7.             If TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(0) = e.Node.Tag Then
    8.                 FileToOpen = (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(6).ToString)
    9.                 FileDescription = (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(7).ToString)
    10.                 ArgumentType = (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(5).ToString)
    11.  
    12.             End If
    13.         Next intLoopIndex
    14.         '=====================================================================================
    15.         'Me.statusbar1.Text = FileDescription
    16.         Try
    17.             Select Case
    18.         argumenttype = 0 goto selectoion0
    19.         argumenttype = 1 goto selection1
    20.  
    21.             End Select
    22.         Catch ex As Exception
    23.         End Try
    24.  
    25. selection0:
    26.         Dim f As Form
    27.         f = CType(ShowChild(FileToOpen), Form)
    28.         f.MdiParent = Me.MdiParent
    29.         f.Show()
    30.  
    31. Selection1:
    32.  
    33.     End Sub
    34.     Public Function ShowChild(ByVal FormName As String) As System.Object
    35.         Dim s As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
    36.         Dim myTypes() As System.Type = s.GetTypes
    37.         For i As Integer = 0 To myTypes.Length - 1
    38.             If String.Compare(myTypes(i).Name, FormName) = 0 Then
    39.                 Return System.Activator.CreateInstance(myTypes(i))
    40.             End If
    41.         Next
    42.         Return Nothing
    43.     End Function

    The code is not working as yet...

    Any ideas?

    Thanks

    Rudi

  12. #12
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472

    Re: Open Dynamic Child Form

    sorry for the mistake, i've noticed that the treeview control is in the form main, so it will be look like this.
    VB Code:
    1. selection0:
    2.         Dim f As Form
    3.         f = CType(ShowChild(FileToOpen), Form)
    4.         'f.MdiParent = Me.MdiParent
    5.         f.MdiParent = Me
    6.         f.Show()

    and my question is, is there an error occur?

  13. #13

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Re: Open Dynamic Child Form

    Hi Fret,

    Yes I'm getting the error "Object Refrence not set to an instance of an object"

    at the line

    f.MdiParent = Me

    Any ideas?

    Rudi

  14. #14
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472

    Re: Open Dynamic Child Form

    i'm guessing that your passing string name of the form which was not present.

  15. #15

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Re: Open Dynamic Child Form

    Hi Fret,

    Well, I'm sure that the syntac of the forms are correct. All my forms are kept in a project called JMSForms... so to refer to those forms I would use JMSForms.FrmWeighbridge etc etc. Shouldnt I use instead of Dim f As Form
    rather use Dim f as New Form?

    Thanks

    Rudi

  16. #16
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472

    Re: Open Dynamic Child Form

    Quote Originally Posted by Tjoppie
    Hi Fret,

    Well, I'm sure that the syntac of the forms are correct. All my forms are kept in a project called JMSForms... so to refer to those forms I would use JMSForms.FrmWeighbridge etc etc. Shouldnt I use instead of Dim f As Form
    rather use Dim f as New Form?

    Thanks

    Rudi
    VB Code:
    1. f = CType(ShowChild(FileToOpen), Form) ' this will return an instance of the form
    i think your passing the whole reference "JMSForms.FrmWeighbridge", just try to pass the form name itself do not include the project.

  17. #17

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Re: Open Dynamic Child Form

    Hi Fret,

    I just changed my form name in the database to only the name, not the project aswell, but I'm still getting the same.....

    Any ideas?

  18. #18
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472

    Re: Open Dynamic Child Form

    hmmm....as what i've experience that whenever i pass string in that function which is not present in the project it throws "Object reference not set to an instance of an object" so you better check the name carefully that meets the exactly the string you pass.

  19. #19

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Re: Open Dynamic Child Form

    Hi Fret,

    Checked again, I've made some screenshots, dunno if it will help... I'm lost.
    You will see in my titlebar is the string that filetoopen will give.

    Your'e not on Skype or MSN maybe?

    Thanks

    Rudi
    Attached Images Attached Images   
    Last edited by Tjoppie; Aug 26th, 2005 at 05:50 AM.

  20. #20
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472

    Re: Open Dynamic Child Form

    arrggh! i'm getting lost also

    ok here's what i can suggest, just a suggestion make some simple
    sample that would use the function that i've given to you,
    let say you have a form, then on that form you have a button command:
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim f As Form
    3.         f = CType(ShowChild("Form1"), Form) 'knowing that Form1 is present in the project
    4.         f.Show()
    5.     End Sub
    6.  
    7.     Public Function ShowChild(ByVal FormName As String) As System.Object
    8.         Dim s As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
    9.         Dim myTypes() As System.Type = s.GetTypes
    10.         For i As Integer = 0 To myTypes.Length - 1
    11.             If String.Compare(myTypes(i).Name, FormName) = 0 Then
    12.                 Return System.Activator.CreateInstance(myTypes(i))
    13.             End If
    14.         Next
    15.         Return Nothing
    16.     End Function
    if that works perfectly, then maybe the error is passing the string from the database to FileToOpen, then try to check if FileToOpen contains data.

  21. #21

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Re: Open Dynamic Child Form

    I'm trying it now... Did u get my pm?

  22. #22

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Re: Open Dynamic Child Form

    That's not working aswell...Still same error

  23. #23
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472

    Re: Open Dynamic Child Form

    it works fine on here, i think there some kind of slight mistake. It's getting late now here maybe i can help it tommorrow on msn.

  24. #24
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Open Dynamic Child Form

    Are the forms in the SAME project? or in a DIFFERENT one?

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  25. #25

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Re: Open Dynamic Child Form

    It's in a different project

  26. #26

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Re: Open Dynamic Child Form

    Hi Fret,

    Ok,.... I tried by just creating a new form in THE SAME project. Then inserting that in my table, works fine. So in my argument coloumn I have the value "FrmSplash" This opens the forms fine..

    The problem I have is: All my forms, are kept in a class library (another project) called "JMSForms" Now that is refrenced and all, and when I refer to one of my forms, then it's normally "JMSForms.FrmWeighbridge", but inserting that in my table, then gives an error....

    any help?

    Thanks
    Rudi

  27. #27

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Re: Open Dynamic Child Form

    Any Ideas on how I can get this one solved?

    Thanks

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