By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,469 Members | 1,259 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,469 IT Pros & Developers. It's quick & easy.

Reading a multiline textbox and wrtitng to text file

P: n/a
Hi All,

Does anybody know the sytax in VB.NET to write the
contents of a multiline text box to a text file?

Appreciate help.

Suresh
Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Suresh Kumaran" <Ku********@hotmail.com> wrote in news:1b0f01c3731c
$a****************@phx.gbl:
Hi All,

Does anybody know the sytax in VB.NET to write the
contents of a multiline text box to a text file?

Appreciate help.

Suresh


You do it the same way in any .NET language, only the syntax differs.
There is also no difference in where the text comes from. It is no
different to write a string variable or a string property of a class
(such as TextBox.Text).

Here is C# syntax method from a project I worked on. I'm sure you can
convert the syntax. The key is to create a System.IO.StreamWriter for
the file, then call the Write(), Flush(), and Close() methods. Make sure
you put the Close() call in a finally statement to ensure it gets called
in the case of an exception being thrown.
================================================== ==============
public bool Write(string destinationFile)
{
try
{
string sResult = "a sample string from somewhere";
System.IO.StreamWriter sw;
sw = new System.IO.StreamWriter(destinationFile,
false, System.Text.Encoding.ASCII);
try
{
sw.Write(sResult);
sw.Flush();
}
catch{return false;}
finally
{
sw.Close();
}
return true;
}
catch
{
return false;
}
}
================================================== ==============

--
Michael Lang, MCSD
See my .NET open source projects
http://sourceforge.net/projects/colcodegen (simple code generator)
http://sourceforge.net/projects/dbobjecter (database app code generator)
http://sourceforge.net/projects/genadonet ("generic" ADO.NET)

Jul 19 '05 #2

P: n/a
"Suresh Kumaran" <ku********@hotmail.com> wrote in
news:26****************************@phx.gbl:
Michael,

I agree with your coding, but my requirement is slightly
diffrent. I have a multiline text box placed in the
form, Into this text box I am reading reading three
fields as follow :

txtbox.text = field1 & crlf field2 & crlf field3

This will show the result in the multiline text box one
after the other as follows.

result1
result2
result3

The user can change the values in the text box, if he/she
changes the values, how do I write back to the file
these values that appear one after the other to the three
fields in the file I mentioned above?

Hope you got what I am trying to do?

Suresh

Michael Lang <ml@nospam.com> wrote in
news:Xn********************************@207.46.248 .16:
================================================ =========
public bool Write(string destinationFile)
{
try
{
string sResult = "a sample string from somewhere";
System.IO.StreamWriter sw;
sw = new System.IO.StreamWriter(destinationFile,
false, System.Text.Encoding.ASCII);
try
{
sw.Write(sResult);
sw.Flush();
}
catch{return false;}
finally
{
sw.Close();
}
return true;
}
catch
{
return false;
}
}
================================================ =========

--
Michael Lang, MCSD


So do you have 3 variables in your application that you want to write to
a file, or do want to write the contents of the textbox!? It doesn't
matter where the data in the textbox comes from. If you want a text
file written as the text appears in the textbox, then use the code I've
shown.

Are you saying you want to parse the data in the textbox into three
variables in your application. That would be another question entirely.

I'm guessing that you have:
1) read a text file one line at a time using "sw.ReadLine()"
2) saved each line into a separate variable
3) wrote each variable in turn to the text box.

Is this the case? if so why not just do "sw.ReadToEnd()" into a single
string variable, and then put it directly into the textbox? Is there
another reason you need each line in a separate variable? If so then,
as Rick said, you should not let the user edit them all via a single
text box.

You can write a series of variables out to a flat text file with each on
a separate line. It certainly isn't how I would design a settings file,
but you can do it. The StreamWriter also has a WriteLine(...) method to
write a single string to a line followed by a newline character.

You should really take a look at the help files on StreamWriter.

--
Michael Lang, MCSD

Jul 19 '05 #3

P: n/a
Michael,

While I digest what you have said, let me ask something on
the same subject. Is there a possibility that we can set a
limitation to a multiline text box as to how many lines
can be keyed in the text box? Is there a property or
something we can set before hand.

Appreciate help

Suresh
-----Original Message-----
"Suresh Kumaran" <ku********@hotmail.com> wrote in
news:26****************************@phx.gbl:
Michael,

I agree with your coding, but my requirement is slightly diffrent. I have a multiline text box placed in the
form, Into this text box I am reading reading three
fields as follow :

txtbox.text = field1 & crlf field2 & crlf field3

This will show the result in the multiline text box one
after the other as follows.

result1
result2
result3

The user can change the values in the text box, if he/she changes the values, how do I write back to the file
these values that appear one after the other to the three fields in the file I mentioned above?

Hope you got what I am trying to do?

Suresh

Michael Lang <ml@nospam.com> wrote in
news:Xn********************************@207.46.248 .16:
=============================================== ========= =public bool Write(string destinationFile)
{
try
{
string sResult = "a sample string from somewhere"; System.IO.StreamWriter sw;
sw = new System.IO.StreamWriter (destinationFile, false, System.Text.Encoding.ASCII); try
{
sw.Write(sResult);
sw.Flush();
}
catch{return false;}
finally
{
sw.Close();
}
return true;
}
catch
{
return false;
}
}
=============================================== ========= =
--
Michael Lang, MCSD

