How to add textbox control dynamically in runtime in WPF using c#

后端 未结 2 818
猫巷女王i
猫巷女王i 2021-01-29 04:00

I am new in WPF.I want to create 3 textbox for each row at runtime when i click generate button. please help me.

Automatically created textbox

 **Code b         


        
相关标签:
2条回答
  • 2021-01-29 04:36

    I assume your page's content is a Canvas control:

    Code behind:

    protected void ButtonGenerate_Click(object sender, RoutedEventArgs e)
    {
        TextBox tb = new TextBox();
        (this.Content as Canvas).Children.Add(tb);
    }
    

    You may find other textbox property in https://msdn.microsoft.com/en-us/library/system.windows.controls.textbox(v=vs.110).aspx

    0 讨论(0)
  • 2021-01-29 04:57

    Just give your grid a name in xaml :

    <Grid x:Name="Grid1">
        <Button Grid.Column="0"
                Margin="10"
                Click="btnGenerate_Click">
            Button Content
        </Button>
    </Grid>
    

    and in code behind :

    private void btnGenerate_Click(object sender, RoutedEventArgs e)
    {
        //Get the number of input text boxes to generate
        int inputNumber = Int32.Parse(textBoxInput.Text);
    
        //Initialize list of input text boxes
        inputTextBoxes = new List<TextBox>();
    
        //Generate labels and text boxes
        for (int i = 1; i <= inputNumber; i++)
        {
            //Create a new label and text box
            Label labelInput = new Label();
            Grid1.Children.Add(labelInput);
            TextBox textBoxNewInput = new TextBox();
            Grid1.Children.Add(textBoxNewInput);
        }
    }
    

    If you want, you can set the position also so that the newly created elements doesn't overlap each other.

    EDIT :

    You need to create a grid with required rows and columns and then place each textbox or label inside the required row-column combination. It contains 6 rows and 3 columns. Please see the example below :

    <Grid Name="Grid1">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
    </Grid> 
    
    private void btnGenerate_Click(object sender, RoutedEventArgs e)
    {
        //Get the number of input text boxes to generate
        int inputNumber = Int32.Parse(textBoxInput.Text);
    
        //Initialize list of input text boxes
        inputTextBoxes = new List<TextBox>();
    
        //Generate labels and text boxes
        for (int i = 1; i <= inputNumber; i++)
        {
            //Create a new label and text box
            Label labelInput = new Label();
    
            Grid.SetColumn(labelInput, i);
            Grid.SetRow(labelInput, i);
            Grid1.Children.Add(labelInput);
    
            TextBox textBoxNewInput = new TextBox();
            Grid.SetColumn(labelInput, i+1);
            Grid.SetRow(labelInput, i);
            Grid1.Children.Add(textBoxNewInput);
        }
    }
    
    0 讨论(0)
提交回复
热议问题