Search code examples
c#xamltextbox

Write random text into Textbox


I'm creating n 8th ball and I'm trying to randomly generate one of the eight phrases into a textbox once a button is tapped and can't get my head around how to get my phrases in a textbox when a button is tapped.

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

Solution

  • Console.WriteLine() normally writes to the Console output in a Console App. So it seems you are mixing something up...

    Your textbox in the wpf app needs to have a variable name e.g. theTextBox and then you assign the string to the .Text property.

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