none
Remove a specific line from canvas

    Question

  • Hi,

    I have a canvas to which I am dynamically adding different controls like rectangle and many lines.How can I remove  a specific line.

    I tried this but it doesnt work.

    On button click I have a timer which adds a line which shows the current time. I need to draw a new line every one minute and remove the previously drawn line which shows current time. In addition to this line there are many other lines in the canvas.

    DispatcherTimer currentTimer = new DispatcherTimer();
                currentTimer.Interval = TimeSpan.FromSeconds(60);
                currentTimer.Tick += currentTimer_Tick;
                currentTimer.Start();

    private void currentTimer_Tick(object sender, EventArgs e)
            {
                try
                {
                    Line currentTime = new Line();
                    currentTime.Name = "myCanvas";
                    System.Windows.Controls.Canvas.SetLeft(currentTime, 30);              
                    System.Windows.Controls.Canvas.SetTop(currentTime, 50);
                    currentTime.X1 = 32;
                    currentTime.X2 = 50;
                    currentTime.Y1 = currentTime.Y2 = 0;
                    currentTime.Width = 50;
                    currentTime.Height = 0.3;               
                    currentTime.Stroke = new SolidColorBrush(Colors.Red);
                    if (canvas1.Children.Contains(currentTime))   //here it always says false
                        canvas1.Children.RemoveAt(currentTime);
                    canvas1.Children.Add(currentTime);               
                }
                catch(Exception ex)  //Exception:Incorrect parameter
                {
                
                }
            }

    How can I remove the currentline and then add a new one to show current time.  Please help.

    Tuesday, April 09, 2013 3:41 PM

Answers

  • if (canvas1.Children.Contains(currentTime))   //here it always says false

    Of course currentTime can't be found in canvas1.Children. It is a new instance that hasn't been added to canvas1 yet.

    You have a number of options:

    • Iterate through the children list and search for the object you want to remove.
    • When you add a Line, save a reference that you can use later with Children.Remove().
    • Just keep using the same Line and reposition it.

    Richard Woo
    Tuesday, April 09, 2013 8:17 PM

All replies

  • if (canvas1.Children.Contains(currentTime))   //here it always says false

    Of course currentTime can't be found in canvas1.Children. It is a new instance that hasn't been added to canvas1 yet.

    You have a number of options:

    • Iterate through the children list and search for the object you want to remove.
    • When you add a Line, save a reference that you can use later with Children.Remove().
    • Just keep using the same Line and reposition it.

    Richard Woo
    Tuesday, April 09, 2013 8:17 PM
  • Thanks a lot Richard for the reply and the various options.

    I tried the last option wherein I have a Line object which is global and then on each tick of timer I change the position.

    Wednesday, April 10, 2013 3:14 AM