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.
Related
I am making a form and would like to use a ToolStripDropDownButton (basically a button with an arrow pointing down from where u can select an item):
as found in the Microsoft website but I cannot seem to find it in the form[design] toolbox.
I am using VS2015 C# community version, net framework 4.6. I have tried adding the namespace using System.Windows.Forms.ToolStripDropDownButton; but it does not recognize toolstripdropdownbutton. Any help with this would be appreciated. Thanks.
The problem in your code is because System.Windows.Forms.ToolStripDropDownButton is a class, is not a namespace.
Any idea how I can use that class to create the
toolstripdropdownbutton control?
Using Code:
var item = new ToolStripDropDownButton("SomeText");
item.DropDownItems.Add(new ToolStripMenuItem("SubMenu1"));
item.DropDownItems.Add(new ToolStripMenuItem("SubMenu1"));
this.toolStrip1.Items.Add(item);
You can also take a look at below constructor to add an image and an event handler and see more example at bottom of documentation page:
ToolStripDropDownButton Constructor (String, Image, EventHandler)
Result:
Using the designer:
You can put a ToolStrip on form, then click the dropdown of add item. then select DropDownButton.
More information:
How to: Add ToolStrip Items Dynamically
ToolStrip Control Overview
I have a menustrip that I create using the forms designer. I then want to programmatically add a toolstrip toolbar UNDER the menustrip. The reason is that this toolbar is a set of tools that I add as a plugin to the main application. As such, when the plugin is loaded, I create the toolbar.
When I manually add the toolstrip to the form using the forms designer, the toolstrip is correctly positioned under the menustrip. However when I add it programmatically, it snaps to the topmost part of the form, above the menustrip. Here's the code I use to programmatically add the toolstrip:
stereoBar = new ToolStrip();
stereoBar.Anchor = (AnchorStyles)(AnchorStyles.Top | AnchorStyles.Left);
//y location is set to 22, the width of the menustrip
stereoBar.Location = new System.Drawing.Point(0, 22);
stereoBar.Dock = DockStyle.Top;
stereoBar.Name = "StereoToolbar";
stereoBar.Text = "Stereo Plugin Toolbar";
stereoBar.ShowItemToolTips = true;
stereoBar.GripMargin = new Padding(2);
Controls.Add(stereoBar);
Is there anything simple I am missing here?
Thanks in advance!
As described also in this answer, when docking controls in a form the order in which you add them to the form matters; i.e. when adding multiple controls that all dock to the top, the last control that gets added will be the topmost.
So, because you add your toolstrip programmatically and your menu strip via designer, the designer code gets executed first thus having the menu strip always at the bottom.
I think there are three ways out of the dilemma:
First approach
As Hans Passant pointed out, the easiest way to get things into the right order would be to simply call
stereoBar.BringToFront();
right after you added it to the forms' controls.
Second approach
To circumvent this you could also also add the menu strip programmatically and do this after you added the tool strip.
Third approach
Another way out may be to add another container to the form (Panel or groupbox for example) via designer that also docks to the top that simply just functions as a placeholder where you add your tool strip to (so you do not add to the form directly anymore)
Can you tell me how to create new visual component for .net 4.0 in Visual Studio 2010,
i have a book about component creation in Russian Language (Pavel Agurov -Razrabotka Komponentov v MS Visual Studio 2005/2008), but methods in this book covers VS 2005 and 2008. And methods gives some errors in VS 2010.
EDIT:
There is my code
public partial class exComboBox : ComboBox
{
/*public exComboBox()
{
InitializeComponent();
}*/
private System.Collections.Specialized.StringCollection _itemValues = new System.Collections.Specialized.StringCollection();
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Editor("System.Windows.Forms.Design.StringCollectionEditor,System.Editor","System.Drawing.Design.UITypeEditor,System.Drawing")]
public System.Collections.Specialized.StringCollection ItemValues
{
get
{
return _itemValues;
}
set
{
_itemValues = value;
}
}
public object SelectedItemValue
{
get
{
return _itemValues[SelectedIndex];
}
}
}
And when I try to add this component to new form and add values for ItemValues it says
Constructor on type 'System.String' not found.
Right click your project, choose to add a new item. Go to the C# section and select User Control.
This will create a windows control that will appear at the top of the Toolbox when you use the forms designer. Just drag and drop it into your form like any other control.
If you wish to derive it from Combo Box, then just edit the class that is created and change it's base class from a UserControl to a combo box. You might also need to change your constructor top call three base class constructor rather than doing InitialiseComponent for itself.
If you want to use this control in many applications, then you can put the code into a Class Library project, which will make a .dll assembly that other projects can reference to gain access to the control.
Create a Windows Application and then go to Add New Item menu by right clicking on Project in Solution Explorer and you can see there the Component AFAIR.
Assuming you're talking about Winforms...
Deriving from existing controls is probably not something you'll want to do. Especially if you want to change their default behavior or replace their existing properties. Sooner or later you will be facing severe limitations. Believe me, I've been there.
If you want an easy way to repeat some kind of pattern for a ComboBox, I suggest you take a look at IExtenderProvider interface. You can implement it on a Component-derived class that you can drag on the design surface. In this component, you can declare properties that will be attached as extra properties to any comboboxes (or anything else, it's all up to you).
Since the component will know about whatever is attached to it, it will be able to declare event handlers over any attached control / component. For example, if you want your combobox to have items already present, you could handle it's Load event and add the items there.
To learn more about IExtenderProvider, go here.
The ToolboxItem(false) works for Winforms controls.
ToolboxBrowsable(false) does not work for the WPF controls.
[ToolboxBrowsable(false)]
public class MyControl : UserControl
{
}
The MyControl even with ToolboxBrowsable(false) shows up in the Visual Studio toolbox.
What am I doing wrong?
-Datte
The [ToolboxBrowsable(false)] attribute does not have any effect within the same solution.
This is because you might want to be able to use the control in any forms within that solution.
If you compile the class library, remove it from the solution and add a reference to it you should see the control disappearing from the toolbox.
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.