簡體   English   中英

根據用戶輸入創建多個文本框

[英]Creating multiple TextBoxes based on user input

嘿伙計們,

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

我想根據用戶輸入創建未知的文本框。

例如,假設我在Page1上有 TextBox 和 OkButton,用戶必須在文本框中鍵入數字,然后按下 OkButton。 之后,應用程序將導航到另一個頁面(我們稱之為Page 2 ),該頁面將具有與用戶輸入一樣多的文本框。

例如,用戶將在Page1上的 TextBox 中鍵入 5 而不是按下按鈕,應用程序將導航到Page2並且它將包含 5 個准備好用於另一個輸入的 TextBox

這是一個示例源代碼

頁面1.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>

頁面1.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));
    }

頁面2.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
}

感謝大家的寶貴時間和答案。 :)

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

根據用戶輸入創建多個文本框

您可以使用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);
           
        }
      
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM