Link to home
Start Free TrialLog in
Avatar of RookieTim
RookieTim

asked on

Viewing Forms from a TreeView Control

I would like to build all of my form navigation into a treeview control that is load from a database (Sql server).  I have a user, Jon Doe, as the base node and beneath him are the nodes that he has access to view, i.e., Form1, Form2, Form3, etc.  However, I'm not sure how to reference the value of the treeview node, "Form1", to my actual Form1 in solutions explorer.

Please advice

Rookie
Avatar of arif_eqbal
arif_eqbal

Well what I understand is that you would show a list of all forms a particular user has access to on a TreeView and when the user clicks on them you would open that form. If this is what you want and you are using VB.NET

Load the TreeView with the Names of All Forms as Nodes eg. Form1,Form2 etc.

Now on AfterSelect event of TreeView call a Function ShowForm and Pass the name of the form i.e. the TExt of the Node

Now in the function use Select Case on the Names to create reference of the particular form and show it....
ASKER CERTIFIED SOLUTION
Avatar of wguerram
wguerram

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This the code for creating the form

Remember that when adding the form name, you also have to add the form namespace

dim _node as new node

_node.Tag = "MayNameSpace.Form1"

TreeView1.Nodes.Add(_node)

'------------------
Dim f As Object

f = Activator.CreateInstance(Type.GetType(node.Tag, True))

If you have problems, please let me know it.
Avatar of RookieTim

ASKER

Wguerram-

This is works well.  However, my problem is that I want just a single instance of a form open at a given time?  Do you have a suggestion on how to maintain just one instance.

I was anticipating using this block of code, but it dosen't seem to be working as well as I'd have hoped.


' MyChildForm is the one I'm looking for which would be f in our instance
 
    Dim childForm As MyChildForm =  Nothing
    Dim c As Form
 
    For Each c In Me.MdiChildren
           If TypeOf c Is MyChildForm Then
               ' found it
               childForm = CType(c, MyChildForm)
               Exit For
          End If
     Next
 
     If Not childForm Is Nothing Then
          childForm.Show()
          childForm.Focus()
     Else
          childForm = New MyChildForm()
          childForm.MdiParent = Me
          childForm.Show()
          childForm.Focus()  
     End If
 
Thank you very much.
You're welcome!