473,387 Members | 1,876 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,387 software developers and data experts.

Going through user input (textbox) line by line

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

Similar topics

4
by: santa19992000 | last post by:
can I use scanf to get input (some times user enters input sometimes not, just hit keyboard)?. It displays to enter IP address, if user wants to change, then he enters, otherwise he hits keyboard,...
1
by: Jorge Ponte | last post by:
hi I have a Web User Control (ascx) - lets call it "My_WUC" - in a Web form. In that WUC I want have a textbox and a button. I want to click on the button and open a popup (I use javascript for...
4
by: Tim::.. | last post by:
Can someone please help.... I'm having major issues with a user control I'm tring to create! I an trying to execute a sub called UploadData() from a user control which I managed to do but for...
2
by: Tim::.. | last post by:
Can someone please help.... I'm having major issues with a user control I'm tring to create! I an trying to execute a sub called UploadData() from a user control which I managed to do but for...
0
by: Tim::.. | last post by:
Can someone please help.... I'm having major issues with a user control I'm tring to create! I an trying to execute a sub called UploadData() from a user control which I managed to do but for...
3
by: Tim::.. | last post by:
Can someone please help.... I'm having major issues with a user control I'm tring to create! I an trying to execute a sub called UploadData() from a user control which I managed to do but for...
0
by: Paulers | last post by:
hello VB masters I have an issue that I am hoping you can help me with. I am creating an application that accepts user input via a richtextbox. the user can enter multiple entries and I need to...
0
by: MikeY | last post by:
Hi everyone, I posted a question very early today, but maybe I wasn't articulate enough. Hopefully this will illicit a response I've created a windows form and a User Control. This is a...
15
by: Doogie | last post by:
I have a .net app that a user currently enters a number in a text box, hits a button and a data call is executed. She wants the ability to enter in multiple numbers (up to 100). So to make...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.