Solved

Calling event in child form using ToolStripMenuItems on parent form

Posted on 2011-03-10
7
500 Views
Last Modified: 2012-05-11
Hi,
I thought that I have found solution, but in my case it don't works OK..
I have frmTreeView with Save button (triggers btnSave_Click)  on the same that works OK (I use the save button for saving changes against db in my treeview). Now, I want to use Standard Menu Strip and SaveToolStripMenuItem_Click to trigger the event behind btnSave_Click on my frmTreeView to save changes...
Let me say that frmTreeView is child form and Standard menu is attached to parent...
Below you can find my code... (i excluded with purpose not necessery events like loading tree or drag and drop from the code snnipet)...
In line
If oTV.GetNodeCount(False) > 0 Then GetNodeCount returns 0 whan is trigerred by SaveToolStripMenuItem_Click so execution is skiped and nothing is saved against database...
Please could you help me adjust my code in order to make this working
Thanks
' this is code behind parent form
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        Dim formactive As Form = Form.ActiveForm.ActiveMdiChild
        If formactive.Name = "frmTreeView" Then
            frmTreeView.btnSave_Click(sender, e)
        End If
    End Sub

' this is code behind frmTreeView form

Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms



Public Class frmTreeView
    Inherits System.Windows.Forms.Form

    '// NOTE: These variables are shared so they can be accessed by other forms.
    Public Shared g_oTv As TreeView
    Public Shared g_bState_NodesDirty As Boolean
    Public Shared g_bState_NodesPresent As Boolean
    Public Shared g_bState_NodeSelected As Boolean
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Dim CTree As New CTreeView()

    Dim m_bFormIsActivated As Boolean
    Dim m_tnSource As TreeNode = Nothing
    Private m_dreDragTolerance As Rectangle        '~~~ Line added.  (Drag tolerance rectangle)
    Dim da As New Data.SqlClient.SqlDataAdapter
    Dim ds As New DataSet


Public Sub btnSave_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) Handles btnSave.Click

        Dim tn As TreeNode

        If oTV.GetNodeCount(False) > 0 Then
            tn = oTV.Nodes(0)

        End If

        CTree.SaveNodeCollection(tn)

        '// Update the UI.
        g_bState_NodesDirty = False
        SetUIState(g_bState_NodesPresent, g_bState_NodeSelected, g_bState_NodesDirty)

    End Sub

 Private Sub oTv_AfterSelect(ByVal sender As System.Object, _
                                ByVal e As System.Windows.Forms.TreeViewEventArgs) _
                                Handles oTV.AfterSelect

        If Not (e.Node Is Nothing) Then
            g_bState_NodeSelected = True
        Else
            g_bState_NodeSelected = False
        End If
        SetUIState(g_bState_NodesPresent, g_bState_NodeSelected, g_bState_NodesDirty)
        
       

        Me.ListBox1.Items.Clear()
        RepopulateListBox()
End Sub

 Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub
End Class

Open in new window

0
Comment
Question by:alsam
  • 3
  • 3
7 Comments
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35099544
Hi,

In the parent form you have this

Dim formactive As Form = Form.ActiveForm.ActiveMdiChild
If formactive.Name = "frmTreeView" Then
            frmTreeView.btnSave_Click(sender, e)
End If

So, you are calling the BTNSAVE_CLICK event on frmTreeView, but you have to call it on formactive, since it is formactive that points to the ActiveMdiChild form

Besides btnSave control should have set the MODIFIERS property to PUBLIC

Hope that helps
0
 
LVL 1

Author Comment

by:alsam
ID: 35099772
Hi,
1. Modifiers set to Public following to your instructions,
2, " but you have to call it on formactive" . ... what did you mean by this.... what should i change in my code...
Thanks
0
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35099779
Ok, that means that you have to change this line

frmTreeView.btnSave_Click(sender, e)

For this one

formactive.btnSave_Click(sender, e)
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
LVL 1

Author Comment

by:alsam
ID: 35099840
Ok but then I get an error saying that
btnSave_Click is not a member of System.Windows.Forms.Form
What should I do about this?
0
 
LVL 13

Accepted Solution

by:
gamarrojgq earned 500 total points
ID: 35099897
Ok  Declare formactive as Object, so instead of this

Dim formactive As Form = Form.ActiveForm.ActiveMdiChild

use this line

Dim formactive As Object = Form.ActiveForm.ActiveMdiChild
0
 
LVL 1

Author Closing Comment

by:alsam
ID: 35099959
Works...
Thank you very much for your help
0
 
LVL 15

Expert Comment

by:x77
ID: 35100818
You need enumerate the MdiParent Forms to find a form from  frmTreeView class.
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click   
        Dim fMdi = Me.MdiParent
        For Each ChildForm As Form In fMdi.MdiChildren
            Dim f = TryCast(ChildForm, frmTreeView)
            If f IsNot Nothing Then
                f.btnSave_Click(sender, e)
                Return
            End If
            MessageBox.Show("frmTreeView not Found")
        Next        
    End Sub

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

860 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