Allora ... Spero di riuscire ad essere chiaro.
In sostanza ho una form0 principale che non è MDI parent, sulla quale ho tre tasti: uno per aprire Form1, uno per aprire Form2 e uno per aprire form3.
All'apertura form1, form2, form3 sono chiuse.
Vorrei che cliccando su uno dei tre tasti succedesse questo: che si aprisse la form relativa al tasto e che nascondesse eventuali form aperte (nel caso in cui il tasto fosse quello relativo a Form1, le eventuali form aperte che deve nascondere sono la form1 o la form2).
Attualmente per intercettare le form aperte utilizzo una classe chiamata FormsCollection il cui codice lo trovi sotto:
Imports System.Windows.Forms
Public Class FormsCollection : Implements IEnumerator, IEnumerable
Private position As Integer = -1
Private FormListRef As New ArrayList
Private FormListName As New ArrayList
Public Sub New()
End Sub
Public Property Index() As String
Get
Return position
End Get
Set(ByVal Value As String)
position = Value
End Set
End Property
Public Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
Return CType(Me, IEnumerator)
End Function
Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
position += 1
Return (position < FormListRef.Count)
End Function
Public Function MovePrevious() As Boolean
position -= 1
Return (position > -1)
End Function
Public Sub Reset() Implements IEnumerator.Reset
position = -1
End Sub
Public ReadOnly Property Count() As Integer
Get
Return FormListRef.Count
End Get
End Property
Public ReadOnly Property Current() As Object Implements IEnumerator.Current
Get
Return FormListRef(position)
End Get
End Property
Default Public ReadOnly Property Item(ByVal index As Int32) As Form
Get
Return FormListRef(index)
End Get
End Property
Public Sub Add(ByVal frm As Form)
FormListRef.Add(frm)
FormListName.Add(frm.Name)
End Sub
Public Overloads Sub RemoveAt(ByVal Index As Long)
FormListRef.RemoveAt(Index)
FormListName.RemoveAt(Index)
End Sub
Public Overloads Sub RemoveAt(ByVal FormName As String)
Dim intPos As Integer = FormListName.LastIndexOf(FormName)
FormListRef.RemoveAt(intPos)
FormListName.RemoveAt(intPos)
End Sub
Public Function IsOpened(ByVal FormName As String) As Boolean
position = FormListName.LastIndexOf(FormName)
If position >= 0 Then Return True
End Function
End Class
Nella form0 ho inserito il seguente codice per verificare che la myform non sia già aperta. Se non è aperta, la apre.
Dim Forms As New FormsCollection
Dim myForm As String
Private Sub cmdLocalita_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLocalita.Click
myForm = NomeForm
If Forms.IsOpened(myForm) Then
Dim frm As Form = Forms(myForm)
Else
Dim frm As New Parrocchie
Forms.Add(frm)
frm.Show()
End If
Per nascondere l'eventuale form aperta uso il seguente codice:
Forms.RemoveAt(frm.Name)
frm.Hide()
Vorrei scrivere un codice, il più razionale possibilie, che mi permetta di fare l'operazione descritta all'inizio del messaggio.
Spero di essere riuscito a farmi capire. Grazie.
Daniele