Solved

Calling event in child form using ToolStripMenuItems on parent form

Posted on 2011-03-10
7
512 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

705 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