Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple code need to access file in text box

Status
Not open for further replies.

Shaun29

Programmer
Oct 22, 2008
53
US
I click a button open dialog displays, I select my file it displays in text box now I want to use tha selected file so i can stream write all contents to a text file when I click btn 2

here is the code:

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


OpenFileDialog1.Title = "Text Files To Transfer"
OpenFileDialog1.InitialDirectory = "C:temp"

OpenFileDialog1.ShowDialog()

End Sub

Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

Dim strm As System.IO.Stream
strm = OpenFileDialog1.OpenFile()
txtbox.Text = OpenFileDialog1.FileName.ToString()
If Not (strm Is Nothing) Then
'insert code to read the file data
strm.Close()
MessageBox.Show("Are You Ready To Transfer")



End If

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim RandomNumber As New Random()
Dim Tr As IO.TextReader = System.IO.File.OpenText("C:\Users\spritchard\Desktop\Bom\test.txt")
Dim FileLines() As String = Split(Tr.ReadToEnd(), vbCrLf)
Tr.Close()
Dim Textfile As String = FileLines(RandomNumber.Next(0, UBound(FileLines)))
MsgBox(Textfile)


End Sub
End Class

 
Well I want to click a button open dialog appears I select a file. then with another button I click it an it reads the file I selected, and writes the text from within the file to a specific text file I am using as a log. I got it to where I can open the file but when I press my transfer button the text in the actual text box displaying the file name and path is the only thing that gets transferred to my specific text file. ignore the above code here is what I am working with, my second button is where I am trying new things but nothing works I need to open the file path in the text box dialog so i can basically extract the text into another file. keep in mind I am using only text files.




______________________________________________________________



Public Class MainForm

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

openFileDialog1.InitialDirectory = "C:\"
OpenFileDialog1.Title = "Load File"
openFileDialog1.Filter = "Text Files (*.txt|*.txt"
OpenFileDialog1.FileName = ""



If OpenFileDialog1.ShowDialog() <> Windows.Forms.DialogResult.Cancel Then
txtSource.Text = OpenFileDialog1.FileName
Else
txtSource.Text = ""




End If


'Dim Tr As IO.TextReader = System.IO.File.OpenText("C:\users\spritchard\Destop\file 1.txt")
'Dim MyFileLine As String = Split(Tr.ReadToEnd(), vbCrLf)(2)
'Tr.Close()
'MsgBox(MyFileLine)




'Dim fileReader As String
'fileReader = My.Computer.FileSystem.ReadAllText(txtSource.Text)
'MsgBox(fileReader)






''


End Sub







Private Function SourceFileExist() As Boolean
If Not (System.IO.File.Exists(txtSource.Text)) Then
MessageBox.Show("The Source File Does Not Exist!")
Else
SourceFileExist = True


End If
End Function

Private Sub btnCopyFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopyFile.Click

'If Not (SourceFileExist()) Then Exit Sub
'MessageBox.Show("text transfered")
'Dim objText As New System.IO.StreamWriter("C:\Users\spritchard\Desktop\file 2.txt")
'objText.Write(fileReader)

'objText.Close()
'MessageBox.Show("Text Transfered")

'Dim fileWriter As String

Dim filereader As String
fileReader = My.Computer.FileSystem.ReadAllText(txtSource.Text)

MsgBox("Ready to transfer")
filereader = My.Computer.FileSystem.WriteAllText(txtSource.Text)
Dim objText As New System.IO.StreamWriter("C:\Users\spritchard\Desktop\file 2.txt")
'objText.WriteLine(sw)









If Not (SourceFileExist()) Then Exit Sub
MessageBox.Show("text transfered")


End Sub

End Class
 
Here's a working example. Add a new form, and paste the code into the form and run it. Modify as needed.

Code:
    Dim ofd As OpenFileDialog
    Dim WithEvents txtFile As TextBox
    Dim WithEvents btnOpen As Button
    Dim WithEvents btnWriteFile As Button
    Const LogFile As String = "C:\LogFile.txt"

    Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ofd = New OpenFileDialog
        ofd.Filter = "Text Files|*.txt"
        txtFile = New TextBox
        txtFile.Left = 12
        txtFile.Top = 24
        txtFile.Width = 160
        txtFile.ReadOnly = True
        Me.Controls.Add(txtFile)
        btnOpen = New Button
        btnOpen.Text = "..."
        btnOpen.Width = 24
        btnOpen.Height = Me.txtFile.Height
        btnOpen.Left = Me.txtFile.Left + Me.txtFile.Width + 4
        btnOpen.Top = Me.txtFile.Top
        Me.Controls.Add(btnOpen)
        AddHandler btnOpen.Click, AddressOf OpenFileButtonClick
        btnWriteFile = New Button
        btnWriteFile.Width = 60
        btnWriteFile.Text = "Write File"
        btnWriteFile.Top = Me.txtFile.Top + Me.txtFile.Height + 6
        btnWriteFile.Left = 12
        Me.Controls.Add(btnWriteFile)
        AddHandler btnWriteFile.Click, AddressOf WriteFileButtonClick
    End Sub

    Private Sub OpenFileButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.txtFile.Text = ""
        ofd.ShowDialog()
        Me.txtFile.Text = ofd.FileName
    End Sub

    Private Sub WriteFileButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If Not Me.txtFile.Text.Trim = "" Then
            'Read the existing file
            Dim FileText As String = ""
            FileText = System.IO.File.ReadAllText(Me.txtFile.Text)
            'Write to the log file
            System.IO.File.AppendAllText(LogFile, FileText)
            MessageBox.Show("Text copied.")
        Else
            MessageBox.Show("Please select a file.")
        End If
    End Sub
 
Thank you this rocks!!!! your a life saver
 
how would you select multiple files in ofd show dialog()?
 
You just need a few changes. You have to set MultiSelect on the OpenFileDialog to true, and use some looping contructs. You will use the FileNames array property of the OpenFileDialog instead of FileName. Here's an updated example with just a couple changes.

Code:
    Dim ofd As OpenFileDialog
    Dim WithEvents txtFile As TextBox
    Dim WithEvents btnOpen As Button
    Dim WithEvents btnWriteFile As Button
    Const LogFile As String = "C:\LogFile.txt"
    Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ofd = New OpenFileDialog
        ofd.Filter = "Text Files|*.txt"
        ofd.Multiselect = True
        txtFile = New TextBox
        txtFile.Left = 12
        txtFile.Top = 24
        txtFile.Width = 160
        txtFile.ReadOnly = True
        Me.Controls.Add(txtFile)
        btnOpen = New Button
        btnOpen.Text = "..."
        btnOpen.Width = 24
        btnOpen.Height = Me.txtFile.Height
        btnOpen.Left = Me.txtFile.Left + Me.txtFile.Width + 4
        btnOpen.Top = Me.txtFile.Top
        Me.Controls.Add(btnOpen)
        AddHandler btnOpen.Click, AddressOf OpenFileButtonClick
        btnWriteFile = New Button
        btnWriteFile.Width = 60
        btnWriteFile.Text = "Write File"
        btnWriteFile.Top = Me.txtFile.Top + Me.txtFile.Height + 6
        btnWriteFile.Left = 12
        Me.Controls.Add(btnWriteFile)
        AddHandler btnWriteFile.Click, AddressOf WriteFileButtonClick
    End Sub

    Private Sub OpenFileButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.txtFile.Text = ""
        ofd.ShowDialog()
        For Each s As String In ofd.FileNames
            Me.txtFile.Text &= s & ";"
        Next
        If Me.txtFile.Text.EndsWith(";") Then Me.txtFile.Text = Me.txtFile.Text.Substring(0, Me.txtFile.Text.Length - 1)

    End Sub

    Private Sub WriteFileButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If Not Me.txtFile.Text.Trim = "" Then
            'Read the existing file
            Dim FileText As String = ""
            For Each s As String In Me.txtFile.Text.Split(";")
                FileText &= System.IO.File.ReadAllText(s)
            Next
            'Write to the log file
            System.IO.File.AppendAllText(LogFile, FileText)
            MessageBox.Show("Text copied.")
        Else
            MessageBox.Show("Please select a file.")
        End If
    End Sub
 
how would i get it to write to an xls sheet?
 
but does that only select the A, O, L for say or the whole row A,O, L is in?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top