Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. On button click-context menu strip

On button click-context menu strip

Scheduled Pinned Locked Moved C#
graphics
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    Udayaraju
    wrote on last edited by
    #1

    I have 30 buttons and I am trying to implement context menu on each button click by using following method.

    void OnButtonClick(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            btn.ContextMenuStrip.Show(btn, new System.Drawing.Point(0, btn.Height));
        }
    

    I have 2 context menu strip items, Add text Label and Add colour

    private void addTextLabelToolStripMenuItem_Click(object sender, EventArgs e)
    {
    foreach (Control item in panel2.Controls)
    {
    Button btn = (Button)item;
    if (string.IsNullOrEmpty(btn.Text.Trim()))
    {
    frmAddText form = new frmAddText();
    form.ShowDialog();

                    if (frmAddText.IsTextMod)
                    {
                        return;
                    }
                    btn.Text = form.TextInfo;
                    btn.Tag = 0;
                }				} }
    

    Wen i try to do this it is implementing on last button itself. Here Ia m returning Here foreach is not acceptable.

    private void addColourToolStripMenuItem_Click(object sender, EventArgs e)
    {
    colorDialog1.ShowDialog();
    DialogResult dResult = colorDialog1.ShowDialog();
    foreach (Control item in panel2.Controls)
    {
    Button btn = (Button)item;
    string strBtnName = btn.Name;
    if (string.Equals(btn.Name, strBtnName, StringComparison.OrdinalIgnoreCase))
    {
    if (dResult == DialogResult.OK)
    btn.BackColor = colorDialog1.Color;
    } } }

    It is applying color to all buttos. But it should apply for only specific clicked button. Here foreach is not acceptable.

    A D 2 Replies Last reply
    0
    • U Udayaraju

      I have 30 buttons and I am trying to implement context menu on each button click by using following method.

      void OnButtonClick(object sender, EventArgs e)
          {
              Button btn = (Button)sender;
              btn.ContextMenuStrip.Show(btn, new System.Drawing.Point(0, btn.Height));
          }
      

      I have 2 context menu strip items, Add text Label and Add colour

      private void addTextLabelToolStripMenuItem_Click(object sender, EventArgs e)
      {
      foreach (Control item in panel2.Controls)
      {
      Button btn = (Button)item;
      if (string.IsNullOrEmpty(btn.Text.Trim()))
      {
      frmAddText form = new frmAddText();
      form.ShowDialog();

                      if (frmAddText.IsTextMod)
                      {
                          return;
                      }
                      btn.Text = form.TextInfo;
                      btn.Tag = 0;
                  }				} }
      

      Wen i try to do this it is implementing on last button itself. Here Ia m returning Here foreach is not acceptable.

      private void addColourToolStripMenuItem_Click(object sender, EventArgs e)
      {
      colorDialog1.ShowDialog();
      DialogResult dResult = colorDialog1.ShowDialog();
      foreach (Control item in panel2.Controls)
      {
      Button btn = (Button)item;
      string strBtnName = btn.Name;
      if (string.Equals(btn.Name, strBtnName, StringComparison.OrdinalIgnoreCase))
      {
      if (dResult == DialogResult.OK)
      btn.BackColor = colorDialog1.Color;
      } } }

      It is applying color to all buttos. But it should apply for only specific clicked button. Here foreach is not acceptable.

      A Offline
      A Offline
      ABitSmart
      wrote on last edited by
      #2

      Udayaraju wrote:

      string strBtnName = btn.Name; if (string.Equals(btn.Name, strBtnName, StringComparison.OrdinalIgnoreCase)) {

      this condition will always be True and all button will be colored. Edit - you need to refactor your code. You can get the button on whom the contextmenu click happpened rather than looping through all controls.

      Button btn = (sender as ContextMenuStrip).SourceControl as Button;

      1 Reply Last reply
      0
      • U Udayaraju

        I have 30 buttons and I am trying to implement context menu on each button click by using following method.

        void OnButtonClick(object sender, EventArgs e)
            {
                Button btn = (Button)sender;
                btn.ContextMenuStrip.Show(btn, new System.Drawing.Point(0, btn.Height));
            }
        

        I have 2 context menu strip items, Add text Label and Add colour

        private void addTextLabelToolStripMenuItem_Click(object sender, EventArgs e)
        {
        foreach (Control item in panel2.Controls)
        {
        Button btn = (Button)item;
        if (string.IsNullOrEmpty(btn.Text.Trim()))
        {
        frmAddText form = new frmAddText();
        form.ShowDialog();

                        if (frmAddText.IsTextMod)
                        {
                            return;
                        }
                        btn.Text = form.TextInfo;
                        btn.Tag = 0;
                    }				} }
        

        Wen i try to do this it is implementing on last button itself. Here Ia m returning Here foreach is not acceptable.

        private void addColourToolStripMenuItem_Click(object sender, EventArgs e)
        {
        colorDialog1.ShowDialog();
        DialogResult dResult = colorDialog1.ShowDialog();
        foreach (Control item in panel2.Controls)
        {
        Button btn = (Button)item;
        string strBtnName = btn.Name;
        if (string.Equals(btn.Name, strBtnName, StringComparison.OrdinalIgnoreCase))
        {
        if (dResult == DialogResult.OK)
        btn.BackColor = colorDialog1.Color;
        } } }

        It is applying color to all buttos. But it should apply for only specific clicked button. Here foreach is not acceptable.

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        There may be a quicker way of doing it, but the code below works.

        private void testToolStripMenuItem_Click(object sender, EventArgs e)
        {
        if(sender is ToolStripMenuItem)
        {
        // Get the item that was cliked
        ToolStripMenuItem clickedItem = (ToolStripMenuItem)sender;
        // Get the ContextMenuStrip the item belongs to
        ContextMenuStrip strip = (ContextMenuStrip)clickedItem.Owner;
        if (strip.SourceControl is Button)
        {
        // Get the actual button it belongs to
        Button button = (Button)strip.SourceControl;
        button.BackColor = Color.Blue;
        }
        }
        }

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

        U 1 Reply Last reply
        0
        • D DaveyM69

          There may be a quicker way of doing it, but the code below works.

          private void testToolStripMenuItem_Click(object sender, EventArgs e)
          {
          if(sender is ToolStripMenuItem)
          {
          // Get the item that was cliked
          ToolStripMenuItem clickedItem = (ToolStripMenuItem)sender;
          // Get the ContextMenuStrip the item belongs to
          ContextMenuStrip strip = (ContextMenuStrip)clickedItem.Owner;
          if (strip.SourceControl is Button)
          {
          // Get the actual button it belongs to
          Button button = (Button)strip.SourceControl;
          button.BackColor = Color.Blue;
          }
          }
          }

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

          U Offline
          U Offline
          Udayaraju
          wrote on last edited by
          #4

          Thanq Dave Itz working

          D 1 Reply Last reply
          0
          • U Udayaraju

            Thanq Dave Itz working

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            You're welcome :-D

            Dave
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
            Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups