Jump to content

C# windows form problem

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
zkil_cc

zkil_cc

    Newbie

  • Members
  • Pip
  • 3 posts
hello guys, its nice to belong in this forum.
please help me, i do not know what to do next in my program...

i am just noob in C#, i have an understanding on some basics of Java lagnguage but i decided to switch to C#... so i do not totally understand what is going on here...

private void myMenuToolStripMenuItem_Click(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog open = new OpenFileDialog();
open.InitialDirectory = "c:\\";
open.Filter = "Text File|*.txt";
if (open.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = open.OpenFile()) != null)
{
using (myStream)
{
//////////////////////////////////////////////////////////////
//this is my problem... what i plan to do here is to get strings from a text
//file, inside the text file are some information of persons separated by
//this symbol "|"(every line is a new person), then save it
//(somewhere) so that the information of a person will be available to
// display on a textbox in the windows form.

//inside the text file is:
////////////////////////////////////////////////////////////////////////////
//Jack|Jack Black|10-20-2010|balck.com //
//Harry|Harry Potter|01-10-1985|harrypotter.com //
//Jo|Joshua White|02-21-1990|white.com //
//and many others.... //
/////////////////////////////////////////////////////////////////////////////

// Im stuck here, i dont really know what to do next...
StreamReader reader = new StreamReader(open.FileName);
string[] content = reader.ReadLine().Split(new char[] { '|' });
}

thanks in advance...

#2
cdg10620

cdg10620

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 389 posts
You need to write a method that is a custom parser for that type of information. Have a static splitter string that matches "|" and parse the text as you read it. You can use the String.split method and get an array of the items and then populate the text boxes accordingly. You will need to create a parser class and then new up an instance of that parser in your code behind file.

If you need more information let me know.
-CDG10620
Software Developer

#3
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
Splitting a string based on a special character:
string x = "item1|item2|item3|item4";
string[] a = x.Split("|");
//a will contain: 
//a[1] - item1
//a[2] - item2
//etc...
Just make a method that does you job. Where exactly do your compiler returns an error?
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#4
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts

Quote

Im stuck here, i dont really know what to do next...
You said you wanted to display it on the form. You don't know how to do that or what?
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#5
zkil_cc

zkil_cc

    Newbie

  • Members
  • Pip
  • 3 posts
yup, i do not know how to write the code... just like what i said, im not that experienced in programming...:confused:

Quote

Code:
string x = "item1|item2|item3|item4";
string[] a = x.Split("|");
//a will contain:
//a[1] - item1
//a[2] - item2
//etc...
i think this will do the job but what if i want to assign one line of information to a specific person(see the content of the sample text file), what i mean is...

the first line of string will be assigned to x:
string x = "item1|item2|item3|item4";
string[] a = x.Split("|");

the second line of text will be assigned to y:
string y = "item1|item2|item3|item4";
string[] b =y.Split("|");

and so on...
Is that possible? so that i can print out item1 of a or item2 of b...?

#6
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
//ReadToEnd() or ReadAllLines(), I forgot...
foreach (string line in x.ReadToEnd())
{
     string[] a = x.Split("|");
     //Do stuff with a
}
Or, you can create an array with string arrays.
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics