how can i add items to the contextMenu without removing the default items?

hi,

adding context menu to my textbox is simple, but once i add that contextmenu to the textbox, the default items that used to be there when 'none' was chosen disappear. how do i add items to the contextmenu of the textbox without removing items like 'cut', 'paste', 'copy' and other options

thanks,


Answer this question

how can i add items to the contextMenu without removing the default items?

  • aayush_dabas

    Yes it's possible, because TextBox, have the methods, Cut, Copy, Paste, Delete, SelectAll and Undo, and you can call the methods from the ContexMenu.



    public class DefaultContexMenu : ContextMenu

    {

    private TextBoxBase t;

    public DefaultContexMenu(TextBoxBase textBox)

    {

    this.t = textBox;

    MenuItem mi = new MenuItem("&Undo");

    mi.Click += new EventHandler(OnUndo);

    this.MenuItems.Add(mi);

    this.MenuItems.Add("-");

    mi = new MenuItem("Cu&t");

    mi.Click += new EventHandler(OnCut);

    this.MenuItems.Add(mi);

    mi = new MenuItem("&Copy");

    mi.Click += new EventHandler(OnCopy);

    this.MenuItems.Add(mi);

    mi = new MenuItem("&Paste");

    mi.Click += new EventHandler(OnPaste);

    this.MenuItems.Add(mi);

    mi = new MenuItem("&Delete");

    mi.Click += new EventHandler(OnDelete);

    this.MenuItems.Add(mi);

    this.MenuItems.Add("-");

    mi = new MenuItem("Select &All");

    mi.Click += new EventHandler(OnSelect);

    this.MenuItems.Add(mi);

    }

    protected override void OnPopup(EventArgs e)

    {

    base.OnPopup(e);

    //Handle Here the Enabled Disabled Items.

    }

    void OnSelect(object sender, EventArgs e)

    {

    t.SelectAll();

    }

    void OnDelete(object sender, EventArgs e)

    {

    t.Clear();

    }

    void OnPaste(object sender, EventArgs e)

    {

    t.Paste();

    }

    void OnCopy(object sender, EventArgs e)

    {

    t.Copy();

    }

    void OnCut(object sender, EventArgs e)

    {

    t.Cut();

    }

    void OnUndo(object sender, EventArgs e)

    {

    t.Undo();

    }

    }

     



  • Rajat Dasgupta

    Hello again.

    You can't. I haven't seen it done, anyway.
    You have effectively replaced the default context menu.
    But it should be possible to duplicate the default items and their functionality

    Gorm Braarvig

  • v-mousah

    thanks,
  • Netferret

    anyone
  • how can i add items to the contextMenu without removing the default items?