So do you have 3 variables in your application that you

want to write toa file, or do want to write the contents of the textbox!? It doesn'tmatter where the data in the textbox comes from. If you want a textfile written as the text appears in the textbox, then use the code I'veshown.

Are you saying you want to parse the data in the textbox into threevariables in your application. That would be another question entirely.
I'm guessing that you have:
1) read a text file one line at a time using "sw.ReadLine ()"2) saved each line into a separate variable
3) wrote each variable in turn to the text box.

Is this the case? if so why not just do "sw.ReadToEnd()" into a singlestring variable, and then put it directly into the textbox? Is thereanother reason you need each line in a separate variable? If so then,as Rick said, you should not let the user edit them all via a singletext box.

You can write a series of variables out to a flat text file with each ona separate line. It certainly isn't how I would design a settings file,but you can do it. The StreamWriter also has a WriteLine (...) method towrite a single string to a line followed by a newline character.
You should really take a look at the help files on StreamWriter.
--
Michael Lang, MCSD

.

Jul 19 '05 #4

P: n/a
"Suresh kumaran" <ku********@hotmail.com> wrote in news:243b01c373ef
$e****************@phx.gbl:
Michael,

While I digest what you have said, let me ask something on
the same subject. Is there a possibility that we can set a
limitation to a multiline text box as to how many lines
can be keyed in the text box? Is there a property or
something we can set before hand.

Appreciate help

Suresh


There is a MaxLength property, but that controls how many characters can
be in the control, not how many lines.

You have to write your own line limiter code. Use the "TextChanged"
event to validate the text property to how many newline characters it can
contain. You could also use the KeyPress event. Look for the enter key,
and increment a counter each time they hit enter, when the limit is
reached don't allow another enter. The problem with KeyPress is that the
user could still paste text in that contains an enter.

I highly encourage you to just use different controls for each field.
Have a textbox for each field. When writing all these fields to a file,
use the WriteLine() method on the StreamWriter for each textbox value.
When reading the file at the app load, read one line at a time and put
the contents of that line into the correct textbox.

--
Michael Lang, MCSD
See my .NET open source projects
http://sourceforge.net/projects/colcodegen (simple code generator)
http://sourceforge.net/projects/dbobjecter (database app code generator)
http://sourceforge.net/projects/genadonet ("generic" ADO.NET)

Jul 19 '05 #5

P: n/a
Michael,

Thanks. I think that would be the best option.
Suresh

-----Original Message-----
"Suresh kumaran" <ku********@hotmail.com> wrote in news:243b01c373ef$e****************@phx.gbl:
Michael,

While I digest what you have said, let me ask something on the same subject. Is there a possibility that we can set a limitation to a multiline text box as to how many lines can be keyed in the text box? Is there a property or
something we can set before hand.

Appreciate help

Suresh
There is a MaxLength property, but that controls how

many characters canbe in the control, not how many lines.

You have to write your own line limiter code. Use the "TextChanged"event to validate the text property to how many newline characters it cancontain. You could also use the KeyPress event. Look for the enter key,and increment a counter each time they hit enter, when the limit isreached don't allow another enter. The problem with KeyPress is that theuser could still paste text in that contains an enter.

I highly encourage you to just use different controls for each field.Have a textbox for each field. When writing all these fields to a file,use the WriteLine() method on the StreamWriter for each textbox value.When reading the file at the app load, read one line at a time and putthe contents of that line into the correct textbox.

--
Michael Lang, MCSD
See my .NET open source projects
http://sourceforge.net/projects/colcodegen (simple code generator)http://sourceforge.net/projects/dbobjecter (database app code generator)http://sourceforge.net/projects/genadonet ("generic" ADO.NET)
.

Jul 19 '05 #6

P: n/a
Michael,

Do you have a sample code on what you have suggested?
Suresh

-----Original Message-----
Michael,

Thanks. I think that would be the best option.
Suresh

-----Original Message-----
"Suresh kumaran" <ku********@hotmail.com> wrote innews:243b01c373ef
$e****************@phx.gbl:
Michael,

While I digest what you have said, let me asksomething on the same subject. Is there a possibility that we canset a limitation to a multiline text box as to how manylines can be keyed in the text box? Is there a property or
something we can set before hand.

Appreciate help

Suresh


There is a MaxLength property, but that controls how

many characters can
be in the control, not how many lines.

You have to write your own line limiter code. Use

the "TextChanged"
event to validate the text property to how many newline

characters it can
contain. You could also use the KeyPress event. Look

for the enter key,
and increment a counter each time they hit enter, when

the limit is
reached don't allow another enter. The problem with

KeyPress is that the
user could still paste text in that contains an enter.

I highly encourage you to just use different controls

for each field.
Have a textbox for each field. When writing all these

fields to a file,
use the WriteLine() method on the StreamWriter for each

textbox value.
When reading the file at the app load, read one line at

a time and put
the contents of that line into the correct textbox.

--
Michael Lang, MCSD
See my .NET open source projects
http://sourceforge.net/projects/colcodegen (simple code

generator)
http://sourceforge.net/projects/dbobjecter (database

appcode generator)
http://sourceforge.net/projects/genadonet ("generic"

ADO.NET)

.

.

Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.