读取多行文本框和wrtitng到文本文件 [英] Reading a multiline textbox and wrtitng to text file

查看:126
本文介绍了读取多行文本框和wrtitng到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


有没有人知道VB.NET中的sytax将多行文本框的

内容写入文本文件?


感谢帮助。


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

推荐答案

" Suresh Kumaran <喾******** @ hotmail.com>在新闻中写道:1b0f01c3731c
"Suresh Kumaran" <Ku********@hotmail.com> wrote in news:1b0f01c3731c


a **************** @ phx.gbl:
a****************@phx.gbl:
大家好,

有人知道VB.NET中的sytax将多行文本框的内容写入文本文件吗?

感谢帮助。

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




你在任何.NET语言中都这样做,只有语法不同。

文本的来源也没有区别。编写字符串变量或类的字符串属性

(例如TextBox.Text)不同于




这是我工作的项目中的C#语法方法。我相信你可以转换语法来转换
。关键是为文件创建一个System.IO.StreamWriter

,然后调用Write(),Flush()和Close()方法。确保

你将一个Close()调用放在finally语句中,以确保在抛出异常的情况下调用



================================================= = ==============

public bool写(string destinationFile)

{

try

{

string sResult =" 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;}

终于

{

sw.Close();

}

返回true;

}

catch

{

返回false;

}

}

================================== ================ ==============

-

Michael Lang,MCSD

请参阅我的.NET开源项目
http://sourceforge.net/projects/colcodegen (简单代码生成器)
http://sourceforge.net/projects/dbobjecter(数据库应用程序代码生成器)
http://sourceforge.net/projects/ genadonet (通用) ADO.NET)



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)


" Suresh Kumaran" <ク******** @ hotmail.com>写在

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

我同意你的编码,但我的要求略有不同。我有一个多行文本框放在
表单中,在这个文本框中我正在阅读三个
字段如下:

txtbox.text = field1& crlf field2& crlf field3

这将在多行文本框中显示结果,其后如下:

result1
result2
result3

用户可以更改文本框中的值,如果他/她更改了值,我该如何写回文件
这些值出现在除了上面提到的文件中的三个字段之外?

希望你得到我想做的事情?

Suresh
Michael Lang< ml@nospam.com>在
新闻中写道:Xn ******************************** @ 207.46.248 .16:
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写(string destinationFile)
{
尝试
{
string sResult =" a sample来自某个地方的字符串" ;;
System.IO.StreamWriter sw;
sw = new System.IO.StreamWriter(destinationFile,
false,System.Text.Encoding.ASCII);
尝试
{sw / Write(sResult);
sw.Flush();
}
抓住{return false;}
终于 {
sw.Close();
}
返回true;
}
抓住
{
返回false;
}
}
======================================== ======== =========

-
Michael Lang,MCSD
================================================ =========
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




您的应用程序中是否有3个要写入的变量

a文件,或者想写文本框的内容!?在文本框中的数据来自何处,它并不重要。如果你想要一个文本

文件,当文本出现在文本框中时,请使用我已经显示的代码$




您是说要将文本框中的数据解析为应用程序中的三个
变量。这完全是另一个问题。


我猜你有:

1)使用一次一行读取一行文本文件 sw.ReadLine()"

2)将每一行保存到一个单独的变量中

3)将每个变量依次写入文本框。


这是这种情况吗?如果是这样,为什么不做sw.ReadToEnd()单个

字符串变量,然后直接把它放到文本框中?

你需要在一个单独的变量中的每一行的另一个原因是什么?如果是这样,那么,Rick说,你不应该让用户通过单个

文本框编辑它们。


您可以将一系列变量写入平面文本文件,每个变量分别在

a单独行中。我当然不是如何设计一个设置文件,

但你可以做到。 StreamWriter还有一个WriteLine(...)方法,以便
将一个字符串写入一行后跟一个换行符。


你应该真的拿看看StreamWriter上的帮助文件。


-

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

这篇关于读取多行文本框和wrtitng到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