Click here to Skip to main content
15,392,679 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello Masterminds,

I am creating an online test application, in which i am generating 100 buttons approx. at run time on form load.
C#
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);
                //flowLayoutPanel1.ScrollControlIntoView(button);
            }
        }

and on this button click i am changing the background color
C#
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 form Named "Next". Now my problem is if i am currntly in question "1." and i press button next i want to change background image of button which text is "2".

Need your valueable guidance to solve this problem.


Thanks...

What I have tried:

I am out of mind, don't know how to access run time generated buttons from another button control
Posted
Updated 16-Jun-20 2:38am
Comments
VR Karthikeyan 11-Dec-16 23:26pm
   
can you share a screenshot of your form?
KManishS 11-Dec-16 23:46pm
   
https://photos.google.com/share/AF1QipOQ-xh3hBypJ4XZf-rmJIcoCCbiKXoawQQTuIVn2_kNQU5x0RNsh1yjMWomCjKXug?key=ZFFwcFZYVURMQks1MEhJV3pQWnp0UXFHc1NYOENR

1 solution

Hi Manish,

Basically you need the following two steps:

1) Keep track of the last button index.
2) On 'next' click get the next button of the last button and process that (with background image or whatever).

1) Keeping track of the last button index is easy as they have names like Question1, Question2... etc.

You will need a global variable to keep track of that:
int LastButtonIndex;
Then change the ButtonClickOneEvent as follows. It is just taking a substring to obtain the button index.
C#
void ButtonClickOneEvent(object sender, EventArgs e)
{
    Button button = sender as Button;
    button.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.button_Orange));
    lblQuestionNo.Text = ((int)button.Tag).ToString() + ".";
    LastButtonIndex = Convert.ToInt16(button.Name.Substring(8)); // Added this line
    btnNext.Focus();
}
2) On 'next' click get the next button and process as required:
C#
private void btnNext_Click(object sender, EventArgs e)
{
    //if (LastButtonIndex + 1 > clsGlobalVars.gnTotalQuestion) return;    // Checking if it reached the last button.
    Control[] NextQuestion;
    string ButtonName = "Question" + (LastButtonIndex + 1);
    NextQuestion = Controls.Find(ButtonName, false);

    if (NextQuestion.GetUpperBound(0) != -1)    // Checking if the button was found.
        (NextQuestion[0] as Button).BackColor = Color.Red;  // Change this line according to your need.
}
   
v3
Comments
KManishS 12-Dec-16 0:55am
   
(NextQuestion[0] as Button).BackColor = Color.Red; // Change this line according to your need.

error in the above line, index was outside the bounds of array
Mehedi Shams 12-Dec-16 1:08am
   
Sorry Manish, please check the updated code for btnNext_click() event. It checks if the button was found or not.

Did the code work at all for your buttons or it happened at a certain point (like the last button click)? I tested the code and it worked fine. So a little confused.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900