Home > database >  Creating multiple TextBoxes based on user input
Creating multiple TextBoxes based on user input

Time:10-27

Hy guys,

I am doing an desktop app in UWP using C#.

And I want to create unknow text boxes based on user input.

For example let's say that I have TextBox and OkButton on Page1 the user have to type number in text box and than press the OkButton. After that the app will navigate to the another page (let's call it Page 2) which will has as many text boxes as the input of user was.

For an instance user will type 5 into the TextBox on the Page1 than he will press the button, the App will navigate to the Page2 and it will contain 5 TextBoxes ready for another input

Here is an example source code

Page1.xaml

<Grid>
 <TextBox x:Name="UserInput" Margin="558,459,557,459" Header="UserInput"/>
 <Button x:Name="DoneButton" Height="29" Width="95" Margin="558,565,0,0" VerticalAlignment="Top" Content="Done" Click="DoneButton_Click"/> </Grid>

Page1.xaml.cs

public sealed partial class Page1 : Page
{
    public Page1()
    {
        this.InitializeComponent();
    }

    private void DoneButton_Click(object sender, RoutedEventArgs e)
    {
      (App.Current as App).UserInput =  Convert.ToInt32( UserInput.Text); // this will store userinput data in the global app variable
                                                                          // variable so I can work with that later
        this.Frame.Navigate(typeof(Page2));
    }

Page2.xaml

<Grid>
    <TextBlock Text="Here I want to create text boxes based on user input" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="30"/>
</Grid>

Page2.xaml.cs

 public sealed partial class Page2 : Page
{
    public Page2()
    {
        this.InitializeComponent();
    }

    public int UserInput = (App.Current as App).UserInput; //storing global app variable to work with it later

    //here I want to crate multiple text boxes
}

Thank you guys for your valuable time and answers. :)

CodePudding user response:

public void cr(int numberoftextboxes)
{
    int x = 10;  // < --your starting x coordinate(will stay the same if you want your textboxes stacked horizontally.
    int y = 10;  // < --your starting y coordinate
    int h = 20;  // < --textbox height
    int w = 100; // < --textbox width
    int s = 15;  // < --spacing

    for (int c = 0; c < numberoftextboxes; c  )
    {
        TextBox tb = new TextBox();
        tb.Name = "Your TextBox name";
        tb.Text = "Your TextBox Text";
        tb.Size = new Size(w, h);
        Page2.Controls.Add(tb);
        tb.Location = new Point(x, y);
        y = y   h   s;
    }
}

if you create Page2 from the button click, you can place the call to cr() in the constructor of Page2.

public sealed partial class Page2 : Page
{
    public Page2()
    {
        this.InitializeComponent();
        cr();
    }

    public int UserInput = (App.Current as App).UserInput; //storing global app variable to work with it later

    private void cr(int numberoftextboxes)
    ....
}

CodePudding user response:

Creating multiple TextBoxes based on user input

You could pass page1's number of textbox to page2 with Navigate method, then generate matched count of textbox in page2

For example

page1

<StackPanel Orientation="Vertical">
    <TextBox x:Name="Number" />
    <Button
        x:Name="MyBtn"
        Click="Button_Click"
        Content="Generate" />
</StackPanel>

.......

private void Button_Click(object sender, RoutedEventArgs e)
{
    Frame.Navigate(typeof(AddPage), Number.Text);
}

Page2

<StackPanel x:Name="RootLayout" Orientation="Vertical" />

......

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    if (e.Parameter != null && e.Parameter.ToString() != string.Empty)
    {
        var count = Convert.ToInt32(e.Parameter);
      
        for (int i = 0; i < count; i  )
        {
            var textbox = new TextBox();
            textbox.Margin = new Thickness(0, 0, 15, 0);
            RootLayout.Children.Add(textbox);
           
        }
      
    }

}
  • Related