Click here to Skip to main content
15,843,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Let me try this with a little more detail. Thank you for your help so far Christian, but I believe I'm leading you down the wrong road. My apologies.

Some background info, this is a WPF application that has a WrapPanel to hold several user controls that are added to it at runtime. The panel starts empty. There is a list of icons representing various shapes the user can click on. When an icon is clicked, it addes the shape (actually a MoveBlock user control representing the shape) to the WrapPanel. The user is allowed to have more than one of each shape and can add/remove/reorder the shapes to create a pattern.

Right now I'm testing my implementation. I have a test button on the main window with the following code:
Controls.MoveBlock newMove = new Controls.MoveBlock();
newMove.Name = "Move1";
newMove.Width = 96;
newMove.Height = 96;
newMove.Highlight = Controls.MoveBlockHighlight.Selected;
... other settings
MovesWrapPanel.Children.Add(newMove);
newMove = new Controls.MoveBlock();
newMove.Name = "Move2";
... other settings
MovesWrapPanel.Children.Add(newMove);


That works fine. I can see the controls in the WrapPanel and they act like they are suppose to. However, I cannot remove the control using the following code:
MovesWrapPanel.Children.Remove((Controls.MoveBlock)MovesWrapPanel.FindName("Move1"));

But the code will work if I created the control using the designer and named it "Move1".

In an effort to figure out what's going on, I have a MessagBox showing the name of the control when it is clicked.
MessageBox.Show("Name is " + this.Name);

Clicking Move1 shows "Name is Move1" and clicking Move2 shows "Name is Move2".

Christian, you mentioned that it could be using another property, specifically the ID. That sounds like a great explanation, but I can't find any other property that has a value that matches the MoveBlock.Name value.

I'm trying to delete specific children of a WrapPanel. Each child is a user control I created, MoveBlock. They are added and removed at runtime by the user.

[UPDATED] I'm looking for the ability to remove a specific item that is selected by the user. There may be 10 items and the user wants to remove the 4th one. Also, the user has the ability to reorder the controls. It may start of 123456, but be 4631 (removed two) or 53271 (added another) by the time the user is done. I then need to go through the list in the order displayed to make a file to create the part shown.

I'm using:

MovesWrapPanel.Children.Remove((Controls.MoveBlock)MovesWrapPanel.FindName("Move1"));


This works fine as long as I named the control "Move1" when I added using the designer.

However, it doesn't if I don't give the control a name and change it to "Move1" by setting the Name property of the control at runtime before running the line above:

private void UserControl_MouseUp(object sender, MouseButtonEventArgs e)
    {
      this.Name = "Move1";
    }


I know this is a basic question, but I have had no luck finding the answer.

Thanks,
Posted
Updated 3-Dec-09 10:06am
v3

Instead of using findname, you can iterate over the children and use the 'as' keyword, as in

MoveBlock mv = child as MoveBlock;

at this point, if mv is not null, it was a moveblock, so you can delete it.
 
Share this answer
 
I suspect that it's the ID property and not the name property that is being searched for, even though it's called FindName. Did you try setting the ID ? If not, the next question is, if you iterate over them using the code I gave you, what name/ID DOES it have ?


wrote:
private void UserControl_MouseUp(object sender, MouseButtonEventArgs e) { this.Name = "Move1"; }


Here's a thought - why are you doing this on the server, and what is the order of operations between this and the other code ?
 
Share this answer
 
v2
Oh - sorry. In that case, the click event on the object you want to remove, is passed the sender property. You can cast this to your class type ( it is the control that was clicked ) and so you can remove it from there, the name is then irrelevant. For some bizarre reason, I thought you were asking an ASP.NET question.
 
Share this answer
 

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