【问题标题】:Creating multiple TextBoxes based on user input根据用户输入创建多个文本框
【发布时间】:2021-12-12 04:50:13
【问题描述】:

大家好,

我正在使用 C# 在 UWP 中做一个桌面应用程序。

我想根据用户输入创建 unknow 文本框。

例如,假设我在 Page1 上有 TextBox 和 OkButton,用户必须在文本框中输入数字,然后按下 OkButton。之后,应用程序将导航到另一个页面(我们称之为 Page 2),该页面将包含与用户输入一样多的文本框。

例如,用户在 Page1 上的 TextBox 中键入 5,然后按下按钮,应用程序将导航到 Page2,它将包含 5 个 TextBox准备好另一个输入

这是一个示例源代码

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
}

感谢你们宝贵的时间和答案。 :)

【问题讨论】:

    标签: c# xaml uwp textbox user-input


    【解决方案1】:

    根据用户输入创建多个文本框

    您可以使用Navigate 方法将page1 的文本框数量传递给page2,然后在page2 中生成匹配的文本框计数

    例如

    第 1 页

    <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);
    }
    

    第2页

    <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);
               
            }
          
        }
    
    }
    

    【讨论】:

    • 非常感谢你 :) 它真的和例外一样有效。
    【解决方案2】:
    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;
        }
    }
    

    如果通过按钮单击创建 Page2,则可以在 Page2 的构造函数中调用 cr()。

    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)
        ....
    }
    

    【讨论】:

    • 非常感谢 :)
    • 很抱歉,但当我尝试它时,tb.Location 不存在。我试图找到一些解决方案,但我没有。
    • 不用担心。我看到您接受了 Nico Zhu 的更优雅和完整的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 2022-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多