5 Replies - 594 Views - Last Post: 20 June 2020 - 06:00 AM Rate Topic: -----

#1 spiplc   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 4
  • Joined: 17-June 20

Button

Posted 18 June 2020 - 12:00 AM

Hello I was wondering how to change colors of Button with for loop by the number


  for (int i = 2; i < 10; i++)
            {

                Button btn = new Button();

                Button(i).BackColor = Color.Red;
               

            }


I have done this in vb a long time ago but i'm having trouble with C# please help
Is This A Good Question/Topic? 0
  • +

Replies To: Button

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7444
  • View blog
  • Posts: 25,075
  • Joined: 05-May 12

Re: Button

Posted 18 June 2020 - 04:32 AM

Did you do this in VB or VB.NET? If it was VB.NET, then it would be just the same.

Anyway, line number 4 is not doing you any good. You are not even using the variable, not is creating a new instance of a button what you want too do since you want to get a reference to an existing button, not create a new button. Try iterating through the Controls collection of the form. Check the type of the control to see if it is a button. And then filter by the Name property if you had seen the Name on your controls.
Was This Post Helpful? 0
  • +
  • -

#3 DarenR   User is offline

  • D.I.C Lover

Reputation: 705
  • View blog
  • Posts: 4,555
  • Joined: 12-January 10

Re: Button

Posted 18 June 2020 - 06:17 AM

to change the button color you will have to search for the button and then change the color unless like you coded above are changing the color on building the button
Was This Post Helpful? 0
  • +
  • -

#4 spiplc   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 4
  • Joined: 17-June 20

Re: Button

Posted 19 June 2020 - 05:16 PM

I got it to work thank you for the hint and not just telling me the answer i like to figure it out so i know how it work



     for (int i = 1; i < 4; i++)
            {

                string findbutton = "button" + i.ToString();
            Button tbx = Controls.Find(findbutton, true).FirstOrDefault() as Button;
                if (tbx.Text == findbutton)
                {
                    tbx.BackColor = Color.Green;
                
                }
                   


            }

Was This Post Helpful? 2
  • +
  • -

#5 spiplc   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 4
  • Joined: 17-June 20

Re: Button

Posted 19 June 2020 - 05:38 PM

update code


    for (int i = 1; i < 24; i++)
            {

                string findbutton = "EF" + i.ToString();
                Button but = Controls.Find(findbutton, true).FirstOrDefault() as Button;

                if(but != null)
                {
                but.BackColor = Color.Green;
                }

            }


had to add filter to make sure the button = to something or it would crash if the button didn't exist life's all good
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7444
  • View blog
  • Posts: 25,075
  • Joined: 05-May 12

Re: Button

Posted 20 June 2020 - 06:00 AM

I'm glad that you worked it out!!! Lately, people have been less willing to think for themselves, and been more pushy "gimme-the-codez". Glad that they are still folks who appreciate the joy of discovery.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1