Question Write text on .txt file wich i get out of a textbox, without knowing the path

mattijsstu

Member
Joined
Feb 19, 2013
Messages
15
Programming Experience
Beginner
hi

I have been searching the web for a whil and i am stuck in my program. I want to write a value of a textbox on a .txt file. Butch i don't know the path of it becos i have to choose it in the save dialog(wich i want to use ofcours). So how do i get my text in the file? this is my program so far:
VB.NET:
Public Class Form1
    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles SAVE.Click
        Dim myStream As IO.Stream
 
        Dim saveFileDialog1 As New SaveFileDialog()
 
        saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        saveFileDialog1.FilterIndex = 2
        saveFileDialog1.RestoreDirectory = True
 
        If saveFileDialog1.ShowDialog() = DialogResult.OK Then
            myStream = saveFileDialog1.OpenFile()
            If (myStream IsNot Nothing) Then
                myStream.Close()
            End If
        End If
 
    End Sub
 
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TextBox3.Text = Val(TextBox1.Text) ^ (2 / 3) * 50
    End Sub
End Class
 
Hi,

If you want to read and write Text files then it's better to use the StreamReader and StreamWriter classes since they were designed specifically for this purpose. Have a look at this example:-

VB.NET:
Imports System.IO
 
Public Class Form1
 
  Private Sub btnCalculate_Click(sender As System.Object, e As System.EventArgs) Handles btnCalculate.Click
    TextBox3.Text = (Val(TextBox1.Text) ^ (2 / 3) * 50).ToString
  End Sub
 
  Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
    Dim saveFileDialog1 As New SaveFileDialog()
 
    saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
    saveFileDialog1.FilterIndex = 2
    saveFileDialog1.RestoreDirectory = True
 
    If saveFileDialog1.ShowDialog() = DialogResult.OK Then
      'open a new file stream with your selected filename from the SaveDialog control
      Using myStream As New StreamWriter(saveFileDialog1.FileName.ToString)
        myStream.WriteLine(TextBox3.Text.ToString)
        myStream.Close()
      End Using
      MsgBox("Created File " & saveFileDialog1.FileName.ToString)
    End If
  End Sub
End Class

Notice how the StreamWriter is declared within a Using block. This is to ensure that all resources are released back to the system once the StreamWriter is no longer needed.

Hope that helps.

Cheers,

Ian
 
nice!

thanks this worked! Now i only have to be able to puth in my .txt "result='here goes the result from textbox3'" and i have do make a few more calculations(wich i know how to doe) just the part where i puth something infront of the result is a bit hard :(

edit: i added "result="& textbox3... and it worked :)

gonna puth al my formulas in the program now. when i am done i need to remake the prog to be able to puth it in a database(acces/excel) proberly gonna need help with that to. buth then i wil make a new post i think
 
Last edited:
i also get an erro while running it when i put numbers in and calculate. here is my code:
VB.NET:
Imports System.IO

Public Class Form1
    Dim result1 As String = ""
    Dim result2 As String = ""
    Dim result3 As String = ""
    Dim result4 As String = ""
    Dim result5 As String = ""
    Dim result6 As String = ""
    Dim result7 As String = ""
    Private Sub btnCalculate_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        result1 = 2.718281828 ^ ((3 / 7) * (Math.Log(Val(TextBox1.Text)) - Math.Log(57.7) - Math.Log(Val(TextBox2.Text))))
        result2 = 50 / (result1 ^ (2 / 3))
        result3 = 0.5 * (result1)
        result6 = result3 + result1
        result4 = (result6 * (Math.Cos(Val(TextBox7.Text)).ToString)) / 2
        result5 = result4 + Val(TextBox6.Text).ToString
        result7 = result1 * Val(textbox8.text)
        TextBox3.Text = result1
        TextBox4.Text = result2
        TextBox5.Text = result3


    End Sub


    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim saveFileDialog1 As New SaveFileDialog()


        saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        saveFileDialog1.FilterIndex = 2
        saveFileDialog1.RestoreDirectory = True


        If saveFileDialog1.ShowDialog() = DialogResult.OK Then
            'open a new file stream with your selected filename from the SaveDialog control
            Using myStream As New StreamWriter(saveFileDialog1.FileName)
                myStream.WriteLine("D=" & result1)
                myStream.WriteLine("n_max=" & result2)
                myStream.WriteLine("d=" & result3)
                myStream.WriteLine("hv=" & result4)
                myStream.WriteLine("V=" & result5)
                myStream.WriteLine("ht=" & result7)


                myStream.Close()
            End Using
            MsgBox("Created File " & saveFileDialog1.FileName)
        End If
    End Sub


End Class
AND the things i want to be able to let the user pick out of a table are the one in textbox 2 and the one in textbox 8. So there are basicly 2 tabel i wich to puth in my program buth i don't know how to do this. User just needs to be able to pick 1 value out of both tabels wich is the used in the calculation.
 
well i have a screenshot, it's in dutch buth it has somthing to do with a double, i don't get it becos it are all strings. This happens wen e run the programm, fill in the values and click the button to start the calculation:
Knipsel.JPG
 
Hi,

Firstly, I do not read Dutch, so my apologies for not having a clue as to what you are doing. That said, you need to turn Option Strict On since this will indicate to you where you are going to get type conversion errors in your routines.

With regards to mathematical calculations these should NEVER be done with String type variables. Each variable should be declared as the type it is expected to hold. i.e:-

Integer or Long for Whole numbers depending on the size of the number you need.

and

Single, Double, Decimal for floating point numbers depending on the precision and size of the number you need

Secondly, If you are getting a number from a TextBox then you should first of all cast it to the correct type of variable that you need as explained above using Integer.TryCast, Decimal.TryCast etc etc...

If you code your variables correctly you should find that these errors will disappear immediately.

Hope that helps,

Cheers,

Ian
 
i don't realy understand since i don't read vb.. I am new to this. I changed them to As decimal, now the program won't even run.
VB.NET:
Imports System.IO

Public Class Form1
    Dim Vijzeldiameter As Decimal = ""
    Dim Toerental As Decimal = ""
    Dim Balkdiameter As Decimal = ""
    Dim Vulhoogte As Decimal = ""
    Dim Vulpunt As Decimal = ""
    Dim result6 As Decimal = ""
    Dim Tegenwaterpeilhoogte As Decimal = ""
    Private Sub btnCalculate_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Vijzeldiameter = 2.718281828 ^ ((3 / 7) * (Math.Log(Val(TextBox1)) - Math.Log(57.7) - Math.Log(Val(TextBox2))))
        Toerental = 50 / (Vijzeldiameter ^ (2 / 3))
        Balkdiameter = 0.5 * Vijzeldiameter
        result6 = Balkdiameter + Vijzeldiameter
        Vulhoogte = (result6 * (Math.Cos(Val(TextBox7)))) / 2
        Vulpunt = Vulhoogte + Val(TextBox6)
        Tegenwaterpeilhoogte = Vijzeldiameter * Val(TextBox8)
        TextBox3.Text = Vijzeldiameter
        TextBox4.Text = Toerental
        TextBox5.Text = Balkdiameter


    End Sub


    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim saveFileDialog1 As New SaveFileDialog()


        saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        saveFileDialog1.FilterIndex = 2
        saveFileDialog1.RestoreDirectory = True


        If saveFileDialog1.ShowDialog() = DialogResult.OK Then
            'open a new file stream with your selected filename from the SaveDialog control
            Using myStream As New StreamWriter(saveFileDialog1.FileName)
                myStream.WriteLine("D=" & Vijzeldiameter)
                myStream.WriteLine("n_max=" & Toerental)
                myStream.WriteLine("d=" & Balkdiameter)
                myStream.WriteLine("hv=" & Vulhoogte)
                myStream.WriteLine("V=" & Vulpunt)
                myStream.WriteLine("ht=" & Tegenwaterpeilhoogte)


                myStream.Close()
            End Using
            MsgBox("Created File " & saveFileDialog1.FileName)
        End If
    End Sub


End Class
 
Back
Top