Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27512

VS 2010 Writing formatted text to a text file

$
0
0
Hello,

This is a follow on post from the following posts I made earlier in March/April of this year.

These are my 4 earlier posts (in date order)

http://www.vbforums.com/showthread.p...ory&highlight=

http://www.vbforums.com/showthread.p...ile&highlight=

http://www.vbforums.com/showthread.p...ame&highlight=

http://www.vbforums.com/showthread.p...ong&highlight=

Here is the code I have so far

Code:

Public Class Form1

    Dim Changed As Boolean = False
    Dim CurrentFile As String = ""
    Dim extension As String = ""

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListView1.Columns.Add("FileName", 230, HorizontalAlignment.Left)
        ListView1.View = View.Details
        ListView1.CheckBoxes = True
    End Sub

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
        If ListView1.CheckedItems.Count > 0 Then
            MsgBox(String.Join(Environment.NewLine, ListView1.CheckedItems.Cast(Of ListViewItem).Select(Function(lvi) lvi.Tag.ToString).ToArray))
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'declare new FolderBrowserDialog + set it's initial properties
        Dim fbd As New FolderBrowserDialog With { _
                .Description = "Select location of Gerber files", _
                .ShowNewFolderButton = False, _
                .SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)}
        ' filter files by willcards (i.e. "*.ext")
        Dim extensions() As String = {"*.gb0", "*.gb1", "*.drl"}

        'show the dialog + display the result in the listview unless cancelled
        'If fbd.ShowDialog = DialogResult.OK Then
        'ListView1.Items.AddRange(Array.ConvertAll(IO.Directory.GetFiles(fbd.SelectedPath), Function(f) New ListViewItem(IO.Path.GetFileName(f)) With {.Tag = f}))
        'End If
        If fbd.ShowDialog = DialogResult.OK Then
            Dim files() As String = FileIO.FileSystem.GetFiles(fbd.SelectedPath, FileIO.SearchOption.SearchTopLevelOnly, extensions).ToArray
            ListView1.Items.AddRange(Array.ConvertAll(files, Function(f) New ListViewItem(IO.Path.GetFileName(f)) With {.Tag = f}))
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim strText As String = ""
        'Dim x As Integer
        Dim s As String = ""
        Dim file As System.IO.StreamWriter
        'Dim intC As Integer
        For Each checkedLVI As ListViewItem In ListView1.CheckedItems
            'Do what you want here
            'strText = s & "Checked Item " & (x + 1).ToString & " = " & ListView1.CheckedItems(x).ToString & ControlChars.CrLf
            strText += "Checked Item " & checkedLVI.Index & " = " & checkedLVI.Text & ControlChars.CrLf
        Next
        file = My.Computer.FileSystem.OpenTextFileWriter("MatrixMakerTest2_Output.fm6", True)
        file.WriteLine(strText)
        file.Close()
        'For intC = 0 To ListView1.Items.Count - 1
        'strText &= CStr(CheckedListBox1.Items.Item(intC)) & vbCrLf

        'Next

    End Sub

    Private Sub ListView1_ItemChecked(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles ListView1.ItemChecked
        For Each item As ListViewItem In ListView1.Items
            'If item.Text = e.Item.Text Then item.Checked = e.Item.Checked
            If String.Equals(IO.Path.GetFileNameWithoutExtension(item.Text),
                IO.Path.GetFileNameWithoutExtension(e.Item.Text),
                StringComparison.CurrentCultureIgnoreCase) Then
                item.Checked = e.Item.Checked
            End If
        Next
    End Sub

    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
        Changed = True
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        ' Displays an OpenFileDialog so the user can select a Cursor.
        Dim openFileDialog1 As New OpenFileDialog()
        openFileDialog1.Filter = "Matrix Files|*.fm6"
        openFileDialog1.Title = "Select a Matrix File"

        If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            ' Assign the cursor in the Stream to the Form's Cursor property.

            extension = openFileDialog1.FileName.Substring(openFileDialog1.FileName.LastIndexOf("."))
            'MsgBox("String variable extension contains: " & extension)

            If extension = ".fm6" Then
                RichTextBox1.Text = FileIO.FileSystem.ReadAllText(openFileDialog1.FileName)
               
                'MsgBox("String variable extension is .fm6 ") 'Not being reached: WHY?
            End If

        End If

    End Sub
End Class

This produces the following form where I save the files selected in a checkbox listview to a text file. I then open the text file.

Name:  MatrixMakerSaveSelectedGerberFileNamesToTextFile.JPG
Views: 1
Size:  114.1 KB

I then read back the selected file names saved in the text file back into an edit window.

Name:  ShowSelectedFilesInEditWindow.JPG
Views: 1
Size:  122.2 KB

So far so good.

First question.

How do I remove the "Checked Item?=" from the string saved in the text file?

My overall objective/aim

My eventual aim is to produce a text file with the following lines in it.

Code:

#Layer types needed are
#1/ TOP
#2/ BOTTOM
#3/ DRILL
#4/ BORDER

#Note: All FAB 3000 Import Matrix Files must end with *.fm6. 

#So for example the layers in the .fm6 matrix file will be

LAYER {

  TYPE=TOP

  NAME= top

  START_DRILL=

  END_DRILL=

  DRAW_COLOR=100,223,0

  FLASH_COLOR=100,223,0

}

LAYER {

  TYPE=BOTTOM

  NAME=bottom

  START_DRILL=

  END_DRILL=

  DRAW_COLOR=223,100,223

  FLASH_COLOR=223,100,223

}

LAYER {

  TYPE=DRILL

  NAME=drill

  START_DRILL=

  END_DRILL=

  DRAW_COLOR=0,0,192

  FLASH_COLOR=0,0,192

}

LAYER {

  TYPE=BORDER

  NAME=outline

  START_DRILL=

  END_DRILL=

  DRAW_COLOR=255,0,0

  FLASH_COLOR=255,0,0

}


#For the board_outline layer the TYPE can be changed to GRAPHIC and the DRAW_COLOR and FLASH_COLOR values experimented with.


#The JOBS are set up as follows (sixteen times if 16 pcb's are to be imported into FAB3K)

######################################################

#Job - 1
# Is for comments

JOB {

  NAME=board1

  LAYER=top,D:\CircuitWizardFiles\Serial_LCD\Serial_LCD_Board_FINAL.gb1

  #LAYER=bottom_layer,D:\CircuitWizardFiles\Serial_LCD\Serial_LCD_Board_FINAL.gb2 

  LAYER=drill,D:\CircuitWizardFiles\Serial_LCD\Serial_LCD_Board_FINAL.drl

  LAYER=outline,D:\CircuitWizardFiles\Serial_LCD\Serial_LCD_Board_FINAL.gb0 

}

The LAYER sections are initially going to be hard coded into the application to start with. So I want to write these sections of text to ALL the files I produce. This because the layers of the PCB I'm interested in will always stay the same.

The JOB section is where I put the filenames I have selected in the checkbox above. Each group of 3 (or so) files will be given a NAME will will appear in the NAME section.

The LAYER section/line is populated as above. File extension .gb1 is associated with lyer top,bottom with .gb2,drill with .drl and outline with .gb0

The names top,bottom, drill and outline will stay the same as will the layers used. The only thing that will change will be the filenames and name of the job (group of files).

This .fm6 file is used to load printed circuit board (PCB) gerber file data into a computer aided manufacturing package (CAM) for PCB production.

My next task

Could someone please help me to write some code that will write the LAYER sections (4 off TOP,BOTTOM,DRILL and BORDER) at the top of the MatrixMakerTest2_Output.fm6 file that I'm writing the text to. I do not know how to format the string that is written to the file properly.

How do I then search for the end of this LAYER section so that I can next build the JOB section of the text file using the files that I have selected in the checkbox above.

I am very grateful for the help given to me so far that has enabled me to make so much progress. I'm hopeing that I can get over this last stumbling block and then I should have enough to go the rest of the way myself.
Thanks :thumb:

David.
Attached Images
  

Viewing all articles
Browse latest Browse all 27512

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>