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
  1. Home
  2. General Programming
  3. C#
  4. How to convert a Control into a ToolStripButton

How to convert a Control into a ToolStripButton

Scheduled Pinned Locked Moved C#
helptutorialquestion
4 Posts 2 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.
  • A Offline
    A Offline
    ArjenGroeneveld
    wrote on last edited by
    #1

    Hello, I have a routine that loops through all the controls on my form. The purpose is to set the Edit Mode or Read Only mode. To find textboxes and buttons inside a TabPage or GroupBox the routine runs recursive. To convert a Windows.Forms.Control in to a Texbox I validate and use .. if (c is TextBox) { ToggleTextBox((c as TextBox), flgEditMode); } So Far So Good. If I do the same for a ToolStripButton I get the message: Cannot convert type 'System.Windows.Forms.Control' to 'System.Windows.Forms.ToolStripButton' Please can you help me with a work around? Kind regards Arjen

    M 1 Reply Last reply
    0
    • A ArjenGroeneveld

      Hello, I have a routine that loops through all the controls on my form. The purpose is to set the Edit Mode or Read Only mode. To find textboxes and buttons inside a TabPage or GroupBox the routine runs recursive. To convert a Windows.Forms.Control in to a Texbox I validate and use .. if (c is TextBox) { ToggleTextBox((c as TextBox), flgEditMode); } So Far So Good. If I do the same for a ToolStripButton I get the message: Cannot convert type 'System.Windows.Forms.Control' to 'System.Windows.Forms.ToolStripButton' Please can you help me with a work around? Kind regards Arjen

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      Hello, The ToolStripButton class doesn't Inherit from Control. Inheritance Hierarchy: System.Object System.MarshalByRefObject System.ComponentModel.Component System.Windows.Forms.ToolStripItem System.Windows.Forms.ToolStripButton So you have to look for the ToolStrip class (which inherits from Control) an than iterate over the Items property. Like this:

      foreach (Control c in this.Controls)
      {
      if (c is ToolStrip)
      {
      ToolStrip ts = c as ToolStrip;
      foreach (ToolStripItem tsi in ts.Items)
      {
      ToolStripButton tsb = tsi as ToolStripButton;
      if(tsb != null)
      {
      ...
      }
      }
      }
      }

      Hope it helps!

      All the best, Martin

      A 1 Reply Last reply
      0
      • M Martin 0

        Hello, The ToolStripButton class doesn't Inherit from Control. Inheritance Hierarchy: System.Object System.MarshalByRefObject System.ComponentModel.Component System.Windows.Forms.ToolStripItem System.Windows.Forms.ToolStripButton So you have to look for the ToolStrip class (which inherits from Control) an than iterate over the Items property. Like this:

        foreach (Control c in this.Controls)
        {
        if (c is ToolStrip)
        {
        ToolStrip ts = c as ToolStrip;
        foreach (ToolStripItem tsi in ts.Items)
        {
        ToolStripButton tsb = tsi as ToolStripButton;
        if(tsb != null)
        {
        ...
        }
        }
        }
        }

        Hope it helps!

        All the best, Martin

        A Offline
        A Offline
        ArjenGroeneveld
        wrote on last edited by
        #3

        Dear Martin, Thank you for your quick reply and solution! With kind regards, Arjen

        M 1 Reply Last reply
        0
        • A ArjenGroeneveld

          Dear Martin, Thank you for your quick reply and solution! With kind regards, Arjen

          M Offline
          M Offline
          Martin 0
          wrote on last edited by
          #4

          ArjenGroeneveld wrote:

          Thank you for your quick reply and solution!

          Glad I could help!

          All the best, Martin

          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