Solved

Cannot open same form multiple times on treeview control unless I click another node and come back vb.net

Posted on 2016-07-28
2
49 Views
Last Modified: 2016-07-28
Hi all.

I have a treeview control (vb.net in Visual Studio) with multiple nodes, when the end user clicks on a node it opens a specific form. This is working fine, but I notice something peculiar:

When I click nodeA, it opens formA. I then decide to click on nodeB, and it opens formB as it should.

Now, let's say I click nodeA, it opens formA. Now I want to open another instance of formA so I click nodeA again but nothing happens, it doesn't open formA again. The only way to get formA to open again is to click on nodeB or another treeview level (parent node etc.) and then go back to nodeA and click it and then it'll open another instance of formA.

Any idea how I can open the same form multiple times by clicking the node multiple times without having to click some different node and then coming back to the original node to open the form multiple times?

Thank you in advance.

Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterSelect

        Dim con As New SqlConnection
        Dim cmd As New SqlCommand
        Dim frm As String

        con.ConnectionString = "Data Source=myServer;Initial Catalog=myDB;Integrated Security=True"

        con.Open()

        cmd.Connection = con

        If e.Node.Text = "MyApp" Then
            Exit Sub

        Else
            cmd.CommandText = "SELECT FormName FROM Menu WHERE Application = @Form"
            cmd.Parameters.AddWithValue("@Form", TreeView1.SelectedNode.Text)

            frm = cmd.ExecuteScalar()
            con.Close()

            If frm Is Nothing Then  'The selected node does not have a form
                Exit Sub
            Else

                Dim f As Form = GetFormByName(frm)

                f.Show()    'Open the form selected in the TreeView control

                Me.BringToFront()   'Bring the MyAppMenu form to the front to be able to have it appear behind the other forms in the next line
                Me.SendToBack()     'Send the MyAppMenu form behind the form just opened in the f.show() line
            End If
        End If

    End Sub

Private Function GetFormByName(ByVal pFormName As String) As Form
        'Try to create a type form using the name specific in pFormName. The False is to prevent an exception because in the If statement we are checking if the Type is null (meaning nothing found)
        Dim T As Type = Type.GetType(pFormName, False)

        If T Is Nothing Then    'If nothing is found then add the current namespace (ie. MyApp.Prospects)

            Dim strFullname As String = Application.ProductName & "." & pFormName
            T = Type.GetType(strFullname, True, True)   'The second True is to do a case-insensitive search for strFullname

        End If

        Return CType(Activator.CreateInstance(T), Form)

    End Function

Open in new window

0
Comment
Question by:printmedia
2 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 41733121
because the AfterSelect event only occurs when a new node is clicked. Try the NodeMouseClick event instead: https://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.nodemouseclick(v=vs.110).aspx
0
 

Author Comment

by:printmedia
ID: 41733149
Thanks again Eric. That was it. I changed my parameter code to be e.Node.Text instead of TreeView1.SelectedNode.Text. And it works like a charm now.

cmd.Parameters.AddWithValue("@Form", e.Node.Text)

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net - TypeInitializer Error 25 32
Creating a route in asp.net webforms 2 32
System.net.Sockets Error 5 27
VB.net Open video relating to control 2 20
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question