So I have problem with a code I am writing, when I write a string to a text document, I get the following error; "Conversion from string "1, 1, 2, 3, 5, 8, 13, 21, 34, 55" to type 'Integer' is not valid." Please help; code below. Thanks in advance!

Imports System.IO
Imports System.Numerics
Public Class Form1
    Dim path As String
    Dim clicked As Boolean
    Dim pastfib2 As BigInteger
    Dim pastfib As BigInteger
    Dim fib As BigInteger
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = "1, 1, "
        pastfib = 1
        pastfib2 = 1
        Timer1.Enabled = True
        clicked = True
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        fib = pastfib + pastfib2
        TextBox1.Text = TextBox1.Text & fib.ToString & ", "
        pastfib2 = pastfib
        pastfib = fib
        TextBox1.SelectionStart = TextBox1.TextLength
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked = True And clicked = True Then
            Timer1.Enabled = False
        Else
            Timer1.Enabled = True
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        clicked = False
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim myStream As Stream = Nothing
        Dim saveFileDialog1 As New SaveFileDialog()
        Dim text As String

        Text = TextBox1.Text
        saveFileDialog1.InitialDirectory = Apppath()
        saveFileDialog1.Filter = "text files (*.txt)|*.txt"
        SaveFileDialog1.FilterIndex = 2
        saveFileDialog1.RestoreDirectory = True
        If saveFileDialog1.ShowDialog() = DialogResult.OK Then
            myStream = saveFileDialog1.OpenFile()
            If (myStream IsNot Nothing) Then
                Dim ioFile As New StreamReader(myStream)
                Write(text) <---------------------------ERROR HERE
                ioFile.Close()
                myStream.Close()
            End If
        End If
    End Sub

    Public Function Apppath() As String
        Return System.AppDomain.CurrentDomain.BaseDirectory()
    End Function
End Class

Recommended Answers

All 2 Replies

Why use a "StreamReader" to "WRITE" to a File?
Have you tried using a Stream"WRITER"?

Dim ioFile As New StreamWriter(myStream)'<---------------------------ERROR WAS HERE
                ioFile.Write(text) '<---------------------------AND HERE
                ioFile.Close()

You don't need to use a savefiledialog box to save a string to a text file and the streamreader class reads from a file not writes to it try this

Imports System.IO
Public Class Form1

Private Sub Save()
Dim fileaccess as New StreamWriter("C:\FileLocation\FileName.txt")

fileaccess.WriteLine(TextBox1.Text)
fileaccess.Close()

End Sub

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

Save()

End Sub
this will save the contents of TextBox1 into a text document when you click the button

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.