将随机文本写入文本框

时间:2018-01-12 10:39:08

标签: c# xaml textbox

我正在创建第8个球,并且我试图在按下按钮后随机生成八个短语中的一个到文本框中,并且无法理解如何获取我的短语点击按钮时在文本框中。

private void Button1_Tapped(object sender, TappedRoutedEventArgs e)
    {
        Random num = new Random();
        int a = num.Next(9);

        switch (a)
        {
            case 0:
               Console.;
                break;
            case 1:
                Console.WriteLine.TextBox("Yes");
                break;
            case 2:
                Console.WriteLine.TextBox("No");
                break;
            case 3:
                Console.WriteLine.TextBox("Maybe");
                break;
            case 4:
                Console.WriteLine.TextBox("You could say that");
                break;
            case 5:
                Console.WriteLine.TextBox("Most certain");
                break;
            case 6:
                Console.WriteLine.TextBox("Dont even try");
                break;
            case 7:
                Console.WriteLine.TextBox("Full steam ahead");
                break;
        }
    }

1 个答案:

答案 0 :(得分:1)

Console.WriteLine()通常在Console App中写入Console输出。所以看来你在混淆什么...

wpf应用中的文本框需要具有变量名称,例如theTextBox然后将字符串分配给.Text属性。

private void Button1_Tapped(object sender, TappedRoutedEventArgs e)
{
    Random num = new Random();
    int a = num.Next(9);

    switch (a)
    {
        case 0:
            theTextBox.Text = "";
            break;
        case 1:
            theTextBox.Text = "Yes";
            break;
        case 2:
            theTextBox.Text = "No";
            break;
        case 3:
            theTextBox.Text = "Maybe";
            break;
        case 4:
            theTextBox.Text = "You could say that";
            break;
        case 5:
            theTextBox.Text = "Most certain";
            break;
        case 6:
            theTextBox.Text = "Dont even try";
            break;
        case 7:
            theTextBox.Text = "Full steam ahead";
            break;
    }
}