Link to home
Start Free TrialLog in
Avatar of bman9111
bman9111

asked on

c# addin for vs 2005

I have created an addin that has a form in it. I would like to have my addin be able to read what ever project code I am in...Is there a way to do that... For example....when I'm in a new project and run my addin it would bring a messagebox that show the new project Program.cs application.run form...


Example..

on any new projects the program.cs looks like this.

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}


I would like it so that when I hit my addin it would bring a messagebox that would say: Form1

or lets say I changed my program.cs to this...

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmtest());
        }
    }
}

the messagebox would say frmtest.


basically want my addin to tell me what application.run form is first..

is that possible????

Avatar of aaronfeng
aaronfeng

Yes, this is possible.  You can pretty much get a hold of anything in the DTE object model.  I also made VS Addin in the past, but I have switched to Visual Studio Integration Package because it is much easier to work with.  The down side of using the VS Integration Package is you need to apply for a key called PLK in order for you to distribute to others.  The PLK is free, but it's just a pain to apply for it (takes a few days to get it) .  If you are the only using it, you don't need the PLK, it will work on the machine you developed on.  In order to work with VS Integration Package you need to install Visual Studio SDK which can be found here: http://msdn.microsoft.com/vstudio/extend/

What you need to do is to parse Program.cs to get the startup form out.

Cheers,

Aaron
http://aaronfeng.blogspot.com
Avatar of bman9111

ASKER

could u give me an example of the steps i need to do what I listed above????

Sure, I will assume you know something about VS Addin already since you are trying to make one, so I'll get to the meat of it.  Basically you want to get the path of Program.cs from the opened solution in the DTE object model.  After you get it, you will open the file on disk and read it then popup the messagebox with the correct info.

// Here is how you get the path of Program.cs
string file = DTE.Solution.FindProjectItem("Program.cs").get_FileNames(0);

// Open and read the file using StreamReader
StreamReader reader = new StreamReader(file);

// Basicall you are looking for Application.Run(...);
// Maybe use RegEx to help out, but you probably just get it out by manipulate the string

// Show the user correct form in the message box
MessageBox.Show(formName);

If you need more help on creating VS Addin in generate, read this tutorial and source code, it will help a lot.
http://www.codeproject.com/useritems/LineCounterAddin.asp

Cheers,

Aaron
getting an error here...

  string file = DTE.Solution.FindProjectItem("Program.cs").get_FileNames(0);

Error      2      An object reference is required for the nonstatic field, method, or property 'EnvDTE._DTE.Solution.get'


what reference am i missing????
Basically you want to be able to get a DTE object, the code didn't work because DTE is a property that returns the object, so try (I'll assume you are doing a VS Addin not VS Integration):

// In the OnConnection method:

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom) {
  DTE2 dte = (DTE2)application;
  string file = dte.Solution.FindProjectItem("Program.cs").get_FileNames(0);
}

That should work, give that a try.

Cheers,

Aaron
not sure where to put the above code....this is what i have....

i have the used the addin wizard in vs2005....then

then on the connect.cs....i have not touch anything except for add this to that class

      public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
            {
                  handled = false;
                  if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
                  {
                        if(commandName == "ListingComponents.Connect.ListingComponents")
                        {
                               Form1 frm = new Form1();
                     frm.Show();
                    handled = true;
                              return;
                   

                }
                  }
           
            }



then of course i add a form1 to the project...I have a command button on it and need to have a piece of code read the program.cs of a new project to see what the startup form is... like stated above....

seems like ur suggestion is for the connect.cs....really need something on the form level...

ASKER CERTIFIED SOLUTION
Avatar of aaronfeng
aaronfeng

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial