How to implement Visual Studio style Add or Remove Buttons toolstrip - c#

I want to implement visual studio style Add or Remove Ruttons toolstrip like following
in my winforms application, how can I achieve this?
I haven't tried anything on this as I am not sure how to start and I don't have much working experience on winforms.
Please suggest.

At first glance it doesn't look all that difficult.
Just add a ToolStripDropDownButton to your ToolStrip with no image or text. That will make the appearance seem more or less similar.
Add to this drop down button one ToolStripMenuItem with a "Add or Remove Buttons" caption. We'll call it AddRemoveMenuItem.
Now populate AddRemoveMenuItem's child menu items with menu items representing all your configurable ToolStripItems. You can link menu item and configurable tool strip item through the menu item's Tag property (you could also subclass ToolStripMenuItem adding a ToolStripItem LinkedToolStripItem { get; set; } property but I don't think its really worth it).
All these "linked" menu items will have to handle their Click events where they will switch their linked tool strip item's Visible property and synchronize their Checked state accordingly.
I'd do that the following way:
linkedMenuItem.Click += (sender, e) => linkedMenuItem.Checked = !linkedMenuItem.Checked;
linkedMenuItem.CheckedChanged +=
(sender, e) =>
{
var linkedToolStripItem = linkedMenuItem.Tag as ToolStripItem;
if (linkedToolStripItem != null)
{
linkedToolStripItem.Visible = linkedMenuItem.Checked;
}
};
When starting up your application set the linked menu items Checked state accordingly to your app's default settings, user settings, etc. and you are done.

Related

Accessing a combobox inside a ToolStrip

I have a ContextMenuStrip for a right-click context menu. Inside is a ToolStripMenuItem that contains a ToolStripItem array. Inside the array is a dynamically created ToolStripComboBox.
The issue I am having is that I cannot access the meat of the combobox. It has been linking to a databinding through the ComboBox. A general version of the code is below.
myToolStripComboBox.ComboBox.DataSource = enumList;
ToolStripItem[] toolStripItems = new ToolStripItem[1];
toolStripItems[0] = myToolStripComboBox;
ToolStripMenuItem sortOrder = new ToolStripMenuItem("Sorter", null, toolStripItems);
rightClickPopupMenu.Items.Add(sortOrder);
Looking through the debug shows that the datasource is indeed still set and I have all the required values, but the right-click context menu refuses to show anything but a blank combobox.
Try this :
private void chData_MouseMove(object sender, MouseEventArgs e)
{
try
{
//your combobox binding Code
tooltip.SetToolTip(cmdfoo, tipInfo);
}
catch { }
}
This is just a simple example.
Had the local c# wizard at work look at it. Something along the lines of the databinding not being called by a proper parent object.
The solution was to just directly add the times to the combo box items.
dropDown.ComboBox.Items.Add(enum);
So what I have done is make a list of the enum objects and done a foreach to add them all.

Customizing look of ContextMenuStrip

I created ContextMenuStrip for my tray icon, and i learned how to add new options there, how to add icons, how to mark them as checked, and how to change whole Context Background.
The thing is i dont know 2 things i would love to learn.
1.How to force newly created options to "continue" background of main Context menu, so they would show rest/same photo?
I created these marked items in code, when adding new folders in my program, folders names are added there as a groups.
2.How to change the look of ContextMenu completly. Is it possible in Visual Studio c#? Any tutorials? (i mean that program name on the left side of menu for example)
Example:
Image to example tray menu
EDIT: I use that do add items under "Grupy":
ToolStripItem item = (contextMenuStrip1.Items[2] as ToolStripMenuItem).DropDownItems.Add("" + Path.GetFileName(#"" + folderBrowserDialog1.SelectedPath));
item.Click += new EventHandler(item_Click);

(VB.NET / C#) How to create Vista-like style MenuStrip? [duplicate]

I noticed that adding a MenuStrip (from the Toolbox) to my form design doesn't yield a menu bar like the one seen in many native Windows applications. Instead I get a menu bar like Visual Studio's own. None of the style settings for MenuStrip appear to mimic the much more common native menu bar.
Is there a way to add a menu bar to my Windows Forms application that looks the same as the one you see in Notepad, Task Manager and others? (Preferably with the designer, but I wouldn't mind adding it programmatically either.)
Screenshot for illustration:
Go to your Toolbox, right click anywhere inside and select "Choose Items".
When the dialog loads and appears, scroll down til you see MainMenu. Add that to the toolbox, and you've got yourself a native menu bar!
You can do this by setting your form's Menu property, like this:
private void Form1_Load(object sender, EventArgs e)
{
this.Menu = new MainMenu();
MenuItem item = new MenuItem("File");
this.Menu.MenuItems.Add(item);
item.MenuItems.Add("Save", new EventHandler(Save_Click));
item.MenuItems.Add("Open", new EventHandler(Open_Click));
item = new MenuItem("Edit");
this.Menu.MenuItems.Add(item);
item.MenuItems.Add("Copy", new EventHandler(Copy_Click));
item.MenuItems.Add("Paste", new EventHandler(Paste_Click));
// etc ...
}
private void Save_Click(object sender, EventArgs e)
{
// save
}
These menus will look like "normal" system menus.
I couldn't find any designer support for this, though. In my defense, I didn't try real hard.
Instead of using a the MainMenu component you can create your own renderer for the MenuStrip component. The advantage here is being able to add images to MenuStripItem objects. Here is the pastebin for the custom renderer:
NativeRenderer
There are different themes that can be applied in the constructor of the renderer. Try them all to see the native themes. To use this renderer simply set the instance to the MenuStrip Renderer property:
menuStrip.Renderer = new NativeRenderer([theme]);
I normally set the MenuStrip's RenderMode to System which gives a minimalist, single colour menu (no gradients or anything decadent like that).
If that does not go far enough, then you'll likely have to jump through some low-level hoops to get what you want.

WPF Ribbon collapse and expand programmatically

With the latest (October 2010) WPF Ribbon libraries, there exists a menu item to minimize/maximize (or collapse/expand, if you prefer) the ribbon control.
Does anyone know if there's also a way to hook into the events that control this behaviour so that it could be controlled programmatically from separate UI?
Or, better yet, is there a way to get a collapse/expand button to display in the ribbon like the 2010 Office apps do?
You can use the boolean property IsMinimized on the Ribbon class to show/hide the ribbon itself. It is a dependency property, so you can bind to its value to support the scenarios you describe.
As far as I know, the default template does not have a show/hide button, like Office does, but it shouldn't be too hard to modify the template (using Blend) to add one.
If what you need is know when the bar gets minimized (this happens when you double click a tab header) you could hook to the IsMinimizedChanged event, but er.. it is missing.
Hopefully it is a DependencyProperty so you can successfully hook to any DependencyProperty change this way:
DependencyPropertyDescriptor.FromProperty(Ribbon.IsMinimizedProperty, typeof(Ribbon))
.AddValueChanged(ribbon, (o, args) => /* your code here */);
What I wanted to do (and hence got here) is to prevent it from minimizing when double clicking the header so I ended up using this code:
DependencyPropertyDescriptor.FromProperty(Ribbon.IsMinimizedProperty, typeof(Ribbon))
.AddValueChanged(ribbon, (o, args) => ribbon.IsMinimized = false);
Is not so fancy but gets the job done.
Add a toggle button(simple button and set its content to v or ^ depending upon the operation requested) and then you can use ContentControl in button click to fulfill your requirement:
ContentControl contentControl = FindVisualChildataBankyName<ContentControl>(rbnName, "mainItemsPresenterHost");
contentControl.Visibility = System.Windows.Visibility.Collapsed;
Use contentControl.Visibility = System.Windows.Visibility.Visible; in order to maximize the ribbon

How to disable ToolStripMenuItem in Context menu Dynamically?

In my windows application i have a context menu with a grid the problem is that I want to disable the ToolStripMenuItem in context menu according to the user previlages.How can i do that. i have done like this but it is not working
private void contextMenuStrip_Machine_Opening(object sender, CancelEventArgs e)
{
toolStripAuthorize.Enabled = INFOpermission.accessAuthorize;
}
but it is not working
You need to set toolStripAuthorize.Enabled to either true or false.
I have no idea what INFOpermission.accessAuthorize is because you didn't show the code that defines that (enum?), but if it's anything other than false, this isn't going to work out like you expect.
I can guarantee that setting the Enabled property of the ToolStripMenuItem that you want to disable to false in the Opening event handler will work. If it's not working for you, you're doing something else wrong, and you need to give us some more information to go on.
If you're stuck, see the sample code here: How to: Handle the ContextMenuStrip Opening Event
EDIT: Armed with new information provided in the comments, I've now isolated the source of the problem. You've assigned the ContextMenuStrip to the RowTemplate of a DataGridView control, and are therefore not able to modify items contained in that context menu in its Opening event handler method.
It turns out that this is a known bug that someone decided was "by design". You can see the original bug report here on Microsoft Connect. The explanation given is that whenever a new row is created based on the RowTemplate (which is how the RowTemplate works), the ContextMenuStrip that you've assigned gets cloned as well. That means the same context menu instance is not used for each row, and whatever properties that you try to set on the original menu items have no effect.
Fortunately, it also gives us a workaround. Like all events, the Opening event passes the actual instance of the ContextMenuStrip that is about to be opened as its sender parameter. This is the context menu whose items you need to modify in order for your alterations to be visible.
So what's the code? It looks like this:
private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
{
ContextMenuStrip cmnu = (ContextMenuStrip)sender;
cmnu.Items[1].Enabled = false;
}
Notice, though, that you'll have to reference the individual menu item that you want to modify by its index. This is just the zero-based position of the item in the menu that you want to modify. You can't use the toolStripAuthorize object like you were trying to do before because a new instance of it has been cloned for each new context menu instance.

Categories

Resources