Solved

How would I turn this console application into a windows form application?  PLEASE HELP!

Posted on 2006-06-18
20
228 Views
Last Modified: 2010-04-23
I am having trouble turning a console application into a windows application.  Basically, the form would have 4 text boxes.  Each text box would house the file path of two three boxes and one csv file.  Each text box would have a button right next to it that would prompt the user to open a file path, 2 for txt and one for csv..the last text box and button would be the export file which would have a save dialog attached.  How would I do this?  I have been trying for a week now and still not getting it to work.  Any takers?  I would really appreciate it!  Thanks!  Below is the console application Module.

Imports System
Imports System.IO
Imports System.Collections

Module Module2
    Dim sr114Input As StreamReader
    Dim sr385Input As StreamReader
    Dim swOutput As StreamWriter
    Dim strRunDate As String
    Dim strRunTime As String
    Dim strInput As String
    Dim strWorkField As String
    Dim strWorkChar As String
    Dim lngCalcField As Long
    Dim employeeDeferral As Long
    Dim employerMatching As Long
    Dim employeeSSN As String
    Dim strOutput As String
    Dim detailRecordCount As Integer
    Dim totalContributionAmount As Long
    Dim loanRepaymentAmount As Long
    Dim sourceArray As Hashtable = New Hashtable

    Function Main(ByVal cmdArgs() As String) As Integer
        If cmdArgs.Length = 3 Then
            Try
                sr114Input = New StreamReader(cmdArgs(0))
                sr385Input = New StreamReader(cmdArgs(1))

                swOutput = New StreamWriter(cmdArgs(2))

            Catch e As Exception
                Console.WriteLine(e.Message)
                Return -1
            End Try
        Else
            Console.WriteLine("usage: Extensis401K <file114.txt> <file385.txt> <pruout.txt>")
            Return -1
        End If

        Try
            ' Create an instance of StreamReader to read from file.
            Using srChart As StreamReader = New StreamReader("401K Chart.csv")
                Dim line As String
                Dim lineArray() As String
                'read and display the lines from the file until the end
                'of the file is reached
                Do
                    line = srChart.ReadLine()
                    If line Is Nothing Then Exit Do
                    lineArray = line.Split(",")
                    If lineArray.Length = 3 Then
                        sourceArray.Add(lineArray(1), lineArray(0))
                    Else
                        Console.WriteLine("Invalid 401K Chart.csv format")
                        Return -1
                    End If
                Loop
                srChart.Close()
            End Using
        Catch e As Exception
            'Let the user know what went wrong.
            Console.WriteLine("The file could not be read:")
            Console.WriteLine(e.Message)
            Return -1
        End Try

        Dim dtRun As Date = Now()
        strRunDate = dtRun.Date.Year.ToString("0000") & dtRun.Date.Month.ToString("00") & dtRun.Date.Day.ToString("00")
        strRunTime = dtRun.Hour.ToString("00") & dtRun.Minute.ToString("00") & dtRun.Second.ToString("00")

        strOutput = "001COMBINED  002496   " & strRunDate & strRunTime & "C" & Space(593)
        swOutput.WriteLine(strOutput)

        detailRecordCount = 0
        totalContributionAmount = 0
        Do              'Process 114
            strInput = sr114Input.ReadLine()
            If strInput Is Nothing Then Exit Do
            strOutput = "11401002496   "

            ' Social Security Number
            employeeSSN = ""
            MoveIt(1, 9, employeeSSN)
            strOutput &= employeeSSN & Space(12)

            'Employee Deferral
            strWorkField = ""
            MoveIt(12, 9, strWorkField)
            employeeDeferral = Convert.ToInt64(FormatIt(strWorkField))
            strWorkField = "000000000" & ConvertToEBCDIC(employeeDeferral)
            strOutput &= "**A" & strWorkField.Substring(strWorkField.Length - 9, 9)

            'Employer Matching
            strWorkField = ""
            MoveIt(23, 9, strWorkField)
            employerMatching = Convert.ToInt64(FormatIt(strWorkField))
            strWorkField = "000000000" & ConvertToEBCDIC(employerMatching)
            strWorkField = strWorkField.Substring(strWorkField.Length - 9, 9)

            totalContributionAmount += employeeDeferral
            totalContributionAmount += employerMatching

            strWorkChar = ""
            MoveIt(40, 4, strWorkChar) ' Sub Plan Number Character for hashtable
            strWorkChar = sourceArray(strWorkChar)

            If employerMatching <> 0 And strWorkChar = "none" Then
                Console.WriteLine("No Matching Plan for non-zero deferral for record")
                Return -1
            End If
            strOutput &= "**P"
            If strWorkChar = "P" Then
                strOutput &= strWorkField   'Employer Matching
            Else
                strOutput &= "000000000"
            End If
            strOutput &= "**6"
            If strWorkChar = "6" Then
                strOutput &= strWorkField   'Employer Matching
            Else
                strOutput &= "000000000"
            End If
            strOutput &= "**4"
            If strWorkChar = "4" Then
                strOutput &= strWorkField   'Employer Matching
            Else
                strOutput &= "000000000"
            End If

            strOutput &= "**D"
            If strWorkChar = "D" Then
                strOutput &= strWorkField   'Employer Matching
            Else
                strOutput &= "000000000"
            End If

            strOutput &= "**C"
            If strWorkChar = "C" Then
                strOutput &= strWorkField   'Employer Matching
            Else
                strOutput &= "000000000"
            End If

            ' Pay Period End Date
            MoveIt(32, 8, strOutput)
            strOutput &= " "

            'Sub Plan Number
            MoveIt(40, 4, strOutput)

            'Formatted Name
            strWorkField = ""
            MoveIt(44, 24, strWorkField)    'Last Name
            strWorkField = strWorkField.Trim & ", "
            MoveIt(68, 12, strWorkField)    'First Name
            strWorkField = strWorkField.Trim & " "
            MoveIt(80, 1, strWorkField) 'Middle Initial
            strWorkField &= Space(30)
            strOutput &= strWorkField.Substring(0, 30)

            'Address 1
            MoveIt(81, 30, strOutput)

            'Address 2
            MoveIt(117, 30, strOutput)

            'City
            MoveIt(153, 18, strOutput)

            'State
            MoveIt(177, 2, strOutput)

            ' Zip Code
            MoveIt(179, 9, strOutput)


            'Date of Birth
            MoveIt(189, 8, strOutput)

            'Original Date of Hire
            MoveIt(197, 8, strOutput)
            strOutput &= Space(8)

            'Marital Status
            strWorkField = ""
            MoveIt(205, 1, strWorkField)    'Marital Status
            Select Case strWorkField
                Case "S"
                    strOutput &= "1"
                Case "M"
                    strOutput &= "2"
                Case Else
                    Console.WriteLine("Invalid marital status for record " & strOutput)
                    Return -1
            End Select

            'Gender Code
            strWorkField = ""
            MoveIt(206, 1, strWorkField)
            Select Case strWorkField
                Case "M"
                    strOutput &= "1"
                Case "F"
                    strOutput &= "2"
                Case Else
                    Console.WriteLine("Invalid gender code for record " & strOutput)
                    Return -1
            End Select

            'EE Status
            strWorkField = ""
            MoveIt(207, 1, strWorkField)
            Select Case strWorkField
                Case "A"
                    strOutput &= "00"
                    MoveIt(197, 8, strOutput) ' Original Date of Hire
                Case "T"
                    strOutput &= "32"
                    MoveIt(208, 8, strOutput) ' Termination Date
                Case Else
                    Console.WriteLine("Invalid EE status for record " & strOutput)
                    Return -1
            End Select

            'Payroll Frequencey
            strWorkField = ""
            MoveIt(216, 1, strWorkField)
            Select Case strWorkField
                Case "Q"
                    strOutput &= "3"
                Case "M"
                    strOutput &= "4"
                Case "S"
                    strOutput &= "5"
                Case "B"
                    strOutput &= "6"
                Case "W"
                    strOutput &= "7"
                Case Else
                    Console.WriteLine("Invalid payroll frequency for record " & strOutput)
                    Return -1
            End Select

            strOutput &= Space(14)

            'Current Pay Period hours
            strWorkField = ""
            MoveIt(219, 4, strWorkField)
            lngCalcField = Convert.ToInt64(FormatIt(strWorkField))
            strWorkField = "000000" & ConvertToEBCDIC(lngCalcField)
            strOutput &= strWorkField.Substring(strWorkField.Length = 6, 6)

            'Current Pay Period Gross Pay
            strWorkField = ""
            MoveIt(226, 11, strWorkField)
            lngCalcField = Convert.ToInt64(FormatIt(strWorkField))
            strWorkField = "000000000000000" & ConvertToEBCDIC(lngCalcField)
            strOutput &= strWorkField.Substring(strWorkField.Length - 15, 15)
            strOutput &= Space(40)

            ' Check Date
            MoveIt(237, 8, strOutput)
            strOutput &= Space(98)

            ' Market Salary
            strWorkField = ""
            MoveIt(245, 10, strWorkField)
            strWorkField = strWorkField.Trim.Replace(".", "")
            lngCalcField = Convert.ToInt64(FormatIt(strWorkField))
            strWorkField = "00000000000" & ConvertToEBCDIC(lngCalcField)
            strOutput &= strWorkField.Substring(strWorkField.Length - 11, 11)
            strOutput &= Space(163)

            'Print Record
            swOutput.WriteLine(strOutput)
            detailRecordCount += 1
        Loop    'End of process 114

        loanRepaymentAmount = 0
        Do  'Process 385       loan
            strInput = sr385Input.ReadLine()
            If strInput Is Nothing Then Exit Do
            strOutput = "385  002496   "

            'social Security Number
            employeeSSN = ""
            MoveIt(1, 9, employeeSSN)
            strOutput &= employeeSSN & Space(6) & "1     "

            'Loan Repayment Principal and Interest
            strWorkField = ""
            MoveIt(12, 9, strWorkField)
            lngCalcField = Convert.ToInt64(FormatIt(strWorkField))
            If lngCalcField = 0 Then
                Console.WriteLine("Zero Loan Repayment for " & employeeSSN)
                Continue Do
            Else
                loanRepaymentAmount += lngCalcField
            End If

            strWorkField = "000000000" & ConvertToEBCDIC(lngCalcField)
            strOutput &= strWorkField.Substring(strWorkField.Length - 9, 9)
            strOutput &= Space(10) & "2   PDED" & Space(19)

            'Sub Plan Number
            MoveIt(21, 4, strOutput)
            strOutput &= Space(547)

            'Print it out
            swOutput.WriteLine(strOutput)
            detailRecordCount += 1
        Loop    'end of Process 385

        Dim strTotalContributionAmount As String = "00000000000" & ConvertToEBCDIC(totalContributionAmount)
        strTotalContributionAmount = strTotalContributionAmount.Substring(strTotalContributionAmount.Length - 11, 11)
        Dim strLoanRepaymentAmount As String = "00000000000" & ConvertToEBCDIC(loanRepaymentAmount)
        strLoanRepaymentAmount = strLoanRepaymentAmount.Substring(strLoanRepaymentAmount.Length - 11, 11)

        strOutput = "999COMBINED   002496    " & strRunDate & strRunTime & _
                     detailRecordCount.ToString("000000000") & " " & _
                     strTotalContributionAmount & " " & strLoanRepaymentAmount & Space(561)

        'Print it
        swOutput.WriteLine(strOutput)

        'close up shop
        sr114Input.Close()
        sr385Input.Close()
        swOutput.Close()

        Console.WriteLine(vbCrLf & vbCrLf & "normal Term @ " & strRunDate & " " & strRunTime & _
        " with " & detailRecordCount & " records, " & vbCrLf & _
        totalContributionAmount / 100 & " total contributions and " & vbCrLf & _
        loanRepaymentAmount / 100 & " total principal and interest repayments.")
        Return 0
    End Function
    Function ConvertToEBCDIC(ByVal lngNumber As Long) As String
        Dim negVal As Boolean
        Dim absNumber As Long

        If lngNumber < 0 Then
            negVal = True
            absNumber = -1 * lngNumber
        Else
            negVal = False
            absNumber = lngNumber
        End If

        Dim strNumber As String = absNumber.ToString()
        If negVal Then
            Select Case strNumber.Substring(strNumber.Length - 1, 1)
                Case 0
                    strNumber = strNumber.Substring(0, strNumber.Length - 1) & "}"
                Case 1
                    strNumber = strNumber.Substring(0, strNumber.Length - 1) & "J"
                Case 2
                    strNumber = strNumber.Substring(0, strNumber.Length - 1) & "K"
                Case 3
                    strNumber = strNumber.Substring(0, strNumber.Length - 1) & "L"
                Case 4
                    strNumber = strNumber.Substring(0, strNumber.Length - 1) & "M"
                Case 5
                    strNumber = strNumber.Substring(0, strNumber.Length - 1) & "N"
                Case 6
                    strNumber = strNumber.Substring(0, strNumber.Length - 1) & "O"
                Case 7
                    strNumber = strNumber.Substring(0, strNumber.Length - 1) & "P"
                Case 8
                    strNumber = strNumber.Substring(0, strNumber.Length - 1) & "Q"
                Case 9
                    strNumber = strNumber.Substring(0, strNumber.Length - 1) & "R"
                Case Else
                    Console.WriteLine(strNumber.Substring(strNumber.Length - 1, 1) & "invalid number in ConvertToEBCDIC")
                    Return -1
            End Select
        End If
    End Function
    Sub MoveIt(ByVal inpStartLoc As Integer, ByVal outLength As Integer, ByRef buildString As String)
        Dim intFrom As Integer = inpStartLoc - 1
        Dim intTo As Integer = intFrom + outLength - 1
        For k As Integer = intFrom To intTo
            buildString &= strInput(k)
        Next
    End Sub

    Function FormatIt(ByVal strInput As String) As String
        Dim strOutput As String = strInput.Trim.TrimStart("0")
        If strOutput.Length = 0 Then
            Return "0"
        Else
            Return strOutput
        End If
    End Function
