Visual C# Add Item to contextMenuStrip with link - c#

How can I add an item to a contextmenustrip, as well as code the button_click action for it?
I'm making a web browser application, and I have a drop down menu of 'favorite' websites. When someone clicks a button, it adds the title of the website into the dropdown menu, but I also need it navigable to the website URL that was inputted. I have no idea how to add button click actions when I add a new item to a contextmenustrip.

Is this a WinForms application?
If so, in the Visual Studio designer, after you drag a contextMenuStrip from the boolbox, it allows you to type new entries at the contextMenuStrip (click where it says 'Type Here' to add a new item). If you are going back to an existing contextMenuStrip, you will have to make it appear by clicking on the contextMenuStrip item at the bottom of the designer.
Once you have the item, you can just double-click on it to whatever code you want to execute on the click event. (This is the default event; for other events you will have to double-click the appropriate event from the Properties pane.

Related

Stop displaying type cursor when clicked on a toolstrip

Im am making a toolstrip for my windows application in c#,
but when I press on a option of my toolstrip, the type cursor shows up.
It want it to be like a button, when you click on it something the code indicates happens. and not having the type cursor show up
Here is a photo of my problem:
I've already set ReadOnly to True, But this only prevents the user from typing in the toolstrip, not removing the type/text cursor.
I've tried Toolstrip.Cursor.Dispose(), But it gave me an error that toolstrip had no atribute Cursor. Al tough is should work on textboxes (haven't tried it on textboxes yet, because I have no need for that)
I'm a beginner with c#, but have some experience with python, so please go easy on me.
I expect that When I click on the box, The text/type cursor doesnt appear.
I use visual studio btw. I created the toolstrip in Design mode.
I Created A toolstrip, with a menu item, and In that menu item a textbox.
I shouldn't have created a textbox, but another menu item, and if that was pressed execute some code.
So If I just create a menu item in the toolstrip and create another menu item in the toolstrip, and set click to one of my code functions it acts as a button.

C# TabControl ContextMenuStrip

In a user control of mine I implement a tab control that should programmatically manage tab pages. I connected the tab control with a context menu strip with the menu items "Add", "Edit", "Delete" to respectively add a new tab page, edit or delete an existing one. Initially, the tab control does not have any tab pages, and in this case the context menu strip does not appear on right mouse click; if a tab page is there, the context menu strip works as required. At that, the context menu strip is attached to the tab control itself, not to any of the tab pages.
I find this state quite illogical, and my question is whether there is any possibility to make the context menu work attached to a tab control work even if the tab control is empty?
Empty TabControl does not receive mouse events. They are passed to the underlying control.
You can do the following.
Put the TabControl inside a Panel of the same size. Assign the same context menu to this Panel. Then, when TabControl is empty, mouse events will be passed to the Panel and menu will be shown too.

cannot add items to ContextMenuStrip on inherited winform

My setup is very simple.
I have a form called FormBaseList, on that form there is a DataGridView and a ContextMenuStrip. The ContextMenuStrip is coupled to the DataGridView and has 2 menuitems.
Now I add a new form to my project, using add Windows Form and then I choose "Windows Forms" / "Inherited Form". As base I choose my FormBaseList.
So now I have a new form, called FormSomethingList that is derived from FormBaseList.
In the visual designer I can now add an additional MenuItem to the ContextMenuStrip on FormSomethingList, but if I compile and run the application, that new MenuItem is gone. When I open FormSomeThingList in the designer the new MenuItem is also gone...
Is this "normal" behaviour or is there something wrong with my project ? I suspect the first but would like some confirmation. And if this is indeed "normal" behaviour, how can I workaround it without doing it all in code.
You don't even have to run the application. If you just rebuild, you will see that the menu option is gone. With each build, you are telling Form2 that it is a form 1, and the context menu gets set to what that is. In fact, notice that in form 2, the properties for the context menu are not editable. Unfortunately, the GUI allows you to type in additional values for the context menu, but if you try to change this by changing the "Items" collection property, in the property window, you will not be able to.
You will just need to add the item programatically. But that is not such a big deal. When you added the menu item in form 2, after you rebuilt, it disappeared from the context menu, but it is still there. Look in the designer code and you will see it. The menu item is still defined as part of form 2, but it's just been disconnected from the context menu. So on form2's load event, you can just readd it.
contextMenuStrip1.Items.Add(myAddedMenuStripItem);
Look in the designer.cs to see the name of the item you added to the context menu.

Putting hotkey/shortcut text next to toolstrip menu items in winforms

I want to be able to show the hotkey combination assigned to a toolstrip menu item in winforms. For instane, in any program (even your browser settings menu) you can see various menu items, and generally, aligned to the right of their item, is their hotkey shortcut. I want to do this programmatically.
Example: Instead of typing
Open a file (ctrl+O)
I want to have the properties show up independently of each other.
How can I achieve this?
You want to you use the ShortcutKeys property of the ToolStripMenuItem. This will let you pick the particular key combination you want for each menu item and it will show up to the right of the menu item. Make sure that you have ShowShortcutKeys property of the ToolStripMenuItem set to true.

One general context menu

I have one general context menu and for example 4 buttons and 4 textboxes - when I click on any of these buttons the context menu is opened and when I click on some items I would like to send this item to textbox, which was between button - I need to send some identificator to context menu to know which button open it and then to know where to send selected item.
How can I do that?
I am using c#
Thanks
this.ActiveControl will give you the button that has the focus. You can put the identifier in the Tag property of each button. This way, this.ActiveControl.Tag will give you the information you need.

Categories