fork download
  1. public partial class MainWindow : Form
  2. {
  3. public MainWindow()
  4. {
  5. InitializeComponent();
  6. AssociateToolStripItems();
  7. }
  8.  
  9. private void AssociateToolStripItems()
  10. {
  11. var menuitems = GetToolStripItems<ToolStripMenuItem>(menuStrip, "mi");
  12. var buttons = GetToolStripItems<ToolStripButton>(toolStrip, "btn");
  13.  
  14. foreach (var key in menuitems.Keys)
  15. {
  16. if (!buttons.ContainsKey(key))
  17. continue;
  18.  
  19. var m = menuitems[key];
  20. var b = buttons[key];
  21. m.EnabledChanged += (s, e) => { b.Enabled = m.Enabled; };
  22. m.CheckStateChanged += (s, e) => { b.CheckState = m.CheckState; };
  23. }
  24. }
  25.  
  26. private Dictionary<string, T> GetToolStripItems<T>(
  27. ToolStrip toolStrip, string prefix) where T : ToolStripItem
  28. {
  29. var allitems = new Dictionary<string, T>();
  30.  
  31. Action<ToolStripItemCollection> subfunc = null;
  32. subfunc = (items) =>
  33. {
  34. foreach (var item in items)
  35. {
  36. if (item is T)
  37. {
  38. var name = (item as T).Name;
  39. if (name.StartsWith(prefix) && name.Length > prefix.Length)
  40. allitems[name.Substring(prefix.Length)] = item as T;
  41. }
  42.  
  43. if (item is ToolStripDropDownItem)
  44. subfunc((item as ToolStripDropDownItem).DropDownItems);
  45. }
  46. };
  47. subfunc(toolStrip.Items);
  48.  
  49. return allitems;
  50. }
  51. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty