Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to Give the Focus to a Specified Form
Message
General information
Forum:
ASP.NET
Category:
Forms
Miscellaneous
Thread ID:
00831190
Message ID:
00831281
Views:
24
Hi Arnaldo,

This really is a design question. I would recommend using a Forms Manager who is responsible for making sure only one form A is instantiated. The manager would be the class that would be responsible for keeping track of all forms opened and allow for different ones to be activated. The class would allow contain an Add and Remove methods to allow adding a form to the manager and removing it. You can also provide other methods for determining which is the active form and other functionality.

>I've found a way to achieve the focus issue, but I find it weird
>
>My opinion is that it shouldn't be necessary to have the forms declared in a global scope, but that's the only way it worked
>
>I'd like a professional opinion from someone else
>
>thanks in advance!
>
>Here is the code:
>
>
>Public Class frmMain
>    Inherits System.Windows.Forms.Form
>
>    Private Sub MenuItem1_Click(ByVal sender As Object _
>    , ByVal e As System.EventArgs) Handles mnArrays.Click
>        With Module1.frmA
>            If Not (.MdiParent Is Me) Then
>                .MdiParent = Me
>            End If
>            .Show()
>            .Activate()
>        End With
>    End Sub
>
>    Private Sub MenuItem2_Click(ByVal sender As Object _
>    , ByVal e As System.EventArgs) Handles mnRel.Click
>        With Module1.frmB
>            If Not (.MdiParent Is Me) Then
>                .MdiParent = Me
>            End If
>            .Show()
>            .Activate()
>        End With
>    End Sub
>
>End Class
>
>Module Module1
>
>    Public frmA As New frmA()
>    Public frmB As New frmB()
>
>End Module
>
>Public Class frmB
>    Inherits System.Windows.Forms.Form
>
>    Private Sub btnA_Click(ByVal sender As System.Object _
>    , ByVal e As System.EventArgs) Handles btnSetFocus.Click
>        With Module1.frmA
>            .Show()
>            .Activate()
>        End With
>    End Sub
>End Class
>
>
>
>the situation is:
>
>I have two forms, and I have to avoid the multiple instances loading from the main menu and I need to be able to set the focus to frmA from a button in frmB.
>
>The only way I've found to solve this is by declaring the instance of the forms in a module and showing and activating those instances, but what if I had 30 forms, do I have to declare 30 distinct variables?, is there no way to access a loaded instance of a form without the public formtype variable?
>
>Any help, opinion, suggestion is highly appreciated
-----------------------------------------

Cathi Gero, CPA
Prenia Software & Consulting Services
Microsoft C# / .NET MVP
Mere Mortals for .NET MVP
cgero@prenia.com
www.prenia.com
Weblog: blogs.prenia.com/cathi
Previous
Reply
Map
View

Click here to load this message in the networking platform