Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a list of borders in WPf when each one has a function event.
But when I run the event function I do not know which number of border i chose from the list
how can i know the index of the border event that occurred?


C#
//the bord list
 
        private  List<Border> bord;

 private Canvas piCanv;
 private void AddItem_Click(object sender, RoutedEventArgs e)
        {

            Grid gridborder = new Grid()
            {
                Width = 80,
                Height = 150

            };


             piCanv = new Canvas()
            {
                ToolTip = "Click here to add Image",
                Width = 75,
                Height = 120,
                Margin = new Thickness(1, 40, 1, 1),
                Background = new SolidColorBrush(Colors.LightCyan),

            };

            

            Label price = new Label()
            {
                Content =proud.ID+ "Price:"
            };
            TextBox priceEdit = new TextBox()
            {
                Width = 30,
                Height = 25,
                Margin = new Thickness(30, 1, 10, 130)
             
            };

            bord = new List<Border>();
             bord.Add(new Border()
            {
                Width = 100,
                Height = 200,
                CornerRadius = new CornerRadius(15),
                BorderThickness = new Thickness(5, 10, 15, 20),
                Margin = new Thickness(5, 5, 5, 5),
                BorderBrush = Brushes.SlateBlue,
                Child = gridborder,
                ToolTip="border"


            });


//the event 
            bord[bord.Count-1].MouseRightButtonDown += RemoveItem_Click;

            piCanv.MouseLeftButtonDown += piCanv_MouseLeftButtonDown;

            gridborder.Children.Add(piCanv);
            gridborder.Children.Add(price);
            gridborder.Children.Add(priceEdit);
            Warp.Children.Add(bord[bord.Count-1]);


        }

//the event function

private void RemoveItem_Click(object sender, RoutedEventArgs e)
      {
         
              bord = sender as List<Border>;

// here i want to know the index of border that i chose

             Warp.Children.Remove(bord[?]);


     




      }
Posted
Updated 11-Dec-13 4:09am
v3
Comments
Karthik_Mahalingam 11-Dec-13 9:55am    
add more info to your question..

what is border ?
[no name] 11-Dec-13 10:10am    
border in wpf

To gain the index position of a item in a list you need to look at the following

C#
 public class Test 
 {
  public int ID {get;set;}
  public string Name {get;set;}
 }


 List<test> SomeData = new List<test>();

 SomeData.Add(new Test {ID = 1, Name="Simon" });
 SomeData.Add(new Test {ID = 2, Name="Simon" });
 SomeData.Add(new Test {ID = 3, Name="Simon" });

 //Manually make the data that I am searching for
 //suitable to show how to find the index
 Test FindThis = new Test() {ID=2, Name="Simon" };

 int index = SomeData.IndexOf(FindThis);
</test></test>


For further reading List(T).IndexOf[^]
 
Share this answer
 
Comments
[no name] 11-Dec-13 12:56pm    
you did not answer my question
i want to know which index list border operate the event function
for some reason it is null i don't know why
Simon_Whale 11-Dec-13 14:03pm    
What control is removeitem_click attached too?
Simon_Whale 11-Dec-13 14:17pm    
I think your cast is incorrect as the "as" list<border> will return null if the cast fails
Interesting. First: it looks like every time you run AddItem_Click() you are resetting your bord list back to zero. I think you'd want to check if it's null if what you really want to do is 'Add' to the list.

second: Have you used the debugger to check what the sender is? I would expect sender to be the actual list item (Border), since that is what you attached the mouse event to.

several problem with the RemoveItem_Click. you are reusing 'bord' and wiping out what ever was in it before this is pore practice.
I also expect the Sender should really be the Border you are wanting to remove from the list.

C#
private void RemoveItem_Click(object sender, RoutedEventArgs e)
      {
              bord = sender as List<Border>;

// here i want to know the index of border that i chose

             Warp.Children.Remove(bord[?]);

      }


So replace
bord = sender as List<Border>;
with

C#
Border item = sender as Border;


then

Warp.Children.Remove(item);

should work. you get null because you are trying to cast something (Border) to something that it isn't (List<border>)
 
Share this answer
 
v2

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