public partial class MainWindow : Form
{
public MainWindow()
{
InitializeComponent();
AssociateToolStripItems();
}
private void AssociateToolStripItems()
{
var menuitems = GetToolStripItems<ToolStripMenuItem>(menuStrip, "mi");
var buttons = GetToolStripItems<ToolStripButton>(toolStrip, "btn");
foreach (var key in menuitems.Keys)
{
if (!buttons.ContainsKey(key))
continue;
var m = menuitems[key];
var b = buttons[key];
m.EnabledChanged += (s, e) => { b.Enabled = m.Enabled; };
m.CheckStateChanged += (s, e) => { b.CheckState = m.CheckState; };
}
}
private Dictionary<string, T> GetToolStripItems<T>(
ToolStrip toolStrip, string prefix) where T : ToolStripItem
{
var allitems = new Dictionary<string, T>();
Action<ToolStripItemCollection> subfunc = null;
subfunc = (items) =>
{
foreach (var item in items)
{
if (item is T)
{
var name = (item as T).Name;
if (name.StartsWith(prefix) && name.Length > prefix.Length)
allitems[name.Substring(prefix.Length)] = item as T;
}
if (item is ToolStripDropDownItem)
subfunc((item as ToolStripDropDownItem).DropDownItems);
}
};
subfunc(toolStrip.Items);
return allitems;
}
}
ICAgIHB1YmxpYyBwYXJ0aWFsIGNsYXNzIE1haW5XaW5kb3cgOiBGb3JtCiAgICB7CiAgICAgICAgcHVibGljIE1haW5XaW5kb3coKQogICAgICAgIHsKICAgICAgICAgICAgSW5pdGlhbGl6ZUNvbXBvbmVudCgpOwogICAgICAgICAgICBBc3NvY2lhdGVUb29sU3RyaXBJdGVtcygpOwogICAgICAgIH0KCiAgICAgICAgcHJpdmF0ZSB2b2lkIEFzc29jaWF0ZVRvb2xTdHJpcEl0ZW1zKCkKICAgICAgICB7CiAgICAgICAgICAgIHZhciBtZW51aXRlbXMgPSBHZXRUb29sU3RyaXBJdGVtczxUb29sU3RyaXBNZW51SXRlbT4obWVudVN0cmlwLCAibWkiKTsKICAgICAgICAgICAgdmFyIGJ1dHRvbnMgPSBHZXRUb29sU3RyaXBJdGVtczxUb29sU3RyaXBCdXR0b24+KHRvb2xTdHJpcCwgImJ0biIpOwoKICAgICAgICAgICAgZm9yZWFjaCAodmFyIGtleSBpbiBtZW51aXRlbXMuS2V5cykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaWYgKCFidXR0b25zLkNvbnRhaW5zS2V5KGtleSkpCiAgICAgICAgICAgICAgICAgICAgY29udGludWU7CgogICAgICAgICAgICAgICAgdmFyIG0gPSBtZW51aXRlbXNba2V5XTsKICAgICAgICAgICAgICAgIHZhciBiID0gYnV0dG9uc1trZXldOwogICAgICAgICAgICAgICAgbS5FbmFibGVkQ2hhbmdlZCArPSAocywgZSkgPT4geyBiLkVuYWJsZWQgPSBtLkVuYWJsZWQ7IH07CiAgICAgICAgICAgICAgICBtLkNoZWNrU3RhdGVDaGFuZ2VkICs9IChzLCBlKSA9PiB7IGIuQ2hlY2tTdGF0ZSA9IG0uQ2hlY2tTdGF0ZTsgfTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICAKICAgICAgICBwcml2YXRlIERpY3Rpb25hcnk8c3RyaW5nLCBUPiBHZXRUb29sU3RyaXBJdGVtczxUPigKICAgICAgICAgICAgVG9vbFN0cmlwIHRvb2xTdHJpcCwgc3RyaW5nIHByZWZpeCkgd2hlcmUgVCA6IFRvb2xTdHJpcEl0ZW0KICAgICAgICB7CiAgICAgICAgICAgIHZhciBhbGxpdGVtcyA9IG5ldyBEaWN0aW9uYXJ5PHN0cmluZywgVD4oKTsKCiAgICAgICAgICAgIEFjdGlvbjxUb29sU3RyaXBJdGVtQ29sbGVjdGlvbj4gc3ViZnVuYyA9IG51bGw7CiAgICAgICAgICAgIHN1YmZ1bmMgPSAoaXRlbXMpID0+IAogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBmb3JlYWNoICh2YXIgaXRlbSBpbiBpdGVtcykKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBpZiAoaXRlbSBpcyBUKQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgdmFyIG5hbWUgPSAoaXRlbSBhcyBUKS5OYW1lOwogICAgICAgICAgICAgICAgICAgICAgICBpZiAobmFtZS5TdGFydHNXaXRoKHByZWZpeCkgJiYgbmFtZS5MZW5ndGggPiBwcmVmaXguTGVuZ3RoKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgYWxsaXRlbXNbbmFtZS5TdWJzdHJpbmcocHJlZml4Lkxlbmd0aCldID0gaXRlbSBhcyBUOwogICAgICAgICAgICAgICAgICAgIH0KCiAgICAgICAgICAgICAgICAgICAgaWYgKGl0ZW0gaXMgVG9vbFN0cmlwRHJvcERvd25JdGVtKQogICAgICAgICAgICAgICAgICAgICAgICBzdWJmdW5jKChpdGVtIGFzIFRvb2xTdHJpcERyb3BEb3duSXRlbSkuRHJvcERvd25JdGVtcyk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH07CiAgICAgICAgICAgIHN1YmZ1bmModG9vbFN0cmlwLkl0ZW1zKTsKCiAgICAgICAgICAgIHJldHVybiBhbGxpdGVtczsKICAgICAgICB9CiAgICB9