1

I am creating an online test application, in which I am generating approximately 100 buttons at run time on form load. Here is the piece of code:w

private void addQuestion_Reviewbutton()
        {
            for (int i = 1; i <= clsGlobalVars.gnTotalQuestion; i++)
            {
                Button button = new Button();
                button.Location = new Point(160, 30 * i + 10);
                button.Click += new EventHandler(ButtonClickOneEvent);                
                button.Tag = i;                
                button.Name = "Question" + i;
                button.Text = i.ToString();
                button.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.button));
                button.BackgroundImageLayout = ImageLayout.Stretch;//.Zoom;
                button.FlatAppearance.BorderSize = 0;
                button.Size = new System.Drawing.Size(47, 41);
                button.BackColor = Color.Transparent;
                button.FlatStyle = FlatStyle.Flat;
                button.Font = new System.Drawing.Font("Segoe UI Semibold", 12);
                button.ForeColor = Color.White;
                button.Cursor = Cursors.Hand;
                flowLayoutPanel1.Controls.Add(button);
                
            }
        }

and on this button click, I am changing the background-color.

void ButtonClickOneEvent(object sender, EventArgs e)
        {
            Button button = sender as Button;
            //button.BackColor = Color.Yellow;
            button.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.button_Orange));
            lblQuestionNo.Text = ((int)button.Tag).ToString()+".";
            btnNext.Focus();
            
        }

I have a button on the form Named "Next". Now my problem is that if I am currently in question "1." and I press the button next I want to change the background image of the button whose text is "2".

6
  • 1
    What technology are you creating this in? Is this webforms? Commented Jun 30, 2020 at 11:23
  • 2
    So if current Button has Tag == 1 you want to find the Button with Tag == 2? Why not just loop over these buttons? Or query with a help of Linq? var nextButton = flowLayoutPanel1.Controls.OfType<Button>().FirstOrDefault(btn => btn.Tag == currentButton.Tag + 1); Commented Jun 30, 2020 at 11:25
  • 1
    You could pass the next Button Tag as an CommandArgument to save you the trouble of having to convert back to int to incrememt. Commented Jun 30, 2020 at 11:26
  • 1
    You should not be using WebForms for new projects in 2021. It's been obsolete for 13 years already. Yikes. Commented Sep 2, 2021 at 5:18
  • 1
    "I need your valuable guidance to solve this problem." - my guidance is for you to stop immediately and start-over in ASP.NET Core, or at least ASP.NET MVC (for .NET Framework), otherwise you'll have sunk more and more time into a web-framework that does not work well with modern web-browsers. For example, every "event" handled on the server requires a form POST, which breaks your user's browsers' Back button, which is a really crappy UX (so if the user submits answers for a dozen questions (each requiring their own POST) and then accidentally navigate back they've lost everything. Commented Sep 2, 2021 at 5:20

1 Answer 1

1

Yo check this !

tldr; the guidance are

  1. Is the click event go to ButtonClickOneEvent ?

  2. How to call other button

  3. ...

  4. profit?

      protected void Page_Load(object sender, EventArgs e)
     {
        addQuestion_Reviewbutton();
     }
    
      void ButtonClickOneEvent(object sender, EventArgs e)
     {
         Button button = sender as Button;
    
         var currentbtn_id = button.ID; 
         int nextid =  Convert.ToInt32( currentbtn_id.Substring("Question".Length)) + 1;
        var nextBtn = flowLayoutPanel1.FindControl("Question"+ nextid.ToString() );
         (nextBtn as Button).BackColor = Color.Red;
    
     }
    

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.