Solved

Calling event in child form using ToolStripMenuItems on parent form

Posted on 2011-03-10
7
497 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

822 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