locked
VSPackage: What is the best way to show dialogs/message boxes? RRS feed

  • General discussion

  • Hi.

    In the VS package I am working on I need to sometimes display dialogs/message boxes on environment events. Those events include file addition, deletion, rename and others. What is the best way to show those dialogs/message boxes?

    Currently, for dialogs I use the ShowDialog method of the form I want to show as dialog. It works fine but I have some doubts. I am opening a temporary, invisible Visual Studio instance to do some stuff there without getting in the way of the user. I set some variables so that the temp instance knows not to display those dialogs. But what if someone else decides to do the same(use an invisible instance), and the user has both his and my package installed? Well, my dialogs would popup out of nowhere sometimes, which would be strange. These dialogs I show expect some input and return a result (Yes, No, Cancel, other). My package has a way of defining the default actions for those dialogs but I would not know to not display them and just do the default.

    As for message boxes I was using the MessageBox.Show method. It worked fine but I noticed in some examples that they use the IVsUIShell.ShowMessageBox method. I tried to use it and it works but displays my title as part of the message and uses "Microsoft Visual Studio" as title. What is the purpose of using IVsUIShell.ShowMessageBox? Do I need to use it or I can use the MessagBox.Show method?


    Thanks.
    Thursday, February 25, 2010 5:17 PM

All replies

  • Hi,

    Thanks for your post on MSDN forum.
    I'm a little puzzle about why someone's package(use an invisible instance) will affect your variables for the showing of dialogs, are these variables depend on some common condition?

    IVsUIShell.ShowMessageBox shows the build-in message box, so the title is always "Microsoft Visual Studio", it supports modal message box and help system, and I think it's using the basic message box of system essentially, using MessageBox.Show() is also okay.

    Sincerely,
    Wesley Yao [MSFT]
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact: msdnmg @ microsoft.com


    Please mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, February 26, 2010 5:32 AM
  • Hi.

    Thanks for the reply.

    As for message boxes - if MessageBox.Show is OK I will use it unless I need to integrate help.

    As for dialogs:


    I'm a little puzzle about why someone's package(use an invisible instance) will affect your variables for the showing of dialogs, are these variables depend on some common condition?

    I will try to explain. In my code for some reasons I need to open an invisible instance of Visual Studio. Although it is invisible, it is actually a pretty normal Visual Studio instance - it loads all installed packages and everything. Thus, it loads my package as well. But I know that and I don't want my package to listen for some events(and show dialogs) in the invisible instance - so I do some things to prevent it in the code that instantiated the instance. I set a global variable (in DTE.Globals) so that when my package initializes, it will know that it is in "temp" mode and will not handle events, thus no dialogs will be displayed. In code this is something like:

    EnvDTE80.DTE2 dte8Obj = ...//get new DTE object here - launch new instance
    				
    Globals g = dte8Obj.Globals;
    g["SOME_NAME"] = currentProcessId;
    				
    dte8Obj.SuppressUI = true;	
    dte8Obj.MainWindow.Visible = false;
    As you can see I set a variable to the process ID of the instance that launches the invisible instance. In the initialization of my package I check for this variable. If it exists I do not subscribe for events and I subscribe for the Exit event of the process with the given ID so that I can exit the invisible instance if the process that started it ends unexpectedly. This works fine for now.

    But when someone else decides to use such "temp" "invisible" instance he will not set this global variable so that my package will not know that it is in "temp" mode and it will display some dialogs and wait for response. So, basically, my package works but it relies on no one else using the same approach, which is not good. So I was just sharing this and I wondered if I could do something about it.


    Thanks.
    Friday, February 26, 2010 11:47 AM
  • Hi,

    Thanks for your feedback.
    Now I understand what you are doing.
    I think checking the DTE.MainWindow.Visible in your package might be a solution:

    EnvDTE80.DTE2 dte8Obj = (EnvDTE80.DTE2)Package.GetGlobalService(typeof(SDTE));
    if (dte8Obj.MainWindow.Visible)
    {...}

    Sincerely,
    Wesley Yao [MSFT]
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact: msdnmg @ microsoft.com
    Please mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, March 1, 2010 8:18 AM
  • Hi.

    Thanks for the reply.

    Yes, this might be a solution. I will use this property and do the default event action whenever the window is hidden.
    Monday, March 1, 2010 8:39 AM
  • To follow up on the dialog question. You can display your own modal dialogs if you desire. Just be sure to call IVsUIShell.EnableModeless, to inform the shell when you're entering/exiting a modal state.

    Sincerely,
    Ed Dore
    Monday, March 1, 2010 5:44 PM
  • Hi, Ed.

    Thanks for the reply.

    Do you mean that I need to invoke the IVsUIShell.EnableModeless method before and after I invoke myForm.ShowDialog()? Or just before and after message boxes?

    I read about the method and from what I understand I must use it always. Maybe I should invoke it, check the result and if it is not S_OK, then I don't show the dialog. Is that correct?

    Thanks.
    Tuesday, March 2, 2010 11:20 PM