Solved

Calling event in child form using ToolStripMenuItems on parent form

Posted on 2011-03-10
7
505 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to repeat the data 4 30
vb.net deleting excel sheet in workbook 11 34
vb.net background worker crossthread 11 30
VB.net VSTO Excel Ribbon error 4 13
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 …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

733 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