Extend VS2010 Form Designer Context Menu - c#

I'm trying to add an item to the context menu in the Visual Studio 2010 Form Designer.
So far I have an Addin project which retrieves the IDesignerHost, IDesigner etc. representations of the Form Designer. None of these interfaces seem to expose anything related to context menus. I've also tried retrieving the IMenuEditorService for the designer Site, but this doesn't have any items associated with it.
I've also tried iterating through all the CommandBar items in the window DTE (as seen Visual Studio 2010 Plug-in - Adding a context-menu to the Editor Window), again, none of these seem to represent the Form Designer context menu.
Is there an easy way to do this?
Thanks

Verbs appear in designer menu and control properties. If adding verb to designer would be sufficient to you, then that is how you can make it:
DesignerVerb _verb;
_verb = new DesignerVerb("Do something", OnConvertClick);
var designer = _designerHost.GetDesigner(comp);
if (!designer.Verbs.Contains(_verb))
designer.Verbs.Add(_verb);
void OnConvertClick(object sender, EventArgs e)
{
MessageBox.Show("Hello world!");
}

Related

How Do You Edit a Second (Non-Main) C# Form Window In Visual Studio Designer?

I am writing a C# Windows Forms program in Visual Studio. I have a button that creates and shows (opens) a new Form (window) called VideoWindow. I can edit the MainWindow in the Design workspace in Visual Studio which allows me to visually edit its contents. However, I can't find a way to do the same thing with the VideoWindow. I have tried right clicking on VideoWindow and clicking View Designer, but it just takes me to the MainWindow designer. How do I open the designer for the second VideoWindow? Is this possible? Below is the code that creates and opens the new form:
private void ButtonWindow(object sender, EventArgs e)
{
Form VideoWindow = new Form();
VideoWindow.Size = new Size(500, 300);
VideoWindow.Show();
}
Edit: I know you can (and usually should) access the Designer when you create a form through the Visual Studio wizard via Project -> Add Form. However my question was for if you manually write a form class like NewForm.cs. In that case there would be no auto-generated NewForm.Designer.cs file.
You can customize a new form, and then create the corresponding object after modification. Here are the relevant steps:
1.Create a new form videoform
2.Relevant code:
.Show(); and .ShowDialog();
Note the difference between the two.
private void button1_Click(object sender, EventArgs e)
{
VideoWindow videoWindow = new VideoWindow();
videoWindow.Show();
//videoWindow.ShowDialog();
}
3.Ouput:
After some testing I found that it is possible to access the Visual Studio GUI-based Designer for a form class that was created manually (i.e. not created through the usual Project -> Add Form wizard).
If you create a CustomForm.cs file in your project and, importantly, have that class inherit from Form (System.Windows.Forms), then hit Shift F7, Visual Studio will make a GUI Designer for that form.
It will also automatically create the InitializeComonent method in CustomForm.cs and add the SuspendLayout and ResumeLayout calls and Client size, and Name properties in that method.
In this arrangement, the designer elements will not be separated from the class definitions in a separate form.Designer.cs file like usual. Any change through the Designer GUI will directly effect CustomForm.cs. Of course this is less than ideal because there's a higher likelihood of a developer breaking the form since the auto-generated code is mixed in with the manually written code.

Create parent menu in AddIn using c#

