Create parent menu in AddIn using c# - 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]

Related

Coded UI Test: The test run will not open a simple menu

I am being asked to evaluate Visual Studio for our automation testing efforts.
I have chosen to create a simple Coded UI Test where I simply want to open up Minesweeper (Win7 version), play with the custom game options and then close the application.
Every time I Run the test, it will not open the 'Game' menu item.
// Click 'Game' -> 'Options F5' menu item
Mouse.Click(uIOptionsF5MenuItem, new Point(26, 6));
Now, if I myself click the menu item (basically giving the test a 'push'), the automation takes over by selecting the menu option I need and then will run without any issue. I just can't figure out why the test can not open this menu on its own. I've tried the SetFocus method already and I haven't found any other options. The MSDN documentation offers ZERO help.
Anyone have an idea how I can get this menu to open?
Result Message:
Test method Blorg.CodedUITest1.CodedUITestMethod1 threw exception: Microsoft.VisualStudio.TestTools.UITest.Extension.UITestControlNotFoundException: The playback failed to find the control with the given search properties. Additional Details:
TechnologyName: 'MSAA'
ControlType: 'MenuItem'
Name: 'Options F5'
Try adding the menu item and perform the click like this,
WinMenuItem uIOptionsF5MenuItem = new WinMenuItem(Add the window where the menu is placed) {TechnologyName="MSAA" };
uIOptionsF5MenuItem.SearchProperties.Add("Name", "Options F5");
Mouse.Click(uIOptionsF5MenuItem);

How to add ToolStripSplitButton to Visual Studio Toolbox?

I am using Visual Studio 2010, and I have come across a need to use the ToolStripSplitButton button. I figured out from the reference material that ToolStripSplitButton is a form found under the System.Windows.Forms.
When I attempt to add this to my toolbox to try to use, I am unable to find ToolStripSplitButton, does anybody know how I can add this to my Visual Studio 2010 so I can use it? I don't care if it's not in the toolbox, I just want to make sure the assembly reference is present so I can actually use it.
ToolStripSplitButton can be used only as ToolStripItem of a ToolStrip (contained in ToolStrip.Items). It's designed so that you can't use it as a normal control. It doesn't have a Parent property, and I guess it also doesn't have a Handle property. The inheritance tree is:
ToolStripSplitButton
-> ToolStripDropDownItem
-> ToolStripItem
-> Component
-> ...
Normally, you have to add some ToolStrip to host ToolStripSplitButton.
If you want to use code, here is a simple example:
ToolStrip toolStrip1 = new ToolStrip();
ToolStripSplitButton splitButton1 = new ToolStripSplitButton("Hi there"){Owner = toolStrip1};
//you can add items to ToolStripSplitButton via ToolStripSplitButton.DropDownItems
More info ToolStripSplitButton
It's inherited from and dependent on ToolStripDropDown so you can't use it as a stand-alone control.
You can use it as a child control for ToolStripDropDown.

c# Excel - Display ribbon on exit

I'm using VSTO to design an application with an Excel interface. I want to hide the ribbon on startup (shouldn't be needed in the application) and re-display it on exit (if the user had it originally displayed), to avoid irritating people who use the application and want a ribbon the next time they open Excel.
I can hide the ribbon using essentially the following code in ThisWorkbook_Startup (from this question Excel 2007 Minimize the Ribbon programatically but Not the menu bar):
Office.CommandBars cbs = null;
cbs = Application.CommandBars;
foreach (Office.CommandBar commandBar in cbs)
{
if (commandBar.Name == "Ribbon")
{
this.Application.ActiveWindow.Activate();
Application.SendKeys("^{F1}", true);
}
}
However, the same code or similar variations from the previously referenced question do not seem to work when placed in either the ThisWorkbook_Shutdown or ThisWorkbook_BeforeClose methods. The code is hit but never seems to execute - the ribbon is never restored.
Is there another way to restore the ribbon on exit?
Thanks,
Andrew

Extend VS2010 Form Designer Context Menu

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!");
}

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.

Categories