Results 1 to 10 of 10

Thread: Reference a sub on another form

  1. #1
    Reggie213 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    May 2012
    Posts
    12
    Reputation
    16

    Reference a sub on another form

    Hi,

    So this is driving me half insane.

    I have an mdi form setup so here is my problem

    mdiParent : mdiMain
    mdichild : frmData
    Sub on frmData : Public Sub GenerateData()

    Form : popForm

    I open the parent then the child. In the menustrip on the parent I have an option that opens popForm using popForm.showdialog()

    On popForm there is a button that when clicked I want it to run a Public sub on frmData (the child form)

    Intellisense doesnt even pickup that form when I start typing it.. and just typing in the following

    frmData.Generatedata()

    ...comes up with the error frmMaindata is not declared.

    Thanks

    Reg

    vb.net 2.0, winform app, visual studio 2010

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,504
    Reputation
    1553

  3. #3
    Reggie213 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    May 2012
    Posts
    12
    Reputation
    16
    Hi,

    On the load event of the parent I do the following

    Dim frm As New Form()

    frm.MdiParent = Me
    frm.FormBorderStyle = FormBorderStyle.None
    frm.ControlBox = False
    frm.MinimizeBox = False
    frm.MaximizeBox = False
    frm.Show()
    frm.Dock = DockStyle.Fill


    Me.WindowState = FormWindowState.Maximized
    If blnActive = IsOpen("frmmaindata") Then
    Dim mainWindow As New lstViewCat


    mainWindow.MdiParent = Me
    mainWindow.StartPosition = FormStartPosition.CenterScreen
    mainWindow.Show()
    mainWindow.WindowState = FormWindowState.Maximized
    End If
    The popForm triggers from the toolbar on the parent

    Private Sub OptionsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles OptionsToolStripMenuItem.Click
    popForm.ShowDialog()
    End Sub

  4. #4
    Reggie213 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    May 2012
    Posts
    12
    Reputation
    16
    In simple terms.. the child form shows a dataset that is generated/refreshed using a public sub called generatedata()

    the popForm offers the ability to change some of the criteria, so I was just trying to call the refresh after the user hits save on the popForm!

  5. #5
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,504
    Reputation
    1553

  6. #6
    Reggie213 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    May 2012
    Posts
    12
    Reputation
    16
    Sorry ive changed the name to frmmaindata.. meant to adjust that to keep these posts in sync.. missed that oops

  7. #7
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,504
    Reputation
    1553
    I don't see any mention of frmMainData in there either, other than as a String passed to IsOpen. That doesn't show how you're opening the form you're trying to reference.

  8. #8
    Reggie213 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    May 2012
    Posts
    12
    Reputation
    16
    Please see isopen function.

    Thanks

    Private Function IsOpen(ByVal nameForm As String) As Boolean Dim childfrm As Form
    Dim strName As String
    Dim intLastIndex As Integer


    For Each childfrm In Me.MdiChildren
    strName = childfrm.GetType.ToString
    intLastIndex = strName.LastIndexOf(".")
    strName = Mid(strName, intLastIndex + 2, Len(strName) - intLastIndex)
    If LCase(strName) = LCase(nameForm) Then
    childfrm.BringToFront()
    Return True
    End If
    Next
    Return False




    End Function

  9. #9
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,504
    Reputation
    1553
    And still. That code simply checks whether a form of a specified type is open. Is doesn't open anything. Where are you opening this form?

  10. #10
    Reggie213 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    May 2012
    Posts
    12
    Reputation
    16
    Although you might not think you have provided me the answer you have...

    Originally this form was called lstViewCat.. I renamed it but didn't change the class name so at the top of the code it still stated Public Class lstViewCat

    DOH!!!

    Now ive renamed that class it all shows up in intellisense and allows me to reference the public sub

    frmData.Generatedata()

    And that should also explain how

    If blnActive = IsOpen("frmdata") Then
    Dim mainWindow As New frmData


    mainWindow.MdiParent = Me
    mainWindow.StartPosition = FormStartPosition.CenterScreen
    mainWindow.Show()
    mainWindow.WindowState = FormWindowState.Maximized
    End If
    Opens up my form!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking