locked
How to dynamically remove textbox in a stackpanel RRS feed

  • Question

  • Say, I have added 3 Stack Panel each has a textbox in it with below code.

    The problem:

    1) How to get the index of the textBox that I touch/Click in the textbox in any of the StackPanel ?

    2) How to remove both the StackPanel and textBox in the Parent StackPanel3 ?

    <StackPanel x:Name="StackPanel3" Orientation="Horizontal" Margin="20,10,0,0" HorizontalAlignment="Left" Height="130" VerticalAlignment="Top" Width="1235" Grid.Row="1"/>

    private void btnAddNewLine_Click(object sender, RoutedEventArgs e)
      {
         Counter++;
                
         TextBox mytxtBox = new TextBox();  
         mytxtBox.Name = "txtName" + Counter.ToString() ;
         mytxtBox.Height = 60;
         mytxtBox.Width = 1200;
         mytxtBox.Margin = new thickness(8,8,8,8); 
         mytxtBox.FontSize = 26;


         StackPanel Sp = new StackPanel();
         Sp.Name = "SP" + Counter.ToString();
         Sp.Orientation = Orientation.Horizontal;
         Sp.Width = 1220;

         Sp.Children.Add(mytxtBox);
       
         StackPanel3.Children.Add(Sp);

      }

    Thanks

    Friday, January 10, 2014 7:46 AM

Answers

  • You could hook the StackPanels to a Tapped event handler on the page, when they're created:

    // ...
    Sp = new StackPanel();
    Sp.Tapped += Sp_Tapped;
    Sp.Orientation = Orientation.Horizontal;
    // ...

    And then remove the tapped panel through that event handler:

    private void Sp_Tapped(object sender, TappedRoutedEventArgs e)
    {
        StackPanel Sp = sender as StackPanel;
        if (Sp != null)
        {
            StackPanel3.Children.Remove(Sp);
        }
    }

    • Marked as answer by FireDance Sunday, January 12, 2014 10:09 AM
    Friday, January 10, 2014 12:30 PM

All replies

  • Hi,

    While removing you can just remove the StackPanel inside StackPanel3 since TextBox is its children it will also be removed automatically.

    var stackPanels = StackPanel3.Children.OfType<StackPanel>();
    foreach (var stackPanel in stackPanels) StackPanel3.Children.Remove(stackPanel);


    srithar

    Friday, January 10, 2014 10:22 AM
  • Hi

    I am not removing all the stackPanels inside the main-stackpanel.

    Say, I have 3 newly added stackpanels into the Main StackPanel, if user click or tap one stackpanel, say stackPanel_1, I want to remove this stackpanel_1.

    How to do this?

    Thanks

    Friday, January 10, 2014 12:05 PM
  • You could hook the StackPanels to a Tapped event handler on the page, when they're created:

    // ...
    Sp = new StackPanel();
    Sp.Tapped += Sp_Tapped;
    Sp.Orientation = Orientation.Horizontal;
    // ...

    And then remove the tapped panel through that event handler:

    private void Sp_Tapped(object sender, TappedRoutedEventArgs e)
    {
        StackPanel Sp = sender as StackPanel;
        if (Sp != null)
        {
            StackPanel3.Children.Remove(Sp);
        }
    }

    • Marked as answer by FireDance Sunday, January 12, 2014 10:09 AM
    Friday, January 10, 2014 12:30 PM
  • Thanks. It works.
    Sunday, January 12, 2014 10:09 AM