0

I am making a form system where i need to find a panel to change it's background using string imploration. I have done something similar for a label using the code below where numberEntered is a seperate interger in the program.

Label label = Controls.Find($"Num{numberEntered}", true).OfType<Label>().FirstOrDefault();
label.Text = "Text"

How can I do something similar for a panel where where I can find a panel using a seperate variable in the name? Such as $"Panel{number}"

I have already tried this:

 Panel panel = Controls.Find($"Ans{answerNum}Panel", true).OfType<Panel>().FirstOrDefault();
 panel.BackgroundImage = Programming_Project.Properties.Resources.MutliChoiceCorrectSelected;

However it throws a NullReferenceException. Any help is much appreciated!

  • "However it throws a NullReferenceException" Then your Panel is not being found. Either the value in `answerNum` is not what you expected, or the name of the Panel in question is not what you think it is. Are you creating the panels at run-time, or dynamically? – Idle_Mind Jan 05 '21 at 13:28
  • They are already created with the designer, and I am just trying to find them, using the control find method. – Torin macdonald Jan 05 '21 at 14:25

2 Answers2

1

Try it this way please:

String ctlName = $"Ans{answerNum}Panel";
Panel panel = this.Controls.Find(ctlName, true).FirstOrDefault() as Panel;
if (panel != null) {
    panel.BackgroundImage = Programming_Project.Properties.Resources.MutliChoiceCorrectSelected;
}
else {
    MessageBox.Show("Unable to find " + ctlName);
}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
0

You can try to open Form1.Designer.cs where Form1 is your form name. Then find the panel code and modify it. That's how I tried and it worked.

It should look something like this:`

private void InitializeComponent()
    {
        this.panel1 = new System.Windows.Forms.Panel();
        this.SuspendLayout();
        // 
        // panel1
        // 
        **this.panel1.BackColor = System.Drawing.SystemColors.ActiveCaption;**
        this.panel1.Location = new System.Drawing.Point(169, 41);
        this.panel1.Name = "panel1";
        this.panel1.Size = new System.Drawing.Size(427, 190);
        this.panel1.TabIndex = 0;
        this.panel1.Paint += new 
        System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(800, 450);
        this.Controls.Add(this.panel1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);

    }
  • Thank you for the response, however i want to change them dynmaically during run time, so i'm not sure this will work – Torin macdonald Jan 05 '21 at 14:25
  • I found this, maybe it will help you https://stackoverflow.com/questions/58103658/how-to-fill-panel-background-color-based-on-dynamic-value – Daniel Rusu Jan 05 '21 at 16:59