Multiple Text Boxes - Write to txt File on Same Line + More.

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1198 Views - Last Post: 06 February 2012 - 12:09 PM Rate Topic: -----

Topic Sponsor:

#16 ripgriggs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 25-January 12

Re: Multiple Text Boxes - Write to txt File on Same Line + More.

Posted 06 February 2012 - 11:10 AM

View PostnK0de, on 06 February 2012 - 10:47 AM, said:

what I meant by disabling boxes is to write a function like this to disable Textboxes after data is saved so that the user can't enter anymore data.

Public Function disableBoxes() As Integer

    txtCarrier.Enabled = False
    txtLocation.Enabled = False
        
    'all the other Textboxes

    Return Nothing

End Function


if its too much of a trouble, remove disableBoxes() from the code for now as I see you're not very familiar with Functions yet. Its not necessary for the program to run.



ok when i did that the program works great,

but it asks me for the load origin and destination and for the file name, everytime i click the save button.

instead of it just asking once..


other then that it works perfect.


what did i do wrong??


Imports System
Imports System.IO
Imports System.Text


Public Class Form1

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click

        'declare variables to hold the header details and the filename
        Dim loadOrigin, destination, deliverdate, filename, path As String
        Dim flag As Boolean = False


        loadOrigin = InputBox("Where is the Load Origin?", "Origin")
        destination = InputBox("Where is the Destination?", "Destination")
        deliverdate = InputBox("Where is the delivery date?", "Delivery date")
        filename = InputBox("What's the text file name?", "File Name")

        path = "C:\" + filename + ".txt"


        If File.Exists(path) Then
            MessageBox.Show("File already exists")
            Exit Sub    'always remember to put "Exit Sub" after the condition you do not want to proceed. It holds the program there. If not the execution will continue with the rest of the code even after you get the error message
        Else
            Dim fs As FileStream = File.Create(path)
            fs.Dispose()

            Dim writer As New StreamWriter(path, True)
            If flag = False Then
                writer.Write("Origin : " & Trim(loadOrigin) & vbTab & "-" & vbTab)
                writer.Write("Destination : " & Trim(destination) & vbTab & "-" & vbTab)
                writer.Write("Deliver Date : " & Trim(deliverdate) & vbTab & vbCrLf & vbCrLf & vbCrLf)

                flag = True
                'See the variable declaration part at the beginning. This flag variable make the header details to be 
                'written to the text file only once. It is initialized as False. So when it comes down here and this 
                'If statement checks if the True or false and since it is False, the header details get written to the 
                'file. And after that it switches to True. Then when you append another, it comes down here and checks 
                'with the If statement but now its value is True. So it skips writing header details once again.

            End If

            writer.Write(Trim(carrierbox.Text) & vbTab & "-" & vbTab)
            writer.Write(Trim(locationbox.Text) & vbTab & "-" & vbTab)
            writer.Write(Trim(destinationbox.Text) & vbTab & "-" & vbTab)
            writer.Write(Trim(quotebox.Text) & vbTab & "-" & vbTab)
            writer.Write(Trim(phonebox.Text) & vbTab & "-" & vbTab)
            writer.Write(Trim(faxbox.Text) & vbTab & "-" & vbTab)
            writer.Write(Trim(emailbox.Text) & vbTab & "-" & vbTab)
            writer.Write(Trim(datebox.Text) & vbTab & "-" & vbTab)
            writer.Write(Trim(notebox.Text) & vbTab & vbCrLf & vbCrLf)
            writer.Write(Trim(emptybox.Text) & vbTab & "-" & vbTab)

            writer.Close()
            MessageBox.Show("Text written to file", "Data Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
            clearFields()  'a function to clear out all the data from all the textboxes




        End If

    End Sub

    Public Function clearFields() As Integer

        'created a function to clear out fields
        carrierbox.Text = ""
        locationbox.Text = ""
        destinationbox.Text = ""
        quotebox.Text = ""
        phonebox.Text = ""
        faxbox.Text = ""
        emailbox.Text = ""
        datebox.Text = ""
        notebox.Text = ""
        emptybox.Text = ""

    End Function

End Class

Was This Post Helpful? 0
  • +
  • -

#17 nK0de  Icon User is offline

  • can't spell BITCH without IT
  • member icon

Reputation: 183
  • View blog
  • Posts: 732
  • Joined: 21-December 11

Re: Multiple Text Boxes - Write to txt File on Same Line + More.

Posted 06 February 2012 - 11:23 AM

Its because you have put those inputboxes inside the btnNew_Click event. So everytime you click that button, those inputboxes pop up.

Quote

To make the program ask the load origin and the destination only one time, you put that code inside the Form's Load event.


You haven't read my previous post. Study the code I've posted. The events where I've put the code.
Was This Post Helpful? 1
  • +
  • -

#18 ripgriggs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 25-January 12

Re: Multiple Text Boxes - Write to txt File on Same Line + More.

Posted 06 February 2012 - 11:27 AM

View PostnK0de, on 06 February 2012 - 11:23 AM, said:

Its because you have put those inputboxes inside the btnNew_Click event. So everytime you click that button, those inputboxes pop up.

Quote

To make the program ask the load origin and the destination only one time, you put that code inside the Form's Load event.


You haven't read my previous post. Study the code I've posted. The events where I've put the code.



i see,

i am not in a good spot to figure this out right now


i will probably play with it later this week when i get time away from work and school.


i really appreciate your help man!
Was This Post Helpful? 0
  • +
  • -

#19 nK0de  Icon User is offline

  • can't spell BITCH without IT
  • member icon

Reputation: 183
  • View blog
  • Posts: 732
  • Joined: 21-December 11

Re: Multiple Text Boxes - Write to txt File on Same Line + More.

Posted 06 February 2012 - 11:31 AM

No sweat. And if you have trouble understand things, DIC's VB.Net Resources Blog is a good place to start.
Was This Post Helpful? 1
  • +
  • -

#20 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 162
  • View blog
  • Posts: 563
  • Joined: 03-February 10

Re: Multiple Text Boxes - Write to txt File on Same Line + More.

Posted 06 February 2012 - 12:09 PM

You should really try not to use the InputBox function as this is hold over from VB6. You should be using a separate new form to gather this data, then return it to the main form to process it. This new form would benefit from the OpenFileDialogBox and FolderBrowserDialog classes.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2