Search code examples
c#windows-phone-8

Change text in Textbox with button click?


I'm making an app that totals up results from a coin toss, and I'm having trouble changing the numbers in my textboxes, and I could use some help.

So, how could I go about this?

Here's my code:

private void Button_Click_1(object sender, RoutedEventArgs e) {
        Random random = new Random();
        int number = random.Next(1, 100);

        //debug_TB.Text = Convert.ToString(number);

        if (number <= 50)
        {
            yesornogrid.Background = new SolidColorBrush(Colors.Green);
            numberTot = numberTot + 1;
            numberYes = numberYes + 1;

            yes_total_TB.Text = 
        }
        else if (number >= 51)
        {
            yesornogrid.Background = new SolidColorBrush(Colors.Red);
            numberTot = numberTot + 1;
            numberNo = numberNo + 1;
        }
    }

Solution

  • Just something like this:

    yes_total_TB.Text = string.Format("{0} YES of {1} tries", numberYes, numberTot);
    

    and of course

    no_total_TB.Text = string.Format("{0} NO of {1} tries", numberNo, numberTot);
    

    UPDATE

    In C# 6.0, this could be totally rewritten using string interpolation

    yes_total_TB.Text = $"{numberYes} YES of {numberTot} tries";
    no_total_TB.Text = $"{numberNo} NO of {numberTot} tries"