Reading a multiline textbox and wrtitng to text file

  • Thread starter Thread starter Suresh Kumaran
  • Start date Start date
S

Suresh Kumaran

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
 
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)
 
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

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,

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
 
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)
 
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

app
code generator)
.
 
Back
Top