End Module
0
Comment
Question by:VBBRett
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 9
20 Comments
 

Author Comment

by:VBBRett
ID: 16931943
Any takers?
0
 
LVL 12

Expert Comment

by:vb_jonas
ID: 16931955
Hi! I am not sure how you want your app to work, but making a dialog front for choosing the files for your main-sub could look like this:

Public Class Form1

' Three textboxes, (why four?), named textbox1, textbox2 and textbox3
' Four buttons, button1, button2, button3, button4
'

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim d As New OpenFileDialog
        d.Filter = "*.txt|*.txt"
        If d.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.TextBox1.Text = d.FileName
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim d As New OpenFileDialog
        d.Filter = "*.txt|*.txt"
        If d.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.TextBox2.Text = d.FileName
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim d As New SaveFileDialog
        d.Filter = "*.txt|*.txt"
        If d.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.TextBox3.Text = d.FileName
        End If
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim MyCmdArgs(2) As String
        MyCmdArgs(0) = TextBox1.Text
        MyCmdArgs(1) = TextBox2.Text
        MyCmdArgs(2) = TextBox3.Text
        Module2.Main(MyCmdArgs)
    End Sub
End Class
0
 
LVL 12

Expert Comment

by:vb_jonas
ID: 16931963
TextBox1 = sr114Input-path
TextBox2 = sr385Input-path
TextBox3 = swOutput-path