I want to add menu to visual studio in AddIn.
I found how to create submenu item in Expose an Add-In on the Tools Menu (Visual C#), but I want to create primary menu (like 'File', 'Edit', 'View' etc.)
How can I do that using c#?
I think You can use this:
Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar =
((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["MenuBar"];
CommandBarPopup myNewPopUpControl =
menuBarCommandBar.Controls.Add(MsoControlType.msoControlPopup) as CommandBarPopup;
myNewPopUpControl.Caption = "MyMenu";
myNewPopUpControl.Visible = true;
First line is from the OnConnection of the Add-In template.
As I experienced each element of Controls collection has a zero Id, so the arrangment might be problematic. Also you can create Buttons and PopUps with Automation (Exception will be thrown otherwise)
http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.commandbars.commandbarcontrols.add.aspx
http://msdn.microsoft.com/en-us/library/office/aa190793(v=office.10).aspx
Update:
Your question seems to be a duplicate
[a question] Adding a Menu to the Visual Studio Menu Bar within an Add-In [a]

What is the standard way to add controls to a TabPage at Design Time?

I am creating my first Windows Forms application, to be deployed on Windows Mobile and I am having some trouble designing a Tabbed Interface.
I had assumed that I could Create a TabControl, then Add some TabPages and then drag Controls on to each Tab Page in turn. This does not appear to be possible and most of the information I see on the web seems to suggest that the controls should be added dynamically at run-time.
Am I missing something obvious here or is this indeed correct?
If you do have to add the controls at runtime then how do people generally manage the design process. Do they create a Custom UserControl for each tab and then add that at runtime?
Design environment (C# Visual Studio 2005, .net 2.0)
Runtime environment (Windows Mobile 6.1)
Update 1
The actual steps taken within visual studio were as follows :-
Select New Project -> SmartDevice -> Windows Mobile 6 Professional -> Device Application
Added a TabControl to Form1. This automatically adds tabPage1 and tabPage2
Update 2
The solution to this is embarrassingly noobish. The TabControl puts the tabs at the bottom of the page, the first thing I was doing was resizing the tab control to a single line which was then hiding the TabPage control.
Currently i don't use Windows Mobile, but i think it works quite the same.
After adding a TabControl to your form you should take a look into the properties and search for TabPages. Here you can add and delete new TabPages to your Control and design it as you like in the designer.
To your question about using UserControls on each TabPage i would definitely say Yes. It makes easier to separate between each page and what will happen on each one.
Also at a last step i am going to move the needed code out of the Designer.cs into my own function (e.g. var tabControl = CreateTabControl() where all of my properties are set. Then i put all my UserControls into an
private IEnumerable<Type> GetAllTypes()
{
yield return typeof(MyFirstControl);
yield return typeof(MySecondControl);
}
and make an
private void CreateTabPages(TabControl tabControl, IEnumerable<Type> types)
{
foreach(var type in types)
{
var control = Activator.CreateInstance(type);
var tabPage = new TabPage();
tabPage.Controls.Add(control);
tabControl.TabPages.Add(tabPage);
}
}
this will then be called by
CreateTabPages(tabControl, GetAllTypes());
With this approach i can easily add another Tab Page with a single line of code and design it in its own scope.
I just opened vs2008 and created a tabcontrol, then I added controls inside using drag and drop in the designer and I didn't found any problem.
The way I use to do it is to create a usercontrol for each tab, But I add the usercontrol to the tab in the designer. (note that the usercontrol will not appear in the toolbox until you generate your solution).
I didn't know why your method are not working. Did you stop your application before try to add the controls?
Good Luck.

How to put an extended WinForms Control on ToolBox

I plan to add functionalities to TextBox with the following:
public class TextBoxExt : TextBox
{
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
}
}
The question is how can we use this TextBoxExt? Is there anyway to get this class onto the ToolBox so that we can just drag and drop it onto the form? If not, what is the best way to use the TextBoxExt?
Build you project with TextBoxExt, make sure it compiles ok.
With the form that you want TextBoxExt on, open the toolbox, right click and select "choose items"
Browse to you .exe or dll that you compiled in 1)
make sure that TextBoxExt has a tick next to it, press ok
TextBoxExt should appear in the toolbox, drag it onto your form
(There is another way of doing this, opening the designer file and renaming the instances of TextBox to TextBoxExt but manual editing of designer files can be considered hazardous by some)
I know this is super old question, but maybe still useful for someone else that has same problem like me - as it's still on the top Google :)
You might interest to use ToolboxItemAttribute (http://msdn.microsoft.com/en-us/library/system.componentmodel.toolboxitemattribute(v=vs.110).aspx).
I did this at my end to resolve the problem.
[ToolboxItem(true)]
public class PanelTitle : LabelControl {
// Whatever code to override LabelControl here...
}
Rebuild the solution and the extended control should be shown in the toolbox.
Any custom control in your project should show up in the Toolbox automatically. I have found that sometimes the controls won't show until you close a re-open Visual Studio. I assume the issue has something to do with caching of the contents of the Toolbox.
You need to add a constructor to your derived class.
public class TextBoxExt : TextBox
{
public TextBoxExt()
{
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
}
}
Your control should appear in the toolbox for your solution automatically. To have it appear for other projects, you have to do Choose Toolbox items, as others have said.
If you want to provide special design-time functionality, then you will also need to provide some additional designer related attributes and probably your own class derived from ControlDesigner.
I fell into this trap just a couple of hours ago.
I've got a .NET 2.0 Windows Application project with some custom UserControls; it worked fine.
So I decided to order my files in subfolders, to make my project a little bit cleaner.
After that, Visual Studio 2010 designer stopped loading my forms, and ToolBox won't show my controls anymore.
I freaked out, moving back source files in project root, resetting ToolBox, but nothing seemed to work.
After that, I remembered I used ReSharper "Remove Unused References", so I tried to put back unused reference, in particular System.Data: problem solved! :O
I can't say you why, but this worked for me.
Hope my experience can help someone else. :)
Bye,
Nando
I created an empty constructor for my custom implementation of UltraGridBagLayoutPanel. Although david.healed is right it isn't necessary, it is quite useful to put a breakpoint in to check that when the form initialises it is using your class to implement your custom control.
It would have been a lot easier to edit the designer file, but I tried it and changed both the field type for the control and also changed the assignment of the field to a new instance of my custom control.
private Infragistics.Win.Misc.UltraGridBagLayoutPanel ultraGridBagLayoutPanel1;
this.ultraGridBagLayoutPanel1 = new Infragistics.Win.Misc.UltraGridBagLayoutPanel();
to
private Athia.Reports.ultraGridBagLayoutPanel1 ultraGridBagLayoutPanel1;
this.ultraGridBagLayoutPanel1 = new Athia.Reports.ultraGridBagLayoutPanel1();
Doing this destroys Visual Studio every time, and to fix it requires using a text editor to put it back again. Therefore unless anyone can describe what is wrong with my implementation of this approach, perhaps calling the class the same as the control name isn't a great idea, I think the only safe and reliable way to achieve this is as Calanus describes in steps 1 to 5 or as an small deviation from that as Rob Windsor rightly points out restarting VS will bring the control into the Toolbox automatically. Unfortunately for me I then have to change all of the child controls over from the original class to my customised class :-(.
Within the same Solution this should work automatically. However, I have found that if the Target Framework aren't matching the Toolbox does not populate. ( I'm assuming really Reference needs to be of version same or lower than target of Reference. ) ( I did get a warning about non-matching Frameworks )
By making these the same Target Framework, Recompile, Restart VS. the control populated correctly. ( I also added the ToolboxItem(true) Attribute)

Visual Studio web user control events only show up in design mode?

Calling all Visual Studio gurus — when I'm working on a .ascx or .aspx file in a c# web project, the events do not show up in the properties panel unless I switch into the design view from the code view. Is this an intentional functionality of Visual Studio? Both VS2005 and VS2008 seem to work this way.
And is there any way to get the events to show up in the properties panel all the time?
I don't know if that's the way VS is 'intended to work, but yes that's a limitation. In case you've noticed sometimes clicking on the control and pressing F4 (or clicking on the properties tab) fails to load the properties for the correct control, and then you gotta select it from the list.
Sigh
That apart, if you make a usercontrol of your own, and give it an event, that event will not show up in the properties tab when you put it on a page. You'll have to capture it manually in the Page_Init event (like demonstrated by fallen888).
These days I don't bother with going to the properties tab to see an event. You can just as well type the event's name in the mark-up and then write it in the code-behind file.
Yes, that's how Visual Studio is intended to work. This doesn't help you view them in properties panel, but you can get a list of events (among other things) by typing the following in the code-behind:
"this." and intellisense should show you a list.
What I typically do is override the OnInit method and put all event handler mappings in there. So that it looks something like this:
override protected void OnInit(EventArgs e)
{
this.Load += new System.EventHandler(this.Page_Load);
this.myButton.Click += new System.EventHandler(this.myButton_Click);
base.OnInit(e);
}
If you do it using intellisense, as soon you type in "+= " you'll have the option to auto-complete that line and the event handler method's signature as well.
Yep, I wish we had a similar level of event autocompletion that we get with WPF where you can see the event name in IntelliSense and get it to automatically create a new stub event for you in code behind :(

Categories