I have created a Visual Studio Add-In that adds a form to an existing Project in the opened solution.
This is how I create the form:
string templatePath = sol.GetProjectItemTemplate("Form.zip", "csproj");
//sol is an EnvDTE80.Solution2
proj.ProjectItems.AddFromTemplate(templatePath, formName);
//proj is an EnvDTE.Project
After that I can successfully get the reference to the ProjectItem of the added form, then I can get a reference to the System.Windows.Forms.Form, and through this reference I can add a button to the form like this:
Button btn = new Button();
btn.Text = "my funky button";
btn.Name = "newButton";
btn.Size = new Size(150, 23);
btn.Location = new Point(30, 30);
frmNewForm.Controls.Add(btn);
//frmNewForm is a System.Windows.Forms.Form
And then the button is successfully added to the form:
However, when I try to save this form, it just won’t save. I click Visual Studio’s [save] button, it just doesn’t turn gray. Even if I click [Save All], the form won’t be saved. Then I close Visual Studio, reopen it, and open the project to which I have added the new form with my Add-In, and the new button simply isn’t there. Just an empty form.
I’ve even tried saving the project and the solution programmatically through the following code:
itemForm.Save(itemForm.Name);
//itemFrom is an EnvDTE.ProjectItem
proj.Save(proj.FullName);
//proj is an EnvDTE.Project
I’ve thought that this would be because the instance of the Visual Studio that I was using to test my Add-In is a debug one, opened right after I run my Add-In. But I’ve tried using the installed Add-In (which remained automatically after running it), and the problem persisted.
Update
I’ve just noticed two things:
1) the button only appears on the design of the form, and nowhere else. And it doesn’t even let me select it to see it’s attributes.
It’s name doesn’t appear in Intellisense, in the object list, or even on the design document of the form.
As a test, I’ve added a button manually, and this one I can select, and interact with it:
What I could get from that is that I am not adding the button properly.
Then the new question regarding the button would be: How can I add a new button to a form created through EnvDTE in a way that I can interact with it in design time ?
2) While trying to see the differences from my funky button and my Manually added button, I’ve attempted something I hadn’t before with a programmatically created form: instantiate and run it.
And here's how I've done it:
MyFunkyForm frm = new MyFunkyForm ();
frm.Show();
It flashes on the screen (apparently with none of my two buttons), and the execution ends. Even if I try to do something on it’s Form_load, it’s executed, then the form flashes, and the execution is ended (the form is closed and debugging is over), as if I had called the Close() method.
Then the additional question would be: Did I skip a step while adding the form, or am I not creating it properly at all ?
you are adding a form to a c# project, I think you should not instantiate the form alone by itself like you did but if you want to see it you should execute the application.
I don't know how to do this, never tried myself, found this, hopefully it's helpful:
http://www.mztools.com/articles/2006/mz2006016.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With