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. C#:Problem in disabling all buttons in a form except few using a method.

C#:Problem in disabling all buttons in a form except few using a method.

Scheduled Pinned Locked Moved C#
helpcsharptutorialquestion
4 Posts 4 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.
  • K Offline
    K Offline
    kssknov
    wrote on last edited by
    #1

    hi all can any one help me how to disable all buttons in a form except few using a method. I coded to disable all buttons as: private void disable_buttons() { foreach (Control ctrl in this.Controls) { if (ctrl.GetType() == typeof(Button)) { ctrl.Enabled = false; } } } but when called from same class load event it doesnt works? pls suggest me for this problem? private void UserMaster_Load(object sender, EventArgs e) { disable_buttons(); } thank u

    Kssk

    C C L 3 Replies Last reply
    0
    • K kssknov

      hi all can any one help me how to disable all buttons in a form except few using a method. I coded to disable all buttons as: private void disable_buttons() { foreach (Control ctrl in this.Controls) { if (ctrl.GetType() == typeof(Button)) { ctrl.Enabled = false; } } } but when called from same class load event it doesnt works? pls suggest me for this problem? private void UserMaster_Load(object sender, EventArgs e) { disable_buttons(); } thank u

      Kssk

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Are you calling it before the initialize method which creates the controls and also enables them ? It should work, it will disable all buttons, you need to reenable the ones you don't want to disable.

      Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      1 Reply Last reply
      0
      • K kssknov

        hi all can any one help me how to disable all buttons in a form except few using a method. I coded to disable all buttons as: private void disable_buttons() { foreach (Control ctrl in this.Controls) { if (ctrl.GetType() == typeof(Button)) { ctrl.Enabled = false; } } } but when called from same class load event it doesnt works? pls suggest me for this problem? private void UserMaster_Load(object sender, EventArgs e) { disable_buttons(); } thank u

        Kssk

        C Offline
        C Offline
        CKnig
        wrote on last edited by
        #3

        this might have the problem that buttons inside panels or frames are not disabled (just use a recursive walker). Concerning your question: as was said before: disable all and then enable the ones you like or give every button a describing Tag (for example a groupname or whatever) and check for this Tag in you foreach-loop to only disable the ones in the given group.

        1 Reply Last reply
        0
        • K kssknov

          hi all can any one help me how to disable all buttons in a form except few using a method. I coded to disable all buttons as: private void disable_buttons() { foreach (Control ctrl in this.Controls) { if (ctrl.GetType() == typeof(Button)) { ctrl.Enabled = false; } } } but when called from same class load event it doesnt works? pls suggest me for this problem? private void UserMaster_Load(object sender, EventArgs e) { disable_buttons(); } thank u

          Kssk

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          if you replace if (ctrl.GetType() == typeof(Button)) by if (ctrl is Button) you get simpler code, that recognizes Buttons as well as controls that inherit from Button. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          Sorry for any delays in replying, I currently don't always get e-mail notifications.


          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