Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过拆分读取ComboBox上的.txt文件_C#_Windows_Combobox - Fatal编程技术网

C# 通过拆分读取ComboBox上的.txt文件

C# 通过拆分读取ComboBox上的.txt文件,c#,windows,combobox,C#,Windows,Combobox,我正在尝试读取DLL中的文本文件,但它只读取第一行(重复) 文本文件,如: 100 UserName1 Job 101 UserName2 Job 102 UserName3 Job 103 UserName4 Job 104 UserName5 Job 105 UserName6 Job 106 UserName7 Job 107 UserName8 Job try { Assembly assembly = Assembly.LoadFile(App

我正在尝试读取DLL中的文本文件,但它只读取第一行(重复)

文本文件,如:

100 UserName1   Job
101 UserName2   Job
102 UserName3   Job
103 UserName4   Job
104 UserName5   Job
105 UserName6   Job
106 UserName7   Job
107 UserName8   Job
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());
}
所以基本上我在我的组合框上复制了“UserName1”。(我只想读取第二个单元格,即用户名)

我的代码:

100 UserName1   Job
101 UserName2   Job
102 UserName3   Job
103 UserName4   Job
104 UserName5   Job
105 UserName6   Job
106 UserName7   Job
107 UserName8   Job
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());
}
试试看
{
Assembly=Assembly.LoadFile(Application.StartupPath+“/MyLists.dll”);
System.Resources.ResourceManager ResourceManager=新的System.Resources.ResourceManager(“ClassLibrary1.Properties.Resources”,程序集);
string[]strArrays15=resourcemanager.GetString(“作业列表”).Split('\t');
对于(int t=1;t<(int)strArrays15.Length;t++)
comboBox1.Items.AddRange(strArrays15[1].Split('\n'));
返回;
}
捕获(例外情况除外)
{
Show(例如ToString());
}

问题在您的循环中:

for (int t = 1; t < (int)strArrays15.Length; t++)
    comboBox1.Items.AddRange(strArrays15[1].Split('\n'));
另一种防止这种简单打字错误的书写方法是:

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

假设您的资源字符串具有
\n
作为换行符和
\t
作为行的列之间的分隔符,那么您的循环应该是

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());
}
试试看
{
Assembly=Assembly.LoadFile(Application.StartupPath+“/MyLists.dll”);
System.Resources.ResourceManager ResourceManager=新的System.Resources.ResourceManager(“ClassLibrary1.Properties.Resources”,程序集);
string[]strArrays15=resourcemanager.GetString(“作业列表”).Split('\n');
for(int row=0;row

应该添加一点错误检查,但是现在让我们假设您的资源字符串格式正确,并且在格式化方面没有任何意外

您正在使用
t
作为计数器进行循环,但绝不在循环中使用
t
。这有帮助吗?使用
comboBox1.Items.AddRange(strArrays15[t].Split('\n'))
而不是
comboBox1.Items.AddRange(strArrays15[1].Split('\n'))无效。它将加载所有这些文件。我只想加载第二个单元格。第一个拆分应该在\n不在\t上,第二个拆分应该在\t上不在\t上\n@user3748153你至少看到你的循环没有意义了吗?使用它不会有帮助。它会像这样加载100个UserName1作业101个UserName2作业等。。我只想把第二个牢房装点得像个符咒,格雷西亚斯:p