locked
form in Addin RRS feed

  • Question

  • I created a new Addin

    public class Connect : IDTExtensibility2, IDTCommandTarget
        {

             public Connect()
            {
            }

            public DTE2 GetObject()
            {
                return _applicationObject;
            }

            ............

            private DTE2 _applicationObject;

       }

    Now I create new form like this : http://preview.filesonic.com/img/853161.png

    public partial class Form1 : Form
        {
            Connect C;
            public Form1()
            {
           
                InitializeComponent();
                C = new Connect();
                _app = C.GetObject();
            }

            private void btFind_Click(object sender, EventArgs e)
            {
                OutputWindow outWin = _app.ToolWindows.OutputWindow;
                OutputWindowPane outPane = outWin.OutputWindowPanes.Add("Keyboard Shortcuts");
                outPane.Clear();
                outPane.Activate();

                foreach (Command cmd in _app.Commands)
                {
                    object[] cmdBindings = cmd.Bindings as object[];
                    if (cmdBindings.Length > 0)
                    {
                        string bindingStr = string.Join(", ", cmdBindings);
                        string s1 = cmd.Name.ToLower();
                        string s2 = tb1.Text.ToLower();
                        if (s1.IndexOf(s2) >= 0)
                            outPane.OutputString("Command: " + cmd.Name + ", Shortcut: " + bindingStr + "\n");
                        else
                            MessageBox.Show("This command does not exist !");
                    }
                }
                tb1.Text = "";

            }

            ............

             private DTE2 _app;

        }

     

    I want to output All command of VS when click button find , but nothing's happen , statement : _app = C.GetObject(); doesn't work , means _app is null . Plz help ! I want to access _applicationObject of class Connect (private type) . Is anything wrong in my code , plz help

    Wednesday, April 13, 2011 5:08 PM

Answers

  • It isn't clear to me from your above code what you are trying to do exactly.

    1:  Normally you don't create the Connect class, Visual Studio does, in doing so it ends up providing the 'application object'.  If you simply new up your class that doesn't happen, there is no magic here :)

    2:  I assume your form is created from your AddIn in response to a command?  If so why not pass along anything the form needs into its constructor, in this case it seems you need DTE, so why not provide that to your form instead of it trying to new up an instance of the Connect object?

    3:  Alternatively you can use ServiceProvider.GlobalProvider to get an instance of DTE.

    Ryan

    Wednesday, April 13, 2011 5:49 PM
  • found it ! I do not install VS SDK , my mistake !
    • Marked as answer by Victor_Chen Thursday, April 21, 2011 7:53 AM
    Thursday, April 14, 2011 2:04 AM

All replies

  • It isn't clear to me from your above code what you are trying to do exactly.

    1:  Normally you don't create the Connect class, Visual Studio does, in doing so it ends up providing the 'application object'.  If you simply new up your class that doesn't happen, there is no magic here :)

    2:  I assume your form is created from your AddIn in response to a command?  If so why not pass along anything the form needs into its constructor, in this case it seems you need DTE, so why not provide that to your form instead of it trying to new up an instance of the Connect object?

    3:  Alternatively you can use ServiceProvider.GlobalProvider to get an instance of DTE.

    Ryan

    Wednesday, April 13, 2011 5:49 PM
  • Can you give me exactly code to do with ServiceProvider.GlobalProvider , how to create an instance of DTE with it . (by the way I created a VS Addin , not a VS package . So can not find Microsoft.VisualStudio.Shell) . My purpose is to find all command with name that the text of textbox is substring of it .
    Wednesday, April 13, 2011 11:38 PM
  • Whether you are an AddIn or a Package you can reference MPF (Microsoft.VisualStudio.Shell).  To get DTE via ServiceProvider you simply say

    DTE dte = (DTE)ServiceProvider.GlobalProvider.GetService(typeof(SDTE));

    Though as I said, since your form is created by someone (you?) why does that person not simply provide the necessary services on construction? That is generally a better practice than 'pulling things out of thin air' as it makes your dependencies very clear, using things like ServiceProvider.GlobalProvider hides these dependencies inside your method bodies.

    Ryan

     

    Wednesday, April 13, 2011 11:50 PM
  • can not find reference Microsoft.VisualStudio.Shell.10.0 or Microsoft.VisualStudio.MPF   . Help ! (using framework 4.0 )
    Thursday, April 14, 2011 1:15 AM
  • found it ! I do not install VS SDK , my mistake !
    • Marked as answer by Victor_Chen Thursday, April 21, 2011 7:53 AM
    Thursday, April 14, 2011 2:04 AM
  • Do you have the SDK installed? Though as I said you don't need to do that, your connect class is instantiated by VS, why doesn't it pass the instance of DTE that VS gives to it along to your form?

    Ryan

    Thursday, April 14, 2011 2:07 AM