Results 1 to 6 of 6

Thread: Format Exception Unhandled

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    2

    Format Exception Unhandled

    When I run this program I get a Format Exception was Unhandled Input String Format was not a correct format. It happens at the 2nd Listbox1.text line when I have the Sting formatted Anyone have any ideas
    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim Pop As IO.StreamReader = IO.File.OpenText("populationdata.txt")
    3.         Dim Group As String
    4.         Dim Male, Female, Total, Male2, Female2, Total2 As Double
    5.         Dim Total3 As Double
    6.         Dim fmtstng As String = "{0,-15} {1,14} {2,13} (3,12:p} {4,11:p} {5,10:p}"
    7.  
    8.         Group = Pop.ReadLine
    9.         Total += CDbl(Pop.ReadLine)
    10.         Total += CDbl(Pop.ReadLine)
    11.         Group = Pop.ReadLine
    12.         Total += CDbl(Pop.ReadLine)
    13.         Total += CDbl(Pop.ReadLine)
    14.         Group = Pop.ReadLine
    15.         Total += CDbl(Pop.ReadLine)
    16.         Total += CDbl(Pop.ReadLine)
    17.         Pop.Close()
    18.         ListBox1.Items.Add("Age Group    Females     %Males   %Females   %Total")
    19.         Pop = IO.File.OpenText("populationdata.txt")
    20.         Group = Pop.ReadLine
    21.         Male = CDbl(Pop.ReadLine)
    22.         Female = CDbl(Pop.ReadLine)
    23.         Total2 = Male + Female
    24.         Male2 = Male / Total2 * 100
    25.         Female2 = Female / Total2 * 100
    26.         Total3 = Total2 / Total * 100
    27.         ListBox1.Items.Add(String.Format(fmtstng, Group, Male, Female, Male2, Female2, Total3)):wave:  This is where I have the issue
    28.         Group = Pop.ReadLine
    29.         Male = CDbl(Pop.ReadLine)
    30.         Female = CDbl(Pop.ReadLine)
    31.         Total2 = Male + Female
    32.         Male2 = Male / Total2 * 100
    33.         Female2 = Female / Total2 * 100
    34.         Total3 = Total2 / Total * 100
    35.         ListBox1.Items.Add(String.Format(fmtstng, Group, Male, Female, Male2, Female2, Total3))
    36.         Group = Pop.ReadLine
    37.         Male = CDbl(Pop.ReadLine)
    38.         Female = CDbl(Pop.ReadLine)
    39.         Total2 = Male + Female
    40.         Male2 = Male / Total2 * 100
    41.         Female2 = Female / Total2 * 100
    42.         Total3 = Total2 / Total * 100
    43.         ListBox1.Items.Add(String.Format(fmtstng, Group, Male, Female, Male2, Female2, Total3))
    44.         Pop.Close()
    Last edited by Hack; May 30th, 2007 at 10:45 AM. Reason: Added VB Highlight Tags

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Format Exception Unhandled

    Moved to VB.NET

  3. #3
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Format Exception Unhandled

    It looks like you are using String.Format incorrectly. What are you intending to happen when you use String.Format?
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    2

    Re: Format Exception Unhandled

    Im using the String Format to post the variables pulled from the Text files in a list box in certain format that looks like this

    Age Group Males Females %males %females %total
    25-40 51,015 47,898 51.15% 48.9% 35%


    There second line is what I am trying to print to the list box with that string.format code

  5. #5
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: Format Exception Unhandled

    You need at least two arguments for System.String.Format. Have a look through the Object Browser for details on it.
    Prefix has no suffix, but suffix has a prefix.

  6. #6
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Format Exception Unhandled

    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width