locked
How to add textboxes dynamically into StackPanel RRS feed

  • Question

  • I want to add textBox dynamically into the StackPanel inside a ScrollViewer.

    The problem:

    - The added textboxes dont stack ontop one another. They all line up as a straigth line horizontally.

    These what I need:

    1) My ScrollViewer Height=380, the StackPanel Height = 130

    How to increase the StackPanel height to contain more textboxes ?

    2) all added textBoxes stack up vertically ontop one another.3
    ) Each textbox has a margin
        -  this is not correct :  mytxtBox.Margin = new thickness("8,8,8,8");

    4) StackPanel can be scrolled in ScrollViewer.

     Thanks

    here the code:

    <ScrollViewer x:Name="SV2" Grid.Row="1" Margin="33,171,0,0" VerticalScrollMode="Auto" HorizontalAlignment="Left" VerticalAlignment="Top" Height="380" Width="1274" >

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

    </ScrollViewer>


     private void btnAddNewLine_Click(object sender, RoutedEventArgs e)
     {
               
        TextBox mytxtBox = new TextBox();

         Counter++;

         mytxtBox.Name = "txtName" + Counter.ToString() ;
         mytxtBox.Height = 60;
         mytxtBox.Width = 1210;

         //mytxtBox.Margin = new thickness("8,8,8,8");    

         mytxtBox.FontSize = 26;

         StackPanel3.Children.Add(mytxtBox);

      }


    • Edited by FireDance Thursday, January 9, 2014 8:22 AM new requirements
    Thursday, January 9, 2014 8:17 AM

Answers

  •  

    don't give height to stack panel just provide width

    and height& width to ScrollViewer element. only provide

    VerticalScrollBarVisibility="Auto"

     

     



    Thursday, January 9, 2014 9:58 AM

All replies

  •            

    public static float h=0;

    private void btnAddNewLine_Click(object sender, RoutedEventArgs e)
     {

        TextBox mytxtBox = new TextBox();

         Counter++;

         mytxtBox.Name = "txtName" + Counter.ToString() ;

         mytxtBox.Margin = new thickness("8,h+8,8,8");   

        mytxtBox.Height = 60;
        h=h+mytxtBox.Height ;

      mytxtBox.Width = 1210;

     

         mytxtBox.FontSize = 26;

         StackPanel3.Children.Add(mytxtBox);

      }

    I made changes directly here so please correct syntactical errors if any.

    Regards,

    pankaj

    Thursday, January 9, 2014 8:34 AM
  • Hi , I got the adding textboxes into stackpanel working.

    But I am not able to solve these problems:

    1) My ScrollViewer Height=380, the StackPanel Height = 130

         How to increase the StackPanel height to contain more textboxes ?

    4) StackPanel can be scrolled in ScrollViewer

    Thanks

    Thursday, January 9, 2014 9:10 AM
  •  

    don't give height to stack panel just provide width

    and height& width to ScrollViewer element. only provide

    VerticalScrollBarVisibility="Auto"

     

     



    Thursday, January 9, 2014 9:58 AM