473,386 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Reading a multiline textbox and wrtitng to text file

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
6 12688
"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
"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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: David Gray | last post by:
Greetings all, I'm working on a program that allows a user to enter notes in a multiline textbox. I would like to be able to read the contents of the textbox (as records - one per line) and...
4
by: Michael C | last post by:
Hi all, I'm trying to add lines to a multiline textbox. Here's what I'd like to end up with in the textbox (as an example): Line1 Line2 Line3 I've tried a couple of methods but nothing...
7
by: Joel Finkel | last post by:
Folks, I have a form that has several TextBoxes, some of which have the TextMode set to MultiLine. Each is pre-loaded with data from a database. The user is allowed to modify each entry. The...
3
by: Luqman | last post by:
How can I retrieve text from html file and write to textbox. Best Regards, Luqman
2
by: Mad Scientist Jr | last post by:
i'm trying to read a file byte by byte (and later alter the data and write it to a 2nd file byte by byte) and running into a problem where it seems to keep reading the same byte over and over again...
12
by: Suresh Kumaran | last post by:
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
0
by: Mike Collins | last post by:
I someone can please help, I am about at an end in trying to figure this out. I am adding some dynamic controls to my page (I found out that I was supposed to be doing that in the oninit event,...
7
by: Anil Gupte | last post by:
I have read a lot about getting lines from a multiline textbox in VB.Net. However, I cannot for the life of me figure out how to write to a multiline textbox. Basically, I have created an array of...
2
by: Nathan Sokalski | last post by:
I have a multiline TextBox that I want to display the text used to create a control in an apsx file. I want each of these to be on a separate line in the TextBox. The only way I know of to place...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.