473,748 Members | 9,596 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 12747
"Suresh Kumaran" <Ku********@hot mail.com> wrote in news:1b0f01c373 1c
$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.Strea mWriter 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.Strea mWriter sw;
sw = new System.IO.Strea mWriter(destina tionFile,
false, System.Text.Enc oding.ASCII);
try
{
sw.Write(sResul t);
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********@hot mail.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.co m> wrote in
news:Xn******** *************** *********@207.4 6.248.16:
============= =============== =============== ==============
public bool Write(string destinationFile )
{
try
{
string sResult = "a sample string from somewhere";
System.IO.Strea mWriter sw;
sw = new System.IO.Strea mWriter(destina tionFile,
false, System.Text.Enc oding.ASCII);
try
{
sw.Write(sResul t);
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.ReadLin e()"
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********@hot mail.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.co m> wrote in
news:Xn******** *************** *********@207.4 6.248.16:
============ =============== =============== ============== =public bool Write(string destinationFile )
{
try
{
string sResult = "a sample string from somewhere"; System.IO.Strea mWriter sw;
sw = new System.IO.Strea mWriter (destinationFil e, false, System.Text.Enc oding.ASCII); try
{
sw.Write(sResul t);
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.ReadLin e ()"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********@hot mail.com> wrote in news:243b01c373 ef
$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 "TextChange d"
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********@hot mail.com> wrote in news:243b01c373 ef$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 "TextChange d"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********@hot mail.com> wrote innews:243b01c37 3ef
$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 "TextChange d"
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
16877
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 store in an array then a file. Perhaps this is the wrong control to use as there seems no way of referencing each line of the text box.
4
31062
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 seems to be working. Code like
7
4050
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 problem is that only the modified data from the SingleLine TextBoxes are returned. The original data are returned for the MultiLine TextBoxes. Also, if the page is sent to the server (for validation, for instance), only the modified data for the...
3
1887
by: Luqman | last post by:
How can I retrieve text from html file and write to textbox. Best Regards, Luqman
2
1863
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 (an endless loop). i thought that BinaryReader.ReadByte advanced to the next byte? i had it time out after 1000 iterations, and keeps outputting the same byte. any help appreciated, my code is below: Imports System.io
12
452
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
2402
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, which I am). I now want to read the text/values of those controls. I have found out that I can read the values if I wait until Page_Load, but then I have the same questions showing up again (along with the new questions) and I do not want them to....
7
15481
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 strings which I want to "paste" into a multiline textbox. Any ideas? -- Anil Gupte www.keeninc.net www.icinema.com
2
5830
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 things on separate lines in a TextBox without doing it programmatically is the following: <asp:TextBox ID="txtMultiLine" runat="server" TextMode="MultiLine"> Line 1 Line 2 Line 3 </asp:TextBox>
0
8830
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9541
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2782
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.