VB Code- HELP!

Hello to all :)
Urgent!! Help Pllllease!!!
 I am trying to enhance something I am working on to make it better. I created a project that took a series of numbers that was entered by the user and displayed: average, minimum, maximum, total, and total numbers.
I now have a text file opening to display the numbers. The program reads one line at a time. When the first number is requested, the data file is to be opened and the first number is to be read and processed. The file is to be left open for the following numbers to be read. The program then is to continue reading numbers until all numbers have been read. When the program is ended the file is to be closed.

I have the majority of it completed... I have it to where the text file is opening and where the count figure is working but from there nothing is reading...

I will attach my project if that will help!


Imports System.IO
Public Class Form1
    Dim InpFileName, Temp As String, FileOpenFlag As Integer
    Dim FileData(100) As String, ItemCount As Integer
    Dim DataFile As StreamReader
    Dim InpNumber, InpTotal, InpCount, InpAverage As Double
    Dim InpMaximum, InpMinimum As Double
 
    Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        Dim dgrResult As DialogResult
        Dim dlgOpen As New OpenFileDialog
        'If FileOpenFlag > 0 Then DataFile.Close()
 
        dlgOpen.InitialDirectory = "F:"
        dlgOpen.Filter = "Text Files|*.txt|Word Files|*.doc|All Files|*.*"
        dgrResult = dlgOpen.ShowDialog()
        If dgrResult = DialogResult.OK Then
            InpFileName = dlgOpen.FileName
            DataFile = File.OpenText(InpFileName)
            FileOpenFlag = 1
            ItemCount = 0
        Else
            TxtData.Text = "File not specified"
        End If
    End Sub
 
    Private Sub btnReadNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadNext.Click
 
 
        If FileOpenFlag > 0 Then
            If DataFile.Peek <> -1 Then
                Temp = DataFile.ReadLine
                If Temp.Trim <> "" Then
                    ItemCount = ItemCount + 1
                    FileData(ItemCount) = Temp.Trim
                    TxtCount.Text = CStr(ItemCount)
                    TxtData.Text = FileData(ItemCount)
                    InpNumber = CDbl(TxtData.Text)
                    If InpCount = 0 Then
                        InpTotal = InpNumber
                        InpAverage = InpNumber
                        InpMinimum = InpNumber
                        InpMaximum = InpNumber
 
                    Else
                        InpTotal = InpTotal + InpNumber
                        InpAverage = InpTotal / InpCount
                        If InpMinimum > InpNumber Then
                            InpMinimum = InpNumber
                            If InpMaximum < InpNumber Then
                                InpMaximum = InpNumber
                            End If
                        End If
                    End If
                Else
                    TxtData.Text = ""
                    TxtData.Text = "Blank line - Not stored"
                End If
            End If
        End If
 
       
    End Sub
 
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If FileOpenFlag > 0 Then DataFile.Close()
    End Sub
 
    Private Sub BtnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClear.Click
        Dim Inpdata, inpTotal, InpCount, InpAverage As Double
        Dim InpMaximum, InpMinimum As Double
 
        Inpdata = CDbl(TxtData.Text)
 
        If Inpdata = Inpdata Then
            inpTotal = 0
            InpAverage = 0
            InpMinimum = 0
            InpMaximum = 0
            InpCount = 0
            Inpdata = 0
        End If
 
 
        TxtData.Text = CStr(Inpdata)
        TxtTotal.Text = CStr(InpTotal)
        TxtAverage.Text = CStr(InpAverage)
        TxtMin.Text = CStr(InpMinimum)
        TxtMax.Text = CStr(InpMaximum)
        TxtCount.Text = CStr(InpCount)
    End Sub
 
 
 
End Class

Open in new window

ashblynn02Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
ashblynn02Author Commented:
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
And what you realy need ???
0
 
PaulHewsCommented:
I notice your filter includes Word (.doc) files.  Make sure you are not using a Word file, as this is a proprietary format that is not like a text file at all.

The code works for me, at least loading items into the text boxes for count and data when I click btnReadNext

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
ashblynn02Author Commented:
yeah the count and data works but none of the other ones. I can't figure why the rest of the txt boxes are not working!!

Does that makes sense at all?
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
>> I can't figure why the rest of the txt boxes are not working!!

But you are assigning any values to the textboxs !!!
0
 
PaulHewsCommented:
Because it might help if you had code to put data in the rest of the textboxes.  Also you will need to increment your InpCount variable somewhere.  
    Private Sub btnReadNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadNext.Click
 
 
        If FileOpenFlag > 0 Then
            If DataFile.Peek <> -1 Then
                Temp = DataFile.ReadLine
                If Temp.Trim <> "" Then
                    ItemCount = ItemCount + 1
                    FileData(ItemCount) = Temp.Trim
                    TxtCount.Text = CStr(ItemCount)
                    TxtData.Text = FileData(ItemCount)
                    InpNumber = CDbl(TxtData.Text)
                    If InpCount = 0 Then
                        InpTotal = InpNumber
                        InpAverage = InpNumber
                        InpMinimum = InpNumber
                        InpMaximum = InpNumber
 
                    Else
                        InpTotal = InpTotal + InpNumber
                        InpAverage = InpTotal / InpCount
                        If InpMinimum > InpNumber Then
                            InpMinimum = InpNumber
                            If InpMaximum < InpNumber Then
                                InpMaximum = InpNumber
                            End If
                        End If
                    End If
                    'increment count
                    InpCount += 1
                Else
                    TxtData.Text = ""
                    TxtData.Text = "Blank line - Not stored"
                End If
            End If
        End If
 
        TxtTotal.Text = CStr(InpTotal)
        TxtAverage.Text = CStr(InpAverage)
        TxtMin.Text = CStr(InpMinimum)
        TxtMax.Text = CStr(InpMaximum)
        TxtCount.Text = CStr(InpCount)
       
    End Sub

Open in new window

0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
Jorge PaulinoIT Pro/DeveloperCommented:
And then If statement are wrong! Change it to this

If InpMinimum > InpNumber Then
   InpMinimum = InpNumber
End If
If InpMaximum < InpNumber Then
   InpMaximum = InpNumber
End If
0
 
ashblynn02Author Commented:
Ok... i will try all of that and see if it works! Thanks so much for all your help! :)
If it doesn't work... I'll be back!
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
Since InpMinimum will be initialized as zero will be always zero. You have to initialize as 1 or a bigger number on the form load. Then when it starts you will grad the minimum number of your file.

InpMinimum  = 1
0
 
ashblynn02Author Commented:
Okay so far the majority of it is working out :) yaaah!

There are still two little problems:

1) The average won't come out right though and I can't figure out why?

2) Also when I click the reset button it sets it to zeros and then if I hit read next again everything goes right back into the boxes. How do I get it to where it resets everything?

Thanks again for all your help :) You guys rock!!!
Imports System.IO
Public Class Form1
    Dim InpFileName, Temp As String, FileOpenFlag As Integer
    Dim FileData(100) As String, ItemCount As Integer
    Dim DataFile As StreamReader
    Dim InpNumber, InpTotal, InpCount, InpAverage As Double
    Dim InpMaximum, InpMinimum As Double
 
    Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        Dim dgrResult As DialogResult
        Dim dlgOpen As New OpenFileDialog
        'If FileOpenFlag > 0 Then DataFile.Close()
 
        dlgOpen.InitialDirectory = "F:"
        dlgOpen.Filter = "Text Files|*.txt|Word Files|*.doc|All Files|*.*"
        dgrResult = dlgOpen.ShowDialog()
        If dgrResult = DialogResult.OK Then
            InpFileName = dlgOpen.FileName
            DataFile = File.OpenText(InpFileName)
            FileOpenFlag = 1
            ItemCount = 0
        Else
            TxtData.Text = "File not specified"
        End If
    End Sub
 
    Private Sub btnReadNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnReadnext.Click
 
 
        If FileOpenFlag > 0 Then
            If DataFile.Peek <> -1 Then
                Temp = DataFile.ReadLine
                If Temp.Trim <> "" Then
                    ItemCount = ItemCount + 1
                    FileData(ItemCount) = Temp.Trim
                    TxtCount.Text = CStr(ItemCount)
                    TxtData.Text = FileData(ItemCount)
                    InpNumber = CDbl(TxtData.Text)
                    If InpCount = 0 Then
                        InpTotal = InpNumber
                        InpAverage = InpNumber
                        InpMinimum = InpNumber
                        InpMaximum = InpNumber
 
                    Else
                        InpTotal = InpTotal + InpNumber
                        InpAverage = InpTotal / InpCount
                        If InpMinimum > InpNumber Then
                            InpMinimum = InpNumber
                            If InpMaximum < InpNumber Then
                                InpMaximum = InpNumber
                            End If
                        End If
                    End If
                    'increment count
                    InpCount += 1
                Else
                    TxtData.Text = ""
                    TxtData.Text = "Blank line - Not stored"
                End If
            End If
        End If
 
        TxtTotal.Text = CStr(InpTotal)
        TxtAverage.Text = CStr(InpAverage)
        TxtMin.Text = CStr(InpMinimum)
        TxtMax.Text = CStr(InpMaximum)
        TxtCount.Text = CStr(InpCount)
 
    End Sub
 
 
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If FileOpenFlag > 0 Then DataFile.Close()
    End Sub
 
    Private Sub BtnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClear.Click
        Dim Inpdata, inpTotal, InpCount, InpAverage As Double
        Dim InpMaximum, InpMinimum As Double
 
        Inpdata = CDbl(TxtData.Text)
 
        If Inpdata = Inpdata Then
            inpTotal = 0
            InpAverage = 0
            InpMinimum = 0
            InpMaximum = 0
            InpCount = 0
            Inpdata = 0
        End If
 
 
        TxtData.Text = CStr(Inpdata)
        TxtTotal.Text = CStr(InpTotal)
        TxtAverage.Text = CStr(InpAverage)
        TxtMin.Text = CStr(InpMinimum)
        TxtMax.Text = CStr(InpMaximum)
        TxtCount.Text = CStr(InpCount)
    End Sub
 
 
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnReadnext.Click
 
    End Sub
End Class

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.