tags:

views:

51

answers:

1

Hi, i'm new to C#.

I need my program to show different parts of the data contained in a txt file into different listboxs (which are on different tabs of a form) so that the user can see the particular block of data they are interested in.

the data contained in the txt file looks like this:

G30:39:03:31 JG06
G32:56:36:10 JG04
G31:54:69:52 JG04
G36:32:53:11 JG05
G33:50:05:11 JG06
G39:28:81:21 JG01
G39:22:74:11 JG06
G39:51:44:21 JG03
G39:51:52:22 JG01
G39:51:73:21 JG01
G35:76:24:20 JG06
G35:76:55:11 JG01
G36:31:96:11 JG02
G36:31:96:23 JG02
G36:31:96:41 JG03

though much more of it :)

The separate listboxes will contain only the lines who's first integer pair matches that listbox's name. For example, all the lines that start "G32" will be added to the G32 listbox.

I think the code would start something like:

private void ReadToBox()
    {
        FileInfo file = new FileInfo("Jumpgate List.JG");
        StreamReader objRead = file.OpenText();
        while (!objRead.EndOfStream)

but i'm not sure where to start in terms of getting it sorted yet.

Any help? There's some rep in it for you :D

EDIT:

private void ViewForm_Load(object sender, EventArgs e)
    {
        this.PopulateListBox(lstG30, "G30", ("Jumpgate List.JG"));
        this.PopulateListBox(lstG31, "G31", ("Jumpgate List.JG"));
        this.PopulateListBox(lstG32, "G32", ("Jumpgate List.JG"));
        this.PopulateListBox(lstG33, "G33", ("Jumpgate List.JG"));
        this.PopulateListBox(lstG34, "G34", ("Jumpgate List.JG"));
        this.PopulateListBox(lstG35, "G35", ("Jumpgate List.JG"));
        this.PopulateListBox(lstG36, "G36", ("Jumpgate List.JG"));
        this.PopulateListBox(lstG37, "G37", ("Jumpgate List.JG"));
        this.PopulateListBox(lstG38, "G38", ("Jumpgate List.JG"));
        this.PopulateListBox(lstG39, "G39", ("Jumpgate List.JG"));
    }

    void PopulateListBox(ListBox lb, string prefix, string textfile)
    {
        string[] filestrings = textfile.Split(Environment.NewLine.ToCharArray());
        foreach(string line in filestrings)
        {
            if (line.StartsWith(prefix))
                lb.Items.Add(line);
        }
    }
+2  A: 

Some semi-pseudocode... A method you would call to populate each listbox. Specify the listbox control, the prefix you want to isolate, and the input file:

void PopulateListBox(ListBox lb, string prefix, string[] textfile)
{
    foreach(string line in textfile)
    {
        if (line.StartsWith(prefix))
        lb.Add(line);
    }
}

EDIT:

This method handles the file as a single string (rather than expecting a string array):

void PopulateListBox(ListBox lb, string prefix, string textfile)
{
    string[] filestrings = textfile.Split(Environment.NewLine.ToCharArray());
    foreach(string line in filestrings)
    {
        if (line.StartsWith(prefix))
        lb.Add(line);
    }
}
JYelton
I was thinking it would be something like that. doesn't that mean you would have to have a new method for each listbox? was hoping there was a quicker way.
Arcadian
No, you provide the name of the listbox you want to populate in the method call, so if you've named your listbox, for example `lstbG32`, then you would do this: `PopulateListBox(lstbG32, "G32", txt);`
JYelton
sorry, blonde moment there, i get it now. I get a guild error on the foreach part though saying cannot convert char to string
Arcadian
Ah, yes the foreach statement - the third (file) parameter would need to be a list or array of strings. If you're just passing it the raw file (a single string) then it is trying to iterate over the characters in that string. Hence you can't call .StartsWith on a character. I'll post an edit for this situation.
JYelton
wouldn't it be something like: `StringReader objReader = new StringReader(textfile); List<String> StringList = new List<String>(); do { StringList.Add(objReader.ReadLine()); } while (objReader.Peek() != -1);1`and then use objReader as the second parameter?
Arcadian
Or not then. so i just call the method for each listbox and it should display the data? If thats true, then could i simply put all the method calls into the form loading event so that the data loads when the form does?
Arcadian
I don't know how many listboxes you have to populate; I often create a method called `PopulateForm()` that I call at startup or when conditions calls for it (i.e. user loaded new settings). There, you would call `PopulateListBox(...)` for each listbox you have to populate. If it gets unwieldy, you may need to do things differently.
JYelton
Its not a large form, it only has 10 listboxes that need populating, and each of those is on its own tab in the form. i'll try it with the method calls in the form loading event and see if they populate.
Arcadian
ok when i tried it all the listboxes displayed blank. so i put a break point on one of the method calls and followed it through. it seems that the method doesn't take the first argument; when i hovered over lb it said lb|{SelectedItem = ""}. i'll post what i'm using so you can see it
Arcadian