|
-
Aug 18th, 2005, 09:49 AM
#1
Thread Starter
Addicted Member
[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:
Private Sub JMSTreeView_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles JMSTreeView.AfterSelect
Me.lblTreeTitle.Text = "MAIN MENU | " & JMSTreeView.SelectedNode.FullPath
'==========================================================================================
Dim FileToOpen As String
For intLoopIndex As Integer = 0 To (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows.Count) - 1
If TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(0) = e.Node.Tag Then
FileToOpen = (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(6).ToString)
End If
Next intLoopIndex
'=====================================================================================
Now my mind is saying to me that this should work. To open the form, I used the following code:
VB Code:
Dim MdiFrm As FrmMAIN
Me.GlobalForm = Me
Dim FrmNewChild As New FileToOpen
FrmNewChild.MdiParent = MdiFrm
'Me.Hide()
FrmNewChild.Show()
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
-
Aug 19th, 2005, 03:54 AM
#2
Thread Starter
Addicted Member
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
-
Aug 19th, 2005, 04:27 AM
#3
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:
Public Sub ShowChild(ByVal mdiparent As Form, ByVal FileToOpen As String)
Dim f As Form
For Each f In mdiparent.MdiChildren
If f.Name = FileToOpen Then
f.MdiParent = mdiparent
f.Show()
Return
End If
Next
End Sub
-
Aug 19th, 2005, 07:40 AM
#4
Thread Starter
Addicted Member
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:
Private Sub JMSTreeView_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles JMSTreeView.AfterSelect
Me.lblTreeTitle.Text = "MAIN MENU | " & JMSTreeView.SelectedNode.FullPath
'==========================================================================================
Dim FileToOpen As String
Dim FileDescription As String
For intLoopIndex As Integer = 0 To (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows.Count) - 1
If TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(0) = e.Node.Tag Then
FileToOpen = (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(6).ToString)
FileDescription = (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(7).ToString)
End If
Next intLoopIndex
'=====================================================================================
Me.lblDescription.Text = FileDescription
End Sub
Public Sub ShowChild(ByVal mdiparent As Form, ByVal FileToOpen As String)
Dim f As Form
For Each f In mdiparent.MdiChildren
If f.Name = FileToOpen Then
f.MdiParent = mdiparent
f.Show()
Return
End If
Next
End Sub
Btw.... The treeview is in an mdi child form aswell.
Last edited by Tjoppie; Aug 19th, 2005 at 07:46 AM.
-
Aug 19th, 2005, 07:58 PM
#5
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:
Public Sub ShowChild(ByVal mdiparent As Form, ByVal name As String, ByVal child As Form)
Dim f As Form
For Each f In mdiparent.MdiChildren
If f.Name = name Then
f.Activate()
Return
End If
Next
child.MdiParent = mdiparent
child.Show()
End Sub
'you must check on what form referring to a variable FileToOpen
if FileToOpen="Form 1" then
ShowChild(mdiForm,"name of your form",[b]New Form1()[/b])
end if
'see i give an instance of form1 depends on what correspand to the variable FileToOpen and you can't go directly pass the string.
-
Aug 19th, 2005, 10:28 PM
#6
Hyperactive Member
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:
Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
If (ListView1.SelectedItems.Count > 0) Then
Dim li As ListViewItem = ListView1.SelectedItems(0)
Dim f As Form
f = CType(ShowChild(li.SubItems(0).Text), Form) ' name of form selected in the listview and create instance of it
f.MdiParent = Me.MdiParent
f.Show()
End If
End Sub
Public Function ShowChild(ByVal FormName As String) As System.Object
Dim s As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
Dim myTypes() As System.Type = s.GetTypes
For i As Integer = 0 To myTypes.Length - 1
If String.Compare(myTypes(i).Name, FormName) = 0 Then
Return System.Activator.CreateInstance(myTypes(i))
End If
Next
Return Nothing
End Function
hope that helps to you.
-
Aug 22nd, 2005, 01:38 AM
#7
Thread Starter
Addicted Member
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:
Private Sub JMSTreeView_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles JMSTreeView.AfterSelect
Me.lblTreeTitle.Text = "MAIN MENU | " & JMSTreeView.SelectedNode.FullPath
'==========================================================================================
Dim FileToOpen As String
Dim FileDescription As String
Dim f As Form
For intLoopIndex As Integer = 0 To (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows.Count) - 1
If TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(0) = e.Node.Tag Then
FileToOpen = (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(6).ToString)
FileDescription = (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(7).ToString)
End If
Next intLoopIndex
'=====================================================================================
Me.lblDescription.Text = FileDescription
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
-
Aug 22nd, 2005, 01:47 AM
#8
Hyperactive Member
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.
-
Aug 22nd, 2005, 02:00 AM
#9
Thread Starter
Addicted Member
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.
-
Aug 22nd, 2005, 08:43 PM
#10
Hyperactive Member
Re: Open Dynamic Child Form
 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:
Public Function ShowChild(ByVal FormName As String) As System.Object
Dim s As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
Dim myTypes() As System.Type = s.GetTypes
For i As Integer = 0 To myTypes.Length - 1
If String.Compare(myTypes(i).Name, FormName) = 0 Then
Return System.Activator.CreateInstance(myTypes(i))
End If
Next
Return Nothing
End Function
usage: this is from an mdi child form or where you said that the treeview is putted.
VB Code:
Dim f As Form
f = CType(ShowChild(FileToOpen), Form)
f.MdiParent = Me.MdiParent
f.Show()
-
Aug 26th, 2005, 04:19 AM
#11
Thread Starter
Addicted Member
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:
Private Sub JMSTreeView_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles JMSTreeView.AfterSelect
'==========================================================================================
Dim FileToOpen As String
Dim FileDescription As String
Dim ArgumentType As String
For intLoopIndex As Integer = 0 To (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows.Count) - 1
If TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(0) = e.Node.Tag Then
FileToOpen = (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(6).ToString)
FileDescription = (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(7).ToString)
ArgumentType = (TreeData.Tables("Qry_Admin_TreeMenu_VBNET").Rows(intLoopIndex).Item(5).ToString)
End If
Next intLoopIndex
'=====================================================================================
'Me.statusbar1.Text = FileDescription
Try
Select Case
argumenttype = 0 goto selectoion0
argumenttype = 1 goto selection1
End Select
Catch ex As Exception
End Try
selection0:
Dim f As Form
f = CType(ShowChild(FileToOpen), Form)
f.MdiParent = Me.MdiParent
f.Show()
Selection1:
End Sub
Public Function ShowChild(ByVal FormName As String) As System.Object
Dim s As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
Dim myTypes() As System.Type = s.GetTypes
For i As Integer = 0 To myTypes.Length - 1
If String.Compare(myTypes(i).Name, FormName) = 0 Then
Return System.Activator.CreateInstance(myTypes(i))
End If
Next
Return Nothing
End Function
The code is not working as yet...
Any ideas?
Thanks
Rudi
-
Aug 26th, 2005, 04:38 AM
#12
Hyperactive Member
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:
selection0:
Dim f As Form
f = CType(ShowChild(FileToOpen), Form)
'f.MdiParent = Me.MdiParent
f.MdiParent = Me
f.Show()
and my question is, is there an error occur?
-
Aug 26th, 2005, 04:51 AM
#13
Thread Starter
Addicted Member
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
-
Aug 26th, 2005, 04:57 AM
#14
Hyperactive Member
Re: Open Dynamic Child Form
i'm guessing that your passing string name of the form which was not present.
-
Aug 26th, 2005, 05:03 AM
#15
Thread Starter
Addicted Member
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
-
Aug 26th, 2005, 05:15 AM
#16
Hyperactive Member
Re: Open Dynamic Child Form
 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:
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.
-
Aug 26th, 2005, 05:22 AM
#17
Thread Starter
Addicted Member
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?
-
Aug 26th, 2005, 05:29 AM
#18
Hyperactive Member
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.
-
Aug 26th, 2005, 05:44 AM
#19
Thread Starter
Addicted Member
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
Last edited by Tjoppie; Aug 26th, 2005 at 05:50 AM.
-
Aug 26th, 2005, 09:42 AM
#20
Hyperactive Member
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:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f As Form
f = CType(ShowChild("Form1"), Form) 'knowing that Form1 is present in the project
f.Show()
End Sub
Public Function ShowChild(ByVal FormName As String) As System.Object
Dim s As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
Dim myTypes() As System.Type = s.GetTypes
For i As Integer = 0 To myTypes.Length - 1
If String.Compare(myTypes(i).Name, FormName) = 0 Then
Return System.Activator.CreateInstance(myTypes(i))
End If
Next
Return Nothing
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.
-
Aug 26th, 2005, 09:53 AM
#21
Thread Starter
Addicted Member
Re: Open Dynamic Child Form
I'm trying it now... Did u get my pm?
-
Aug 26th, 2005, 09:57 AM
#22
Thread Starter
Addicted Member
Re: Open Dynamic Child Form
That's not working aswell...Still same error
-
Aug 26th, 2005, 10:14 AM
#23
Hyperactive Member
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.
-
Aug 26th, 2005, 11:37 AM
#24
Re: Open Dynamic Child Form
Are the forms in the SAME project? or in a DIFFERENT one?
Tg
-
Aug 28th, 2005, 11:50 PM
#25
Thread Starter
Addicted Member
Re: Open Dynamic Child Form
It's in a different project
-
Aug 29th, 2005, 07:01 AM
#26
Thread Starter
Addicted Member
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
-
Sep 1st, 2005, 04:44 AM
#27
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|