2

It is my first ever question post in any site, hope you will welcome me by helping me. I am working on a vb.net windows application where I am using treeview in my main form as software's menu. I am trying to open new forms by pressing enter or clicking on treenodes. Everything works fine when I press enter on the treenode but when I click on a treenode new form open behind the main form. Please help me how to show the new form in front of the main form (setting TopMost property or MDIParent is not appropriate for my application). Thanks in advance

Private Sub TreeView1_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick
     If e.Node.Text = "Purchase" Then
          Dim frm As New frm_purchase
          frm.Show()
     End If
End Sub

Private Sub TreeView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TreeView1.KeyDown
        If e.KeyData = Keys.Enter Then
            If TreeView1.SelectedNode.Text = "Purchase" Then
                Dim frm As New frm_purchase
                frm.Show()
            End If
        End If
End Sub 
Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
ER.
  • 77
  • 4

2 Answers2

1

The .Show() method has one overload to indicate the Owner of the form. Try using this instead of the default:

Private Sub TreeView1_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick
 If e.Node.Text = "Purchase" Then
      Dim frm As New frm_purchase
      frm.Show(ParentForm)
 End If

ParentForm in this case, refers to your MDI container.

You can use this method to display a non-modal form. When you use this method, the Owner property of the form is set to owner. The non-modal form can use the Owner property to get information about the owning form. Calling this method is identical to setting the Owner property of the non-modal and then calling the Show() method.

Hanlet Escaño
  • 17,114
  • 8
  • 52
  • 75
0

Try this:

Private Sub TreeView1_NodeMouseClick(sender As Object, e As TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick
    Dim frm As New frm_purchase
    frm.Show()
End Sub

Private Sub frm_purchase_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
    Me.TopMost = False
End Sub

Private Sub frm_purchase_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.TopMost = True
End Sub

This solution will make your form appear on top when first created, but will not ALWAYS overlap the previous form, if that is the behaviour you want. Otherwise simply setting the owner of the new form to your current one will suffice (as per Hanlet's answer)

A Friend
  • 2,750
  • 2
  • 14
  • 23
  • Thanks its working but I have another problem. Different modules of this application is developed as .dll file (by different teams & I don't have the source code) which I have include in my app as reference file and I need to open forms of these dll. but I cant change the internal code of these dll. Can you help me on this. – ER. Jun 29 '16 at 16:05