work with buttons

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deeas
    New Member
    • Feb 2009
    • 18

    work with buttons

    please how i can handle button corresponding to integer variable like i
    ie: when i=1 i want to enable button1 and when i=2 i want t enable button 2 and so on
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    One way of handling that is to make i a private variable and have only one method to change it. whenever the method is called, attach an event handler to this method. you may pass variables across on what the state of the button should be or let the UI handle that.

    Comment

    • deeas
      New Member
      • Feb 2009
      • 18

      #3
      i think my question wasn't clear enough
      i want to write thing like:
      for i=0 i<10 i++
      button(i).text= number(i)
      is it possible
      thanks

      Comment

      • Shashi Sadasivan
        Recognized Expert Top Contributor
        • Aug 2007
        • 1435

        #4
        the idea is still the same,
        you want to change the propert of the button (text is a property)

        if you run the code above, the application will freeze (try it for a large number so that the for loop takes time) and the text on the button will show up as the last change made to it.

        You will need to run the for loop in a seperate thread, and if you create an even when ii changes, use that to trigger the property of the UI

        Comment

        • deeas
          New Member
          • Feb 2009
          • 18

          #5
          thanks i will try this

          Comment

          • vekipeki
            Recognized Expert New Member
            • Nov 2007
            • 229

            #6
            You could do it by adding references to all you buttons to an array.

            1. Add a private field which will hold references:
            Code:
            // add a private field
            private readonly Button[] buttons;
            2. Then fill the array inside your constructor:
            Code:
            // then add this after InitializeComponent()
            buttons = new Button[] {
                   button1,
                   button2,
                   ...
                   (add all buttons here)
               };

            Comment

            • deeas
              New Member
              • Feb 2009
              • 18

              #7
              this is what i need,thank you

              Comment

              Working...