Solved

Calling event in child form using ToolStripMenuItems on parent form

Posted on 2011-03-10
7
483 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
Comment Utility
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
Comment Utility
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
Comment Utility
Ok, that means that you have to change this line

frmTreeView.btnSave_Click(sender, e)

For this one

formactive.btnSave_Click(sender, e)
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Author Comment

by:alsam
Comment Utility
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
Comment Utility
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
Comment Utility
Works...
Thank you very much for your help
0
 
LVL 15

Expert Comment

by:x77
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now