[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 770
  • Last Modified:

C#: Alternative to Modal window

I'm developing a .NET windows app that has "lookup" windows to allow users to select values for certain fields on the main form.  Normally, these lookup windows would be modal.

However, due to a bug in an integration framework that I'm having to use, I am unable to use any modal windows.  (long story, it's a pain)

Are there workarounds that would allow me to manually handle situations that modal windows normally take care of?

The two issues that come to mind are:

1) When a lookup window is open and the user clicks on another window, I need to either set the focus back on the lookup window, or close the lookup window and return focus to the main form.  I can close the lookup window on the Deactivate event, but is there a way to set the focus back to the main form of the app?  (or can I setup a parent / child relationship between the main form and lookup windows?)

2) How do I prevent the user from opening more than one instance of a lookup window?  Currently, if the lookup is non-modal, the user can open multiple lookup windows.


Are there any other issues that I need to consider?
0
Steve Endow
Asked:
Steve Endow
  • 3
1 Solution
 
Steve EndowMicrosoft MVP - Dynamics GPAuthor Commented:
After trying different things, so far I have the following:

1) When the main form opens the lookup window, I set the main form as the parent of the lookup.

2) Before the main form opens a lookup window, I call the LookupOpen routine to check to see if there is already a lookup window open:

3) If there is an existing lookup already open (LookupOpen == true), I call LookupFocus, which sets the focus back to the existing lookup window.


Anything else, or any better approaches?

// #1

lookup.Show(this);


// #2
private bool LookupOpen()
{
    bool lookupOpen = false;

    for (int i = 0; i <= Application.OpenForms.Count - 1; i++)
    {
        if (Application.OpenForms[i].Name == "LookupGP")
        {
            lookupOpen = true;
        }
        else
        {
            lookupOpen = false;
        }
    }
    return lookupOpen;
}


// #3
private void LookupFocus()
{
    for (int i = 0; i <= Application.OpenForms.Count - 1; i++)
    {
        if (Application.OpenForms[i].Name == "LookupGP")
        {
            Application.OpenForms[i].Focus();
        }
    }
}

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
A simple approach is to DISABLE the main form so that the user can't interact with it:

    this.Enable = false;
    // ...display your "modal" form...

You can subscribe to the FormClosed() event of the "modal" form so you know when to re-enable the form.

Since the user can't interact with the disabled from they also can't open up multiple instances of your "modal" form.
0
 
Steve EndowMicrosoft MVP - Dynamics GPAuthor Commented:
Wow, that is brilliant.  Amazingly elegant.  Two lines of code.

Nice job, thanks!
0
 
Steve EndowMicrosoft MVP - Dynamics GPAuthor Commented:
Impressive, thanks!
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now