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

Going through user input (textbox) line by line

P: n/a
Hi,

I have a little application that reads a text file line-by-line and
processes each line depending on the CVS values. Now I want to change my
program to capture this from a textbox. How do I parse a string
line-by-line? Just like a file?

Thanks
Maz.
Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies

P: n/a
Maziar,

You should refactor the logic part of your application so that your
function takes a TextReader. Once you have that, you can take your
FileStream and then wrap it in a StreamReader, and pass it to your function.
This way, if you want to pass the string contents to the function, you can
just pass the string through a StringReader instance and the function will
accept it (it acts like a stream, allowing you to read character after
character from the string you pass in).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Maziar Aflatoun" <ma***@rogers.com> wrote in message
news:Re********************@rogers.com...
Hi,

I have a little application that reads a text file line-by-line and
processes each line depending on the CVS values. Now I want to change my
program to capture this from a textbox. How do I parse a string
line-by-line? Just like a file?

Thanks
Maz.

Nov 17 '05 #2

P: n/a
Thanks... do you have an example? really stuck here.

Thanks
Maz
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP12.phx.gbl...
Maziar,

You should refactor the logic part of your application so that your
function takes a TextReader. Once you have that, you can take your
FileStream and then wrap it in a StreamReader, and pass it to your
function. This way, if you want to pass the string contents to the
function, you can just pass the string through a StringReader instance and
the function will accept it (it acts like a stream, allowing you to read
character after character from the string you pass in).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Maziar Aflatoun" <ma***@rogers.com> wrote in message
news:Re********************@rogers.com...
Hi,

I have a little application that reads a text file line-by-line and
processes each line depending on the CVS values. Now I want to change my
program to capture this from a textbox. How do I parse a string
line-by-line? Just like a file?

Thanks
Maz.


Nov 17 '05 #3

P: n/a
Maziar,

You could just do something like this:

public void Parse(TextReader reader)
{
// Read line by line here.
string line;

// Read.
while ((line = reader.ReadLine()) != null)
{
// Parse the line.

}
}

That's really all there is to it. You probably have something very
similar in your code that does this right now, you just have to make it work
with a TextReader.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Maziar Aflatoun" <ma***@rogers.com> wrote in message
news:Qv********************@rogers.com...
Thanks... do you have an example? really stuck here.

Thanks
Maz
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:%2****************@TK2MSFTNGP12.phx.gbl...
Maziar,

You should refactor the logic part of your application so that your
function takes a TextReader. Once you have that, you can take your
FileStream and then wrap it in a StreamReader, and pass it to your
function. This way, if you want to pass the string contents to the
function, you can just pass the string through a StringReader instance
and the function will accept it (it acts like a stream, allowing you to
read character after character from the string you pass in).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Maziar Aflatoun" <ma***@rogers.com> wrote in message
news:Re********************@rogers.com...
Hi,

I have a little application that reads a text file line-by-line and
processes each line depending on the CVS values. Now I want to change
my program to capture this from a textbox. How do I parse a string
line-by-line? Just like a file?

Thanks
Maz.



Nov 17 '05 #4

P: n/a
In article <Qv********************@rogers.com>,
Maziar Aflatoun <ma***@rogers.com> wrote:

: Thanks... do you have an example? really stuck here.

Here's a simple example:

public static void WriteFile(string path, string contents)
{
using (StreamWriter w = new StreamWriter(path))
w.Write(contents);
}

public static void LineByLine(TextReader reader)
{
Console.WriteLine("input type = " + reader.GetType());

int n = 1;
string line;
while ((line = reader.ReadLine()) != null)
Console.WriteLine(n++ + ". [" + line + "]");
}

static void Main(string[] args)
{
string text =
"The quick\n" +
"brown fox jumped over\n" +
"the lazy dog.";

string path = "foo.txt";
WriteFile(path, text);
using (StreamReader streamr = new StreamReader(path))
LineByLine(streamr);

TextBox textbox = new TextBox();
textbox.Text = text;
using (StringReader stringr = new StringReader(text))
LineByLine(stringr);
}

Hope this helps,
Greg
Nov 17 '05 #5

P: n/a
In article <11*************@corp.supernews.com>,
Greg Bacon <gb****@hiwaay.net> wrote:

: using (StringReader stringr = new StringReader(text))

Oops, that should be

using (StringReader stringr = new StringReader(textbox.Text))

but the point is the same.

Greg
Nov 17 '05 #6

P: n/a
Thank you so much :)

"Greg Bacon" <gb****@hiwaay.net> wrote in message
news:11*************@corp.supernews.com...
In article <Qv********************@rogers.com>,
Maziar Aflatoun <ma***@rogers.com> wrote:

: Thanks... do you have an example? really stuck here.

Here's a simple example:

public static void WriteFile(string path, string contents)
{
using (StreamWriter w = new StreamWriter(path))
w.Write(contents);
}

public static void LineByLine(TextReader reader)
{
Console.WriteLine("input type = " + reader.GetType());

int n = 1;
string line;
while ((line = reader.ReadLine()) != null)
Console.WriteLine(n++ + ". [" + line + "]");
}

static void Main(string[] args)
{
string text =
"The quick\n" +
"brown fox jumped over\n" +
"the lazy dog.";

string path = "foo.txt";
WriteFile(path, text);
using (StreamReader streamr = new StreamReader(path))
LineByLine(streamr);

TextBox textbox = new TextBox();
textbox.Text = text;
using (StringReader stringr = new StringReader(text))
LineByLine(stringr);
}

Hope this helps,
Greg

Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.