• 쇼핑몰
  • 커뮤니티
  • 북마크

vb.net


[Controls] [vb.net] 열려있는 폼을 확인하여 없으면 폼실행(OpenForms Check)

하나를하더라도최선을
2019.11.03 14:16 2,735 0

본문



폼이 열려 있는지 확인(Check if form is Opened)


Imports System.Linq ' need to add 


If Application.OpenForms().OfType(Of Form2).Any Then
  MessageBox.Show("Opened")
Else
  Dim f2 As New Form2
  f2.Text = "form2"
  f2.Show()
End If



If myForm.IsHandleCreated then
   myForm is open
End If


우수답변 : 

Private Sub OpenWindowsForm(ByVal FormName As String)
    Dim instForm As Form = Application.OpenForms.OfType(Of Form)().Where(Function(frm) frm.Name = FormName).SingleOrDefault()
    If instForm Is Nothing Then
        Dim frm As New Form
        frm = DirectCast(CreateObjectInstance(FormName), Form)
        frm.MdiParent = Me
        Me.Panel1.Controls.Add(frm)
        Me.Panel1.Tag = frm
        frm.Show()
    Else
        instForm.Select()
        instForm.WindowState = FormWindowState.Maximized
        instForm.BringToFront()
    End If
End Sub

Public Function CreateObjectInstance(ByVal objectName As String) As Object
    Dim obj As Object
    Try
        If objectName.LastIndexOf(".") = -1 Then
            objectName = [Assembly].GetEntryAssembly.GetName.Name & "." & objectName
        End If

        obj = [Assembly].GetEntryAssembly.CreateInstance(objectName)

    Catch ex As Exception
        obj = Nothing
    End Try
    Return obj
End Function
Private Sub btnBusqueda_Click(sender As Object, e As EventArgs) Handles btnBusqueda.Click
    OpenWindowsForm("Busqueda")
End Sub

Private Sub btnCalendario_Click_1(sender As Object, e As EventArgs) Handles btnCalendario.Click
    OpenWindowsForm("Calendario")
End Sub





Dim frmCollection = System.Windows.Forms.Application.OpenForms
If frmCollection.OfType(Of Form2).Any Then
    frmCollection.Item("Form2").Activate()
Else
    Dim newForm2 = New Form2
    newForm2.Show()
End If



    Dim xChildWindows = Application.OpenForms.OfType(Of frmForm2)
    If xChildWindows.Any Then
        xChildWindows.First().Focus() 'Focus if exists
    Else
        Dim xfrmNew As New frmForm2() 'Open window if doeasn't exists
        xfrmNew.MdiParent = Me
        xfrmNew.Show()
    End If



Dim formText As String
Dim prevText As String

 Private Sub OpenForm(ByVal frm As Windows.Forms.Form)
        formText = frm.Text
        If formText = prevText Then Exit Sub
        CloseForms()
        ' Make it a child of this MDI form before showing it.
        frm.MdiParent = Me
        frm.Show()
        frm.Location = New Point(0, 0)
        prevText = formText
    End Sub

    Private Sub CloseForms()
        For Each ChildForm As Form In Me.MdiChildren
            ChildForm.Close()
        Next
    End Sub

    Private Sub NewToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PayablesToolStripMenuItem.Click
            OpenForm(frmPayables)
    End Sub




댓글목록 0

등록된 댓글이 없습니다.