none
Nooby need help with code

    Question

  • Hope this is in the right forum, if not Mod please move to correct place.

    Completely new to Vb. Using visual studio 2012 premium. Projects are all windows forms. My issue is i cannot get a space in my output text to textbox. this is my whole code.

    Private Sub btnCompute_Click(sender As Object, e As EventArgs) Handles btnCompute.Click
            Dim Gwon, Glost, total, average As Double
            Gwon = txtWon.Text
            Glost = txtLost.Text
            total = (Glost + Gwon)
            average = (Gwon / total) * 100
            txtOutcome.Text = "The " & " " & txtTeam.Text & "won " & " " & average & "percent of their games this season. "

    my output txt box looks like this at the end:

    The  yankeeswon  50percent of their games this season.

    it needs to look like this:

    The  yankees won  50 percent of their games this season. - i cant seem to get the right code written for to get those 2 spaces i need. Any help would be appreciated.

    Wednesday, February 3, 2016 5:14 AM

Answers

  • Add a space before "percent":

    txtOutcome.Text = "The " & txtTeam.Text & " won " & average & " percent of their games this season."
    Another solution to try is:

    txtOutcome.Text = String.Format( "The {0} won {1} percent of their games this season. ", txtTeam.Text, average)
    


    • Edited by Viorel_MVP Wednesday, February 3, 2016 5:57 AM
    • Marked as answer by Gixxxer4Life Thursday, February 4, 2016 4:48 AM
    Wednesday, February 3, 2016 5:55 AM

All replies

  • Add a space before "percent":

    txtOutcome.Text = "The " & txtTeam.Text & " won " & average & " percent of their games this season."
    Another solution to try is:

    txtOutcome.Text = String.Format( "The {0} won {1} percent of their games this season. ", txtTeam.Text, average)
    


    • Edited by Viorel_MVP Wednesday, February 3, 2016 5:57 AM
    • Marked as answer by Gixxxer4Life Thursday, February 4, 2016 4:48 AM
    Wednesday, February 3, 2016 5:55 AM
  • Maybe you need to format that percentage as well:

         'Declare the variable
         Dim average as double
    
    
         average = (Gwon / total) * 100
         txtOutcome.Text = "The " & txtTeam.Text & " won " & Strings.Format(average,"00") &  "percent of their games this season."
    
        'Alternatively for percentages it looks good with
        'Strings.Format(average,'Percent")
         average = (Gwon / total)
         txtOutcome.Text = "The " & txtTeam.Text & " won " & Strings.Format(average,"Percent") &  " of their games this season."


    Cyrille Precetti


    Wednesday, February 3, 2016 10:05 AM
  • "Completely new to Vb. Using visual studio 2012 premium. Projects are all windows forms. My issue is i cannot get a space in my output text to textbox. this is my whole code."

    You already have two good suggestions for solving your output format issue (which you should mark as answers, btw), but if you intend to work with VB and want to improve your code, I suggest that you turn on Option Strict in all your projects so you can see all the dangerous things you are doing in your code.

    You are doing arithmetic with strings. The contents of any text box is type String. You need to learn about testing input to make sure it is numeric before doing math, and converting strings to numeric types.

    For the best and fastest help with your VB code, post here in future:

    https://social.msdn.microsoft.com/Forums/en-US/vbgeneral/threads

    Wednesday, February 3, 2016 6:01 PM
  • I will mark best answer when i get home and am able to try it to see which solves my issue. Not tryibg to be rude just making sure i dont mark answers that dont solve my issue for others that may have the same problem
    Wednesday, February 3, 2016 11:05 PM
  • i ended up having to to do " " spaces to get it to finally work. for some reason any spaces i put in " or ( is automatically compacted back down to ( n ) spacing. but you answers gave me new ideas i had not though of that worked.
    Thursday, February 4, 2016 4:49 AM