[]
You can add a C1ContextMenu to a control at design time or through code. Click on either of the following links to expand the steps for the designer or for the code.
To add a C1ContextMenu to a Control at design time
To create a context menu and associate it with a menu item, complete the following tasks:
type=note
Note: In this example, a C1ContextMenu is associated with a menu item.
To add a C1ContextMenu to a control programmatically
To add a C1ContextMenu to a control, complete the following steps:
Attach the C1.Win.C1Command to your reference project located in the solution explorer, and then add the C1.Win.C1Command namespace to your source file.
Place the TextBox control onto your form using a drag-and-drop operation.
To create a C1CommandHolder so it will hold the command, double-click on the form control to create a Form_Load event handler and add the following code:
type=note
Note: Use the try and catch method if you want to create more than one command holder for a form. This will catch and ignore the exception when the second attempt to create another commandholder fails.
To write code in Visual Basic
Dim ch As C1.Win.C1Command.C1CommandHolder
ch = C1CommandHolder.CreateCommandHolder(Me)
To write code in C#
C1CommandHolder ch = C1CommandHolder.CreateCommandHolder(this);
Create and set up a copy command, then use the Click event to copy the command when you click on the menu item. Also set up the query handler for the commands so their command is kept up to date automatically by c1command. Add the following code within the form load event handler:
To write code in Visual Basic
'Create and set up the Copy command
Dim cmdCopy As C1Command = ch.CreateCommand()
cmdCopy.Text = "&Copy"
AddHandler cmdCopy.Click, AddressOf clickCopy
AddHandler cmdCopy.CommandStateQuery, AddressOf queryCopy
To write code in C#
// Create and set up the Copy command
C1Command cmdCopy = ch.CreateCommand();
cmdCopy.Text = "&Copy";
cmdCopy.Click += new C1.Win.C1Command.ClickEventHandler(clickCopy);
cmdCopy.CommandStateQuery += new C1.Win.C1Command.CommandStateQueryEventHandler(queryCopy);
Create a context menu to hold the copy command, then assign the context menu to the text box control. In order to create a context menu you must create a command in the C1CommandHolder and assign it to the context menu. Use the C1CommandHolder.CreateCommand and C1CommandHolder.SetC1ContextMenu methods from the C1CommandHolder class. Add the following code within the Form_Load event handler:
To write code in Visual Basic
Dim cm As C1ContextMenu = Ctype(ch.CreateCommand(GetType(C1ContextMenu)), C1ContextMenu)
'Fill it with the links to the commands
cm.CommandLinks.Add(New C1CommandLink (cmdCopy))
ch.SetC1ContextMenu(TextBox1, cm)
To write code in C#
C1ContextMenu cm = ch.CreateCommand(typeof(C1ContextMenu)) as C1ContextMenu;
// Fill it with the links to the commands
cm.CommandLinks.Add(new C1CommandLink(cmdCopy));
ch.SetC1ContextMenu(textBox1, cm);
Invoke the clickCopy method to handle the copy command action. Use the queryCopy method to provide the current state of the copy command. When you click the copy command from the context menu, the current text will be copied into the textbox. You can achieve this by using the following code:
To write code in Visual Basic
Private Sub clickCopy(ByVal sender As Object, ByVal e As C1.Win.C1Command.ClickEventArgs)
Me.textBox1.Copy()
End Sub
'provides the current state of the copy command
Private Sub queryCopy(ByVal sender As Object, ByVal e As C1.Win.C1Command.CommandStateQueryEventArgs)
e.Enabled = Me.textBox1.SelectionLength > 0
End Sub
To write code in C#
private void clickCopy(object sender, C1.Win.C1Command.ClickEventArgs e)
{
this.textBox1.Copy();
}
//provides the current state of the copy command
private void queryCopy(object sender, C1.Win.C1Command.CommandStateQueryEventArgs e)
{
e.Enabled = this.textBox1.SelectionLength > 0;
}
Save and run the application. Enter some text into the text box, then right-click the text to make the context menu appear. The following image shows how the context menu appears next to the text box control at run time: