locked
FORM OPEN NAME RRS feed

  • Question

  • HI

    Use this code to compile all open forms
    I want the name of the form before the last name

    Dim myListA As New List(Of String) For Each frm As Form In Application.OpenForms myListA.Add(frm.Name) Next


    Saturday, August 25, 2018 9:12 AM

Answers

  • ... I want the name of the form before the last name

     Do you mean that you only want the name of the second to the last opened form?  For example,  if you had 3 forms opened,  Form1,  Form2,  and Form3,  you only want the name of Form2 ?

     This will do that but,  you should add some error checking just in case there is only 1 form opened.  It would throw an index out of range exception if there was only 1 form opened,  your main form.
            Dim OpenFormCount As Integer = Application.OpenForms.Count
            Dim FormName As String = Application.OpenForms(OpenFormCount - 2).Name
    


    If you say it can`t be done then i`ll try it

    • Marked as answer by ahmeddc Saturday, August 25, 2018 6:19 PM
    Saturday, August 25, 2018 12:29 PM

All replies

  • Add the following code module to your project.

    Working example found here.

    Namespace My
        Partial Friend Class MyApplication
            ''' <summary>
            ''' Determines if the a form is open
            ''' </summary>
            ''' <param name="sender">Name of form</param>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Function IsFormOpen(sender As String) As Boolean
                Return ((From f In Application.OpenForms.Cast(Of Form)() Where f.Name.Equals(sender) Select f.Name).ToList.Count > 0)
            End Function
        End Class
    End Namespace
    
    

    Usage

    If My.Application.IsFormOpen("Form2") Then
        MessageBox.Show("Yes")
    Else
        MessageBox.Show("No")
    End If


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    • Edited by KareninstructorMVP Saturday, August 25, 2018 10:29 AM Added link to working sample
    Saturday, August 25, 2018 10:27 AM
  • ... I want the name of the form before the last name

     Do you mean that you only want the name of the second to the last opened form?  For example,  if you had 3 forms opened,  Form1,  Form2,  and Form3,  you only want the name of Form2 ?

     This will do that but,  you should add some error checking just in case there is only 1 form opened.  It would throw an index out of range exception if there was only 1 form opened,  your main form.
            Dim OpenFormCount As Integer = Application.OpenForms.Count
            Dim FormName As String = Application.OpenForms(OpenFormCount - 2).Name
    


    If you say it can`t be done then i`ll try it

    • Marked as answer by ahmeddc Saturday, August 25, 2018 6:19 PM
    Saturday, August 25, 2018 12:29 PM