views:

61

answers:

1

Hello my kind sirs. (lol)

I have a panel on Form1. I'm going to add several instances of my custom usercontrol MovieItem to that panel.

To do this I'm going:

panel1.Controls.Add(objMovieItem);

Now, within the MovieItem.cs code I'm adding the .Click events of everything inside the UserControl so that whenever a user clicks ANYWHERE inside my UserControl, it's background color changes.

Here's the problem:

I have about 10 instances of the UserControl on Form1 (inside of the Panel1 of course). How can I put the BackColor of the selected one to lightblue, and all of the others back to the default color?

Here's my code:

private void SearchMovie()
{
     //All this does is create an instance of the UserControl and add it to Panel1.
     MovieItem NewMovie = new MovieItem();
     NewMovie.SearchMovie(txtSearch.Text);
     panel1.Controls.Add(NewMovie);
}

Now, inside of my user controls code:

private void MovieItem_Click(object sender, EventArgs e)
    {            
        MainSearchForm MainFormObject = new MainSearchForm();

        foreach (Control y in MainFormObject.Controls["panel1"].Controls)
        {
            if (y is UserControl)
            {
                if (y is MovieItem)
                {                        
                    y.BackColor = Color.White;
                }                    
            }
        }

        this.BackColor = Color.LightBlue;
    }

Here's what happens. The click events are 100% OK. Works as intended. When I click on an object of my UserControl it changes color correctly. But the other don't change back to default. What am I doing wrong.

Thanks SO.

+1  A: 

In the handler, you create a new MainSearchForm object, and then set its controls to white, so you aren't affecting the form you are displaying.

Use this.Parent to find the parent panel, or use a static variable to hold a reference to your mainForm (or etc).

as in:

private void MovieItem_Click(object sender, EventArgs e)
{
    foreach (Control y in this.Parent.Controls)
    {
        if (y is MovieItem && y != this)
            y.BackColor = Color.White;
    }
    this.BackColor = Color.LightBlue;
}
Jason Williams
I think you're wrong here. (Correct me if I am) If it works at changing the color blue using the NEW keyword, should it work pretty much the same for my problem here?
Sergio Tapia
The new keyword creates a new object - it is not the form that you are running "in", it is a completely different one. The loop then attempts to sets values in the new object, and when the event handler exits, the new object is thrown away. See my edit for an example that should work.
Jason Williams
(P.S. You set the blue on this.BackColor, not on the object that you created with new, which is why the blue part works)
Jason Williams
Jason just to clarify, when using the this.Parent, I'm checking the control of whever this usercontrol is instanced parents?
Sergio Tapia
Yes. "this" is the current object (in this case the MovieItem that was clicked). Hence, "this.Parent" finds the panel that holds your MovieItem controls, and then you can foreach through its "child" list (Controls) to find all the other MovieItem controls at the same level as "this" in the family tree.
Jason Williams