ICreate.Controls.Bars Send comments on this topic.
Walkthrough - CommandButton with sub button and context menu

Glossary Item Box

This walkthrough describes how to create a CommandButton with a subordinate button. A context menu is displayed when the sub button is clicked. The context menu is used to select from a number options. Pressing the main section of the CommandButton is equivalent to clicking on the sub button and selecting the default menu option, which in this case is the New document item. The CommandButton and menu are shown below.

  1. Add a ContextMenuStrip to the Form.
  2. Add the items displayed in the image above to the menu.
  3. Drag a CommandButton onto the Form.
  4. Set the Name of the CommandButton to buttonNew.
  5. Set the Colour properties of the CommandButton as shown below.

  6. Set  the Menu property of the CommandButton to the ContextMenuStrip.
  7. Double click the CommandButton to add a Click event handler.
  8. Implement the CommandButton and ToolStripMenuItem Click event handlers as shown below.
    Visual Basic

    Private Sub CreateNewDocument()

    MessageBox.Show("Create new document")

    End Sub

     

    Private Sub buttonNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _

    Handles buttonNew.Click

    CreateNewDocument()

    End Sub

     

    Private Sub MenuItemDocument_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _

    Handles MenuItemDocument.Click

    CreateNewDocument()

    End Sub

     

    Private Sub MenuItemDatabase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _

    Handles MenuItemDatabase.Click

    MessageBox.Show("Create new database")

    End Sub

     

    Private Sub MenuItemImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _

    Handles MenuItemImage.Click

    MessageBox.Show("Create new image")

    End Sub

     

    Private Sub MenuItemFax_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _

    Handles MenuItemFax.Click

    MessageBox.Show("Create new fax")

    End Sub

     

    Private Sub MenuItemEMail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _

    Handles MenuItemEMail.Click

    MessageBox.Show("Create new email")

    End Sub

     

    Private Sub MenuItemSMS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _

    Handles MenuItemSMS.Click

    MessageBox.Show("Create new SMS")

    End Sub

     

    Private Sub MenuItemMMS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _

    Handles MenuItemMMS.Click

    MessageBox.Show("Create new MMS")

    End Sub

    C#

    private void CreateNewDocument()

    {

    MessageBox.Show("Create new document.");

    }

     

    private void buttonNew_Click(object sender, EventArgs e)

    {

    CreateNewDocument();

    }

     

    private void menuItemDocument_Click(object sender, EventArgs e)

    {

    CreateNewDocument();

    }

     

    private void menuItemDatabase_Click(object sender, EventArgs e)

    {

    MessageBox.Show("Create new database.");

    }

     

    private void menuItemImage_Click(object sender, EventArgs e)

    {

    MessageBox.Show("Create new image.");

    }

     

    private void menuItemFax_Click(object sender, EventArgs e)

    {

    MessageBox.Show("Create new fax.");

    }

     

    private void menuItemEmail_Click(object sender, EventArgs e)

    {

    MessageBox.Show("Create new email.");

    }

     

    private void menuItemSMS_Click(object sender, EventArgs e)

    {

    MessageBox.Show("Create new SMS.");

    }

     

    private void menuItemMMS_Click(object sender, EventArgs e)

    {

    MessageBox.Show("Create new MMS.");

    }

  9. This walkthrough is complete and the application can be executed.