@manhng

Welcome to my blog!

WinForm Menu

December 11, 2017 16:56

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
/*
* Working with OwnerDraw Menus in VB.NET
* https://www.codeproject.com/KB/menus/VbNetOwnerDrawMenu.aspx?msg=1783725#xx1783725xx
* Adding icons to menus in VB.NET
* https://www.codeproject.com/Articles/2481/Adding-icons-to-menus-in-VB-NET
* https://www.codeproject.com/Articles/5461/Working-with-OwnerDraw-Menus-in-VB-NET
* https://www.codeproject.com/Articles/20647/Dual-Menus-Add-a-Left-click-Menu-to-your-Applicati
* https://www.codeproject.com/Articles/11510/Add-Transparent-Menus-and-XP-Titlebar-Buttons-to-y
*/
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
// https://stackoverflow.com/questions/13721627/how-to-add-sub-items-to-a-menustrips-toolstripmenuitem-in-c-sharp
// https://stackoverflow.com/questions/1757574/dynamically-adding-toolstripmenuitems-to-a-menustrip-c-winforms
//
var item = new System.Windows.Forms.ToolStripMenuItem()
{
Name = "Test",
Text = "Test"
};
item.Click += new EventHandler(MenuItemClickHandler);

// https://www.flaticon.com/free-icon/user_181549
item.Image = Image.FromFile(@"C:\Users\manhnguyenv\Pictures\user.png");

var item2 = new System.Windows.Forms.ToolStripMenuItem()
{
Name = "Test",
Text = "Test"
};

item2.Click += new EventHandler(MenuItemClickHandler);

// https://www.flaticon.com/free-icon/user_181549
item2.Image = Image.FromFile(@"C:\Users\manhnguyenv\Pictures\user.png");

item.DropDownItems.Add(item2);

menuStrip1.Items.Add(item);
}

private void MenuItemClickHandler(object sender, EventArgs e)
{
ToolStripMenuItem clickedItem = (ToolStripMenuItem)sender;
clickedItem.Checked = true;
}
}
}

Categories

Recent posts