Solved

VB Code- HELP!

Posted on 2007-11-27
10
178 Views
Last Modified: 2016-08-29
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

0
Comment
Question by:ashblynn02
  • 4
  • 4
  • 2
10 Comments
 

Author Comment

by:ashblynn02
Comment Utility
0
 
LVL 48

Expert Comment

by:jpaulino
Comment Utility
And what you realy need ???
0
 
LVL 38

Expert Comment

by:PaulHews
Comment Utility
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
 

Author Comment

by:ashblynn02
Comment Utility
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
 
LVL 48

Expert Comment

by:jpaulino
Comment Utility
>> I can't figure why the rest of the txt boxes are not working!!

But you are assigning any values to the textboxs !!!
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 38

Accepted Solution

by:
PaulHews earned 500 total points
Comment Utility
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
 
LVL 48

Expert Comment

by:jpaulino
Comment Utility
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
 

Author Comment

by:ashblynn02
Comment Utility
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
 
LVL 48

Expert Comment

by:jpaulino
Comment Utility
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
 

Author Comment

by:ashblynn02
Comment Utility
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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now