I want to add a feature to my MenuStrip where I want there to be an option where you can hover over or press the menu option to open recently opened projects .
File---> Recently Opened Projects---> {List of projects.....}
The same kind of option/menu that exists in Microsoft office products (e.g. word 2007).
I know how to get an array of the file names. I just need to know how to put the array of the names at the Sub MenuStrip.
You can add them dynamically in code:
private void menuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem item = new ToolStripMenuItem();
item.Text = "your file name";
item.Click += new EventHandler(yourEventHandler);
menuItem.DropDownItems.Add(item);
}
You need to create ToolStripMenuItems in a loop and call DropDownItems.Add to add them to your parent menu item.
In the loop, you should add a handler to their Click event.
Related
I have a question : I need to show pdf documents without askink what to open to the user. It needs to be matched with a unique id. Let's say I have a product_ID=55435, and I need to open the 55435.pdf by automatically in a windows form.
Thank you !
I suggest you follow the steps below:
(1) You can add ActiveX controls provided by Adobe to the toolbox.
Method: Right click on the blank of the toolbox=>choose items=>COM Components=>check AdobePDF Reader=>OK
As the picture shows:
(2) You can drag the Adobe PDF Reader control to the form, create a button button1, and add button events.
The button event code is as follows:
private void button1_Click(object sender, EventArgs e)
{
string filename =; // Example: filename = #"F:\SoftWare\PdfFile\55435.pdf";
axAcroPDF1.LoadFile(filename);
}
Running result:
I have a tab control that I can programmatically add and remove tabs from. It works fine but whenever I add a tab this tab is added end of the tab list. How can I add tabs beginning of the tabList dynamically.
This is the code I use to add tabs:
private void button1_Click(object sender, EventArgs e)
{
tabControl1.TabPages.Add("Kare");
}
Thanks from now.
I finally solved it:
you should change your code to that:
tabControl1.TabPages.Insert(0,"Başlık");
let me explain what it means insert is like add but instead of adding tabs to the end of the list it allows the user to choose where the tab will be added. In my case it will be the beginning of the tab list.
0 means beginning of the list or the first tab and "Başlık" stands for the name of my tab.
I need my app to create right-click context menu items (and sub-menu items). I'm not concerned with the code - but I don't know how to make sub-menu items in the registry. It's not as logical as one would expect.
I've searched countless times already and have officially given up searching for now.
I know that we can create a context menu item using regedit.exe by going to the shell key and adding a new one but how do I create sub menu items like 7zip for example?
Take a look at this Code-Project article: Add a context menu to the Windows Explorer. It seems to be very easy by using the Registry class provided by the .net framework.
Some more advanced/better solution seems to be using some library such as: SharpShell
EDIT
Please take a look at: .NET Shell Extensions - Adding submenus to Shell .
Ths part should solve your problem:
// <summary>
// Creates the context menu when the selected item is a folder.
// </summary>
protected void MenuDirectory()
{
ToolStripMenuItem MainMenu;
MainMenu = new ToolStripMenuItem
{
Text = "MenuDirectory",
Image = Properties.Resources.Folder_icon
};
ToolStripMenuItem SubMenu1;
SubMenu1 = new ToolStripMenuItem
{
Text = "DirSubMenu1",
Image = Properties.Resources.Folder_icon
};
var SubMenu2 = new ToolStripMenuItem
{
Text = "DirSubMenu2",
Image = Properties.Resources.Folder_icon
};
SubMenu2.DropDownItems.Clear();
SubMenu2.Click += (sender, args) => ShowItemName();
var SubSubMenu1 = new ToolStripMenuItem
{
Text = "DirSubSubMenu1",
Image = Properties.Resources.Folder_icon
};
SubSubMenu1.Click += (sender, args) => ShowItemName();
// Let's attach the submenus to the main menu
SubMenu1.DropDownItems.Add(SubSubMenu1);
MainMenu.DropDownItems.Add(SubMenu1);
MainMenu.DropDownItems.Add(SubMenu2);
menu.Items.Clear();
menu.Items.Add(MainMenu);
}
You have to create a command folder for example Archive, with two commands: A and B.
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\Archive]
"MUIVerb"="Archive"
"SubCommands"="Windows.A;Windows.B"
The * in key means this menu shows up at right click on any file. If you want it only at the *.7z files, use HKEY_CLASSES_ROOT\.7z\shell\Archive. The value of MUIVerb will be the name of menu item. If you named the MUIVerb to 7-Zip, the right click menu will contains two 7-Zip items.
Then create the commands there:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Windows.A]
"MUIVerb"="Command name of A"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Windows.A\command]
#="notepad.exe \"%1\""
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Windows.B]
"MUIVerb"="Command name of B"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Windows.B\command]
#="notepad.exe \"%1\""
In this example, you get an Archive menuitem, with cascaded two command, whats open the current file with notepad. It works with Windows 7 and newer.
I am trying to edit treeview node names in a treeview (they become editable on a button click ) and then I want them to remain saved (if I exit and enter the aplication again, the new, edited names should be displayed), BUT they always revert to the original name(text) on program reentering.
private void button1_Click(object sender, EventArgs e)
{
treeView1.LabelEdit = true;
}
Questions is how to make the new treeview node names be saved after editing, so when I enter the aplication again they don't reset to the old ones.
You can use the event on application shutdown to manually iterate over the TreeView and save the data to a file. Then when you applications starts again read them and populate the TreeView.
You cannot edit your project by modifying the developped program runtime.
I searched the internet for this but i couldn't find how to do it with C#
What i am trying to do is make it so that when i click on my NewTab button, a new tab appears with the same controls that were on the first tab. I saw some information on how to add a UserControl to your form, but C# doesn't have anything like that.
And for everyone who would say "Post your code", i don't have any, so don't bother saying that, the only code i have is the code for the program and that wouldn't help anyone.
EDIT
I have rewritten my solution to use reflection.
using System.Reflection;
// your TabControl will be defined in your designer
TabControl tc;
// as will your original TabPage
TabPage tpOld = tc.SelectedTab;
TabPage tpNew = new TabPage();
foreach(Control c in tpOld.Controls)
{
Control cNew = (Control) Activator.CreateInstance(c.GetType());
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(c);
foreach (PropertyDescriptor entry in pdc)
{
object val = entry.GetValue(c);
entry.SetValue(cNew, val);
}
// add control to new TabPage
tpNew.Controls.Add(cNew);
}
tc.TabPages.Add(tpNew);
Some information can be found here.
http://www.codeproject.com/Articles/12976/How-to-Clone-Serialize-Copy-Paste-a-Windows-Forms
Your best bet would be to look at this article:
Code Project
Then apply the following code to add the cloned control (this would be in your button click handler code (based on article):
private void button1_Click(object sender, EventArgs e)
{
// create new tab
TabPage tp = new TabPage();
// iterate through each control and clone it
foreach (Control c in this.tabControl1.TabPages[0].Controls)
{
// clone control (this references the code project download ControlFactory.cs)
Control ctrl = CtrlCloneTst.ControlFactory.CloneCtrl(c);
// now add it to the new tab
tp.Controls.Add(ctrl);
// set bounds to size and position
ctrl.SetBounds(c.Bounds.X, c.Bounds.Y, c.Bounds.Width, c.Bounds.Height);
}
// now add tab page
this.tabControl1.TabPages.Add(tp);
}
Then you would need to hook the event handlers up. Will have to think about this.
I know it's an old thread but I just figured out a way for myself and thought I should share it. It's really simple and tested in .Net 4.6.
Please note that this solution does not actually create new controls, just re-assigns them all to new TabPage, so you have to use AddRange each time you change tabs. New tab will show the exact same controls, content and values included.
// Create an array and copy controls from first tab to it.
Array tabLayout = new Control [numberOfControls];
YourTabControl.TabPages[0].Controls.CopyTo(tabLayout, 0);
// AddRange each time you change a tab.
YourTabControl.TabPages[newTabIndex].Controls.AddRange((Control[])tabLayout);