0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:VBBRett
ID: 16931968
Hi, this is what I did.  But for some reason, the form freezes up.  I don't know why, but it just does.
0
 

Author Comment

by:VBBRett
ID: 16931970
the last post that you did, would that be in the main function?
0
 
LVL 12

Expert Comment

by:vb_jonas
ID: 16931980
sorry, no, that was (probably an unnessesary) explanation of the textboxes,

This is what I did:
created a new win app
created a new module
pasted your code there
created the form as above.

When does it freeze?
0
 

Author Comment

by:VBBRett
ID: 16931983
Why am I getting an Argumentoutofrange exception is unhandled error message for the following line of code?

 strOutput &= strWorkField.Substring(strWorkField.Length = 6, 6)
0
 
LVL 12

Accepted Solution

by:
vb_jonas earned 500 total points
ID: 16931988
shouldnt the line be

strOutput &= strWorkField.Substring(strWorkField.Length - 6, 6)

?
0
 

Author Comment

by:VBBRett
ID: 16932020
VB_JONAS YOU ARE THE MAN!!!  This is where I have been having my problem for the past week!  YOU DESERVE THE WHOLE 500 POINTS..ENJOY!  Sorry for the CAPS, but I am so excited that it works now and that line has been staring me in the face since last week.
0
 
LVL 12

Expert Comment

by:vb_jonas
ID: 16932027
Great! Happy to have helped, thanks for the points!
0
 

Author Comment

by:VBBRett
ID: 16932057
It works fine when I have a variable assigning the file path to the textboxes when I start the program.  But when I put in the command for open file dialog boxes, the whole application freezes.
0
 
LVL 12

Expert Comment

by:vb_jonas
ID: 16932068
Im not sure I follow you, how do you mean "put in the command for open file dialog boxes?"
0
 
LVL 12

Expert Comment

by:vb_jonas
ID: 16932073
Do you say that when you choose files with the buttons, (which shows open/save-dialogs), then the app freezes?
0
 
LVL 12

Expert Comment

by:vb_jonas
ID: 16932078
Can you do a Ctrl+Break to see where it's frozen?
0
 
LVL 12

Expert Comment

by:vb_jonas
ID: 16932086
Perhaps you should set a break point at the first line in your Main-function and then step your way through the function to see where its hangs?
0
 

Author Comment

by:VBBRett
ID: 16932105
I will start over.  Could I add the module and have the form import the module almost like a class, or would it be better to make the module a class and then import it.  I want to do this as simply as possible, almost as if the code was in the form, I just don't want it to freeze up on me again.
0
 
LVL 12

Expert Comment

by:vb_jonas
ID: 16932125
Yes, just create a new class and insert your modulecode there, only have to change Module Module2 to Class ClassName and obvously End Module to End Class.

Then your call sub should look something like this:

        Dim MyCmdArgs(2) As String
        MyCmdArgs(0) = TextBox1.Text
        MyCmdArgs(1) = TextBox2.Text
        MyCmdArgs(2) = TextBox3.Text

        Dim c As New ClassName
        c.Main(MyCmdArgs)
0
 
LVL 12

Expert Comment

by:vb_jonas
ID: 16932132
But that really wont change much I think. Did you find out where the app is frozen?
0
 

Author Comment

by:VBBRett
ID: 16932145
No, I exported it as an application and then deleted it.  After I tried to restore it, it wasn't able to open it..so I'm just starting fresh.
0
 

Author Comment

by:VBBRett
ID: 16932350
I have another question.  How do I call a sub from another sub in a form?

I want to call the sub btnFileFind1_Click from mnuOpen_Click
 Private Sub mnuOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOpen.Click
        Dim filefindopen As String

    End Sub

 Private Sub btnFileFind1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFileFind1.Click

        Dim strFileName As String

        OpenFD.InitialDirectory = "C:\PRU401k"
        OpenFD.Title = "Open a text file"
        OpenFD.Filter = "Text Files(*.txt)|*******R.txt"
        OpenFD.ShowDialog()

        strFileName = OpenFD.FileName
        MsgBox(strFileName)
        TextBox1.Text = strFileName
    End Sub

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

696 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