none
.NET: How do I Keep Multiple Forms Active Simultaneously in a Standard Application? RRS feed

  • 質問

  • Hi All,

    How do I keep multiple forms active simultaneously in my C# application?

    This is a non-MDI application.

    The reason is simple. I have two forms that sit next to each other on the display, I want the user to be able to click between them as they like.

    The problem with the current behaviour is that when the user goes to click on the non-active form, their first click activates the form, then a second click is required to fire the control. To make it difficult for me I find in my testing that some controls will fire on the first click, while others will not; nice.

    I've looked at a number of hacks - like hooking up a mouseEnter event to activate the form when the user goes over to it, but this is not ideal for a number of reasons.

    Any suggestions?

    Thanks,

    Ash
    2009年3月5日 19:11

回答

  • You are probably fighting a problem with the ToolStripItem classes.  They are windowless controls, note that they don't derive from the Control class.  The WF crew had to re-implement the behavior of a window from scratch and they got a it subtly wrong in a few places.  The "click to activate doesn't click" behavior is one of them.  Understandably btw, they would have to completely re-invent the window manager as well.

    I don't know of a clean workaround for this.  Having two forms active at the same time is a no-go, Windows just doesn't support it.  I can only recommend you reconsider your user interface.

    Hans Passant.
    • 回答の候補に設定 Kira Qian 2009年3月9日 6:31
    • 回答としてマーク LazyCodeMonkey 2009年3月10日 12:39
    2009年3月6日 2:20
    モデレータ

すべての返信

  • The MouseEnter may be the best way to do it, you could set it up to check if the other form is active and if so, switch over (to prevent activation when other applications are the active form).

    I've never seen any applications which do what you want and it seems like a core windows thing to only have one active at a time, so if it's possible I see it being very complicated and probably fraught with hidden errors.
    2009年3月5日 21:03
  • What are you really trying to do? 

    Think about this for a second.  There can only be one active application or window at a time, that being the one that currently has the focus.  (Hey, there's only one cursur!)  So what are you really trying to do?

    While forms may appear to go idle when another has the focus, that is only appearances.  Forms---or any class for that matter, forms merely provide visual proof---can still react to events.  Test it for your self.  Start a form that has timer, that makes something on the form change back and forth once per second.  Now click a button on it that launches a new form.ShowDialog().  The initial form is still having fun isn't it?

    Reconsider your User Interface if you need.  There's only one cursor, so there will only be one form with the focus.  But, lack of focus does not mean lack of activity on the form!  The other forms are only as dead as your code allows them to be.

    Rudedog  =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    • 回答の候補に設定 Kira Qian 2009年3月9日 6:31
    • 回答の候補の設定解除 LazyCodeMonkey 2009年3月10日 12:39
    2009年3月5日 21:23
  • You are probably fighting a problem with the ToolStripItem classes.  They are windowless controls, note that they don't derive from the Control class.  The WF crew had to re-implement the behavior of a window from scratch and they got a it subtly wrong in a few places.  The "click to activate doesn't click" behavior is one of them.  Understandably btw, they would have to completely re-invent the window manager as well.

    I don't know of a clean workaround for this.  Having two forms active at the same time is a no-go, Windows just doesn't support it.  I can only recommend you reconsider your user interface.

    Hans Passant.
    • 回答の候補に設定 Kira Qian 2009年3月9日 6:31
    • 回答としてマーク LazyCodeMonkey 2009年3月10日 12:39
    2009年3月6日 2:20
    モデレータ
  • I appreciate the comments along the lines of "your UI is broken" but they are not very constructive. Remember that "focus" and "active" are two different things, so no, I'm not proposing to have two windows (or more correctly, controls) maintain focus at once.

    Some applications are quite complex and require the user to juggle different forms. The MDI application is the classic way to provide the user with a consistent UI experience, but most would agree that it works best for programs where the forms contain similar content, eg MS Word. My needs were not quite addressed with the MDI framework, so it was developed as a standard forms application.

    To reassure everyone that I am not being stupid, here are the needs of the application; dual monitors, left hand screen contains "main" program while right hand screen contains a real time video display with overlays. The video handling is running in a separate thread. I could "fake" this display and put it all in one form, but then Windows does not give me the ability to maximise to both screens. Oh wait, yes it does, but if I enable that (by setting Windows to "span" mode) then all forms in windows will maximise to the entire desktop, which is definitely not wanted. To liven things up a bit, in some deployments there will be no video window. While others will not have two monitors. Others still may have three...

    So my initial query still stands, I want: two forms, side by side, which the user can click between and have the buttons fire events on the first click. The final post above addresses the issue perfectly, I am indeed using toolstrips which do not respond on first clicks. Normal buttons do, adding to the confusion.

    My two options are to: fake it with MouseEnter, which should actually work OK for me. Or to encapsulate my forms in some way and then provide my own controls for arranging them nicely on the desktop, similar to NVidea's video driver form handling plugin that I've used in the past. I'll see how I go, but in either case it won't be as elegant as I had hoped.

    Thank you for everyone's input, the feedback is most appreciated.

    Ash

    2009年3月10日 12:52
  • Yes, two active forms is possible, however can formA invoke formB or/and can formA not be the main form started by Application.Run(Form1), I don't know. I am trying to implement the two environments myself. So far unsuccessfully.

    But, yes you can have two actives forms running with user switching back and forth between them. Check the following link.

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.applicationcontext?view=netframework-4.8

    Ayo Soul

    2019年7月12日 23:15