private void button1_Click(object sender, System.EventArgs e) { try { // Create the command holder. This is essential for all C1Command operation C1CommandHolder ch = C1CommandHolder.CreateCommandHolder(this); // Use the pre-built image list ch.ImageList = this.imageList1; // Create and set up the Cut command C1Command cmdCut = ch.CreateCommand(); cmdCut.Text = "C&ut"; cmdCut.Shortcut = Shortcut.CtrlX; cmdCut.ImageIndex = 0; cmdCut.Click += new C1.Win.C1Command.ClickEventHandler(clickCut); cmdCut.CommandStateQuery += new C1.Win.C1Command.CommandStateQueryEventHandler(queryCut); // Create and set up the Copy command C1Command cmdCopy = ch.CreateCommand(); cmdCopy.Text = "&Copy"; cmdCopy.Shortcut = Shortcut.CtrlC; cmdCopy.ImageIndex = 1; cmdCopy.Click += new C1.Win.C1Command.ClickEventHandler(clickCopy); cmdCopy.CommandStateQuery += new C1.Win.C1Command.CommandStateQueryEventHandler(queryCopy); // Create and set up the Paste command C1Command cmdPaste = ch.CreateCommand(); cmdPaste.Text = "&Paste"; cmdPaste.Shortcut = Shortcut.CtrlV; cmdPaste.ImageIndex = 2; cmdPaste.Click += new C1.Win.C1Command.ClickEventHandler(clickPaste); cmdPaste.CommandStateQuery += new C1.Win.C1Command.CommandStateQueryEventHandler(queryPaste); // Create and set up the Undo command C1Command cmdUndo = ch.CreateCommand(); cmdUndo.Text = "&Undo"; cmdUndo.Shortcut = Shortcut.CtrlZ; cmdUndo.ImageIndex = -1; cmdUndo.Click += new C1.Win.C1Command.ClickEventHandler(clickUndo); cmdUndo.CommandStateQuery += new C1.Win.C1Command.CommandStateQueryEventHandler(queryUndo); // Create the context menu to hold other commands C1ContextMenu cm = ch.CreateCommand(typeof(C1ContextMenu)) as C1ContextMenu; // Fill it with the links to the commands cm.CommandLinks.Add(new C1CommandLink(cmdCut)); cm.CommandLinks.Add(new C1CommandLink(cmdCopy)); cm.CommandLinks.Add(new C1CommandLink(cmdPaste)); C1CommandLink cl = new C1CommandLink(cmdUndo); // This will show a delimiter before the Undo link. // Another option is to create a separate empty link with the text "-". cl.Delimiter = true; cm.CommandLinks.Add(cl); // Assign the new context menu to the text box ch.SetC1ContextMenu(textBox1, cm); } catch { // Not the best way to code, but used here to illustrate a point: // It is impossible to create more than one command holder for a form. // The 2nd attempt will fail with an exception. Here we catch and ignore // it... } }
private void Form1_Load(object sender, System.EventArgs e) { C1CommandHolder ch = C1CommandHolder.CreateCommandHolder(this); // In this sample, we use a single event handler for all commands, // and use a switch statement inside that handler to select the specific // action. Alternatively, each command can be assigned its own Click // event handler. Or, a mix of the two approaches is possible. ch.CommandClick += new CommandClickEventHandler(CommandClickHandler); // use the image list for command images ch.ImageList = this.imageList1; // modern look: ch.LookAndFeel = LookAndFeelEnum.Office2003; C1MainMenu mm = new C1MainMenu(); this.Controls.Add(mm); // For the main menu, its CommandHolder property should be set // (this is not required for builds 1.0.20041.47 or later of C1Command). mm.CommandHolder = ch; // Main menu - File C1CommandMenu mFile = (C1CommandMenu)ch.CreateCommand(typeof(C1CommandMenu)); mFile.Text = "&File"; mm.CommandLinks.Add(new C1CommandLink(mFile)); // create commands for file ops C1Command cNew = ch.CreateCommand(); cNew.Text = "&New"; // UserData is arbitrary data associated with commands; // In this example we use text labels to recognize specific commands // in the single command handler. Alternatively, we could have commands // as members of our class, and compare objects to recognize commands. cNew.UserData = "file_new"; cNew.Shortcut = Shortcut.CtrlN; cNew.ImageIndex = 1; C1Command cOpen = ch.CreateCommand(); cOpen.Text = "&Open"; cOpen.UserData = "file_open"; cOpen.Shortcut = Shortcut.CtrlO; cOpen.ImageIndex = 0; C1Command cExit = ch.CreateCommand(); cExit.Text = "E&xit"; cExit.UserData = "exit"; cExit.Shortcut = Shortcut.CtrlX; mFile.CommandLinks.Add(new C1CommandLink(cNew)); mFile.CommandLinks.Add(new C1CommandLink(cOpen)); mFile.CommandLinks.Add(new C1CommandLink()); mFile.CommandLinks[mFile.CommandLinks.Count - 1].Text = "-"; mFile.CommandLinks.Add(new C1CommandLink(cExit)); // Main menu - Window C1CommandMenu mWindow = (C1CommandMenu)ch.CreateCommand(typeof(C1CommandMenu)); mWindow.Text = "&Window"; mm.CommandLinks.Add(new C1CommandLink(mWindow)); C1Command cNewWindow = ch.CreateCommand(); cNewWindow.Text = "New &Window"; cNewWindow.UserData = "window_new"; cNewWindow.Shortcut = Shortcut.CtrlW; cNewWindow.ImageIndex = 2; mWindow.CommandLinks.Add(new C1CommandLink(cNewWindow)); // For toolbars to be dockable/floatable, we must put them // in a C1CommandDock: C1CommandDock dock = new C1CommandDock(); this.Controls.Add(dock); dock.Dock = DockStyle.Top; // Add a toolbar, link it to the (already existing) commands: C1ToolBar tb = new C1ToolBar(); // add file commands and the window menu to the toolbar tb.CommandLinks.Add(new C1CommandLink(cNew)); tb.CommandLinks.Add(new C1CommandLink(cOpen)); C1CommandLink cl; tb.CommandLinks.Add(cl = new C1CommandLink(mWindow)); // because we did not provide an image for the window menu, // make it show as text cl.ButtonLook = ButtonLookFlags.Text; tb.CommandLinks.Add(cl = new C1CommandLink(cExit)); // ditto for the exit command cl.ButtonLook = ButtonLookFlags.Text; // add the new toolbar to the dock dock.Controls.Add(tb); }