user3748153
user3748153

Reputation: 13

Reading .txt file on ComboBox by splitting

I'am trying to read from a textfile which is on my DLL but It only read first line (Duplicated)

Text file like :

100 UserName1   Job
101 UserName2   Job
102 UserName3   Job
103 UserName4   Job
104 UserName5   Job
105 UserName6   Job
106 UserName7   Job
107 UserName8   Job

So basically I'am getting "UserName1" duplicated on my ComboBox . (I want only to read the second cell. which means Usernames)

my code :

try
{
    Assembly assembly = Assembly.LoadFile(Application.StartupPath + "/MyLists.dll");
    System.Resources.ResourceManager resourcemanager = new System.Resources.ResourceManager("ClassLibrary1.Properties.Resources", assembly);

    string[] strArrays15 = resourcemanager.GetString("JobList").Split('\t');

    for (int t = 1; t < (int)strArrays15.Length; t++)
        comboBox1.Items.AddRange(strArrays15[1].Split('\n'));

    return;

}
catch (Exception ex )
{
    MessageBox.Show(ex.ToString());
}

Upvotes: 0

Views: 199

Answers (2)

Steve
Steve

Reputation: 216293

Supposing that your resource string has \n as newline delimiter and \t as delimiter between columns of a line then your loop should be

try
{
    Assembly assembly = Assembly.LoadFile(Application.StartupPath + "/MyLists.dll");
    System.Resources.ResourceManager resourcemanager = new System.Resources.ResourceManager("ClassLibrary1.Properties.Resources", assembly);

    string[] strArrays15 = resourcemanager.GetString("JobList").Split('\n');

    for (int row = 0; row < strArrays15.Length; row++)
    {
        string[] columns = strArrays15[row].Split('\t')
        comboBox1.Items.Add(columns[1]);
    }
    return;
}
catch (Exception ex )
{
    MessageBox.Show(ex.ToString());
}

A bit of error checking should be added but for now let's pretend that your resource string is well formed and there are no surprises in the formatting

Upvotes: 1

Ian
Ian

Reputation: 34489

The issue is in your loop:

for (int t = 1; t < (int)strArrays15.Length; t++)
    comboBox1.Items.AddRange(strArrays15[1].Split('\n'));

You're always using strArrays15[1] which uses the first item. Change to use the t which your incrementing instead:

for (int t = 1; t < (int)strArrays15.Length; t++)
    comboBox1.Items.AddRange(strArrays15[t].Split('\n'));

Another way of writing this which would prevent this simple typo is:

foreach(String item in strArrays15)
   comboBox1.Items.AddRange(item.Split('\n'));

Upvotes: 1

Related Questions