Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

I creating dynamic Textbox within stackpanel. when i create 2 by 2 (two columns and two lines). It's take one line four columns. please help and thanks

Code:
private void button1_Click(object sender, RoutedEventArgs e)
{
    StackPanel txtbox = new StackPanel();
    txtbox.Orientation = Orientation.Horizontal;
    txtbox.Height = 23;

    int y = 10;
    int row = Convert.ToInt32(textBox1.Text);
    int col = row;

    int count = 0;
    for (int i = 0; i < row; i++)
    {
        int x = 10;
        for (int j = 0; j < col; j++)
        {
            TextBox txt = new TextBox();
            txt.Width = 75;
            txt.Name = "tb" + count;
            count++;

            x = x + 70;
            txtbox.Children.Add(txt);
        }
        y = y + 25;
    }
    TxtStack.Children.Add(txtbox);
}



XMAL Code:

XML
<Grid>
    <StackPanel Name="TxtStack" Margin="0,0,0,178"></StackPanel>
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="416,159,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    <TextBox Name="textBox1" Width="120" Margin="290,159,93,129" />
</Grid>
Posted

1 solution

No change in Xaml, change required in code, the way the stackpanel is used as below:

C#
private void button1_Click(object sender, RoutedEventArgs e)
{
    int y = 10;
    int row = Convert.ToInt32(textBox1.Text);
    int col = row;

    int count = 0;
    for (int i = 0; i < row; i++)
    {
        StackPanel txtbox = new StackPanel();
        txtbox.Orientation = Orientation.Horizontal;
        txtbox.Height = 23;

        int x = 10;
        for(int j = 0; j < col; j++)
        {
            TextBox txt = new TextBox();
            txt.Width = 75;
            txt.Name = "tb" + count;
            count++;

            x = x + 70;
            txtbox.Children.Add(txt);
        }
        TxtStack.Children.Add(txtbox);
        y = y + 25;
    }
}
 
Share this answer
 
v2
Comments
Simon Bang Terkildsen 10-Sep-11 16:21pm    
added codeblock
Pradeep Shukla 10-Sep-11 16:21pm    
Thanks simon :-)
gpasupathi 11-Sep-11 5:00am    
Dear Sir, How can i know the cursor position, suppose cursor is 2 line 2 column.

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