pinoyz

Hi i have a question about saving, i can do saving as textfiles something like this

dim write as new savefiledialog

dim writer as new system.io.streamwriter

write.filter = "text|*.txt"

write.title = "Save"

writer = system.io.file.appendtext(save.filename)

writer.Write(Text_Place.Text)

writer.close()

but my problem is instead of textfiles i want is to save it into a string so that i can place that string into richtextbox and send into mail...

regards




Re: Visual Basic Express Edition about saving to string?

Andrej Tozon

Hi,

try System.IO.StringWriter instead of StreamWriter.

Andrej






Re: Visual Basic Express Edition about saving to string?

Shasur

Do you want to open the file again and then get the contents. If that is the case you can use OpenFileDialog to open the file and store the contents of them to a string. Then you can show the string in your rich text box:

http://dotnetdud.blogspot.com/2007/06/openfiledialog-in-visual-basic-net.html

Regards

Shasur






Re: Visual Basic Express Edition about saving to string?

js06

Public Class Form1

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

Dim opendlg As New OpenFileDialog

opendlg.Filter = "Rich Text Files (*.rtf|*.rtf|All Files (*.*)|*.*"

If opendlg.ShowDialog() = Windows.Forms.DialogResult.OK Then

Me.RichTextBox1.LoadFile(opendlg.FileName)

End If

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Dim textstring As String

Dim opendlg As New OpenFileDialog

opendlg.Filter = "Text Files (*.txt|*.txt|All Files (*.*)|*.*"

If opendlg.ShowDialog() = Windows.Forms.DialogResult.OK Then

textstring = My.Computer.FileSystem.ReadAllText(opendlg.FileName)

Me.RichTextBox1.Text = textstring

End If

End Sub






Re: Visual Basic Express Edition about saving to string?

pinoyz

Hi jeff,

what i mean is to save the contents of textboxes & richtextboxes into string/ temp, instead as text file, and read this string and show it in a richtextbox.

Dim Save As New SaveFileDialog()
Dim MyStreamWriter As System.IO.StreamWriter
If NewFile = True Then
Save.Filter = "Plain Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
Save.CheckPathExists = True
Save.Title = "Save"
Save.ShowDialog(Me)
Try
MyStreamWriter = System.IO.File.AppendText(Save.FileName)
MyStreamWriter.Write(Text_Place1.Text)

MyStreamWriter.Write(Text_Place2.Text)

MyStreamWriter.Write(Text_Place3.Text)

MyStreamWriter.Write(Text_Place4.Text)

MyStreamWriter.Write(Text_Place5.Text)

MyStreamWriter.Flush()

MyStreamWriter.Close()

Instead saving in textfiles, i want it to save it as string(global) so that all contents of textbox will be place on a richtextbox...






Re: Visual Basic Express Edition about saving to string?

js06

You are over thinking this one i believe

Have you tried something like this :

Module Module1

Public combinestring As String

End Module

Public Class Form1

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

combinestring = Me.TextBox1.Text & vbCrLf & Me.TextBox2.Text & vbCrLf _

& Me.TextBox3.Text & vbCrLf & vbCrLf _

& Me.TextBox1.Text & " " & Me.TextBox2.Text & " " _

& Me.TextBox3.Text

Me.RichTextBox1.Text = combinestring

End Sub

End Class

This will combine the strings and then you can use them how you wish in the richtextbox

the string will be global to any part of your app if declared in a module

It won't work at the form class level






Re: Visual Basic Express Edition about saving to string?

pinoyz

Hi jeff,

sorry for noob question anyway how about the output is this

mytext1

mytext2

mytext3

mytext4

instead of

mytext1 mytext2 mytext3 mytext4

------------------------------

another problem

contents are in the databases and save it as textfiles, i want is to save this into string and place it ina richtextbox and send it into email ('send as email already done)

For i = 0 to 9

MyStreamWriter.Writeline(Text_Place.Text)

Next i

to your example the the string may overwrite the existing string

regards






Re: Visual Basic Express Edition about saving to string?

pinoyz

In this code it save it as textfiles i want is to save this as string so that i can use that string globally...

@ Jeff,

because there's a loop, probably the string may overwrite the existing string.. pls help

Regards

Here's my code:

Code Snippet

Dim com as new sqlclient.sqlcommand("Select * from Table1",conn)
dim adapter as new sqlclient.dataadapter(com)
Dim dt as new datatable()
Dim ds as new dataset()
dim bind as new bindingsource
dim cnt as new integer

com.connection.open
adapter.fill(ds)
adapter.fill(dt)

cnt = ds.tables().rows().count

textbox1.databindings.clear()
textbox1.databindings.add("text",bind,"content",true)

dim cntr as new integer

Dim save as new savefiledialog
Dim MyStreamWriter As System.IO.StreamWriter
save.filter = "text file|*.txt"
save.title = "Save"

If save.showdialog = windows.form.dialogresult.ok then

for cnter 0 to cnt - 1
MyStreamWriter = System.IO.File.AppendText(Save.FileName)
MyStreamWriter.Writeline(Text_Place.Text)

bind.movenext

next cnter

End if

MyStreamWriter.close

bind.movefirst

com.connection.close






Re: Visual Basic Express Edition about saving to string?

js06

About your first question,

this will give you both ways

combinestring = Me.TextBox1.Text & vbCrLf & Me.TextBox2.Text & vbCrLf _

& Me.TextBox3.Text & vbCrLf & vbCrLf _

& Me.TextBox1.Text & " " & Me.TextBox2.Text & " " _

& Me.TextBox3.Text

the vbcrlf is a carriage return (like hitting enter) (a new line)

If you try out the full code in the other post you will see it populate the rtb both ways

For the second part,

Are you trying to loop through the records and get the text from the bound textboxes as you loop through them

Since it will overwrite the string each time, is that why you wanted to use the richtextbox

You want to loop through adding the combined string of each record into the rtb

And then you will save the complete text from the rtb into a textfile and then email it

Something like this:

combine strings from first record with all textboxes (5 columns)

then place combined string into rtb

then move to next record

combine again and add to rtb on a new line after the first record

loop through all records

then save as textfile from rtb and then email

am i following you






Re: Visual Basic Express Edition about saving to string?

pinoyz

i'll try this one

thanks