2016-01-02 48 views
2

假设我有以下的文本文件:C#组合框列

100 Rogue common_mark_job_0018 
101 Fighter common_mark_job_0019 
102 Kahuna common_mark_job_0020 
103 Spell Singer common_mark_job_0026 
110 Champion common_mark_job_0022 
111 Archer common_mark_job_0023 
112 Druid common_mark_job_0024 
113 Battle Kahuna common_mark_job_0031 
114 Evoker common_mark_job_0032 
120 Berserker common_mark_job_0027 
121 Marksman common_mark_job_0029 
122 Magus common_mark_job_0028 
123 War Kahuna common_mark_job_0030 
124 Beast Master common_mark_job_ga_2summon01 
200 Guide common_mark_job_0033 
201 Holy Warrior common_mark_job_0002 
202 Cleric common_mark_job_0003 
203 Breeder common_mark_job_0004 
210 Knight common_mark_job_0034 
211 Soldier common_mark_job_0035 
212 Bishop common_mark_job_0006 
213 Priest common_mark_job_0007 
214 Soul Breeder common_mark_job_0009 
220 Templar common_mark_job_0005 
221 Mercenary common_mark_job_de_2summon01 
222 Cardinal common_mark_job_0008 
223 Oracle common_mark_job_0037 
224 Master Breeder common_mark_job_0039 
300 Stepper common_mark_job_0040 
301 Strider common_mark_job_0010 
302 Dark Magician common_mark_job_0011 
303 Sorcerer common_mark_job_0012 
310 Assassin common_mark_job_0013 
311 Shadow Hunter common_mark_job_0014 
312 Chaos Magician common_mark_job_0015 
313 Warlock common_mark_job_0016 
314 Battle Summoner common_mark_job_0017 
320 Slayer common_mark_job_0041 
321 Deadeye common_mark_job_0042 
322 Void Mage common_mark_job_0043 
323 Corruptor common_mark_job_0044 
324 Overlord common_mark_job_0045 

我试图加载它(上面的文字)我ComboBoxEdit(应该是简单组合框一样),我做了无论是在Form1_Load上还是在ComboBoxEdit On_click上,它们都可以正常工作。

{ 

     if (comboBoxEdit2.Text != string.Empty) 
     { 

     } 
     else 
     { 
      ComboBoxItemCollection coll = comboBoxEdit2.Properties.Items; 
      coll.BeginUpdate(); 
      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++) 
       { 
        columns = strArrays15[row].Split('\t'); 
        // comboBoxEdit2.Items.Add(columns[1]); 
        coll.Add(columns[1]); 

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

正如你可以看到,我只加载第二小区,这意味着名“流氓”,“战士”等... 现在上的SelectedIndexChanged我这样做:

private void comboBoxEdit2_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     pictureEdit3.Image = new Bitmap(Application.StartupPath + "/jpg/" + columns[2].ToString() + ".jpg"); 
    } 

它不工作,PictureBox图片只显示最后一行图片(不管我从我选择的ComboBox中选择哪个项目,最后一行的图片显示所有情况)。

回答

1

代码中的变量columns当前在您的for循环的每次迭代中只保留一行。因此,每当for循环完全执行时,columns变量将仅保存最后处理的行,这就是为什么您只能得到最后一张照片。你在你的代码执行for循环后,columns变量将保持这个数据(对应于最后一行在你的文件):

columns[0] = "324" 
columns[1] = "Overlord" 
columns[2] = "common_mark_job_0045" 

所以每次SelectedIndexChanged事件处理程序被调用时,columns[2].ToString()总是返回"common_mark_job_0045" 。你想要的是一个数据结构,这样,这样就可以检索每个行权图像(而不是只在最后一排为前):

// Columns of first row 
columns[0][0] = "110" 
columns[0][1] = "Champion" 
columns[0][2] = "common_mark_job_0022" 

// Columns of second row 
columns[1][0] = "111" 
columns[1][1] = "Archer" 
columns[1][2] = "common_mark_job_0023" 

为了使代码工作,你应该保存在你的每一行的列文件,像这样:

string[][] columns; 

... 

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

for (int row = 0; row < strArrays15.Length; row++) 
{ 
    columns[row] = strArrays15[row].Split('\t'); 
    // comboBoxEdit2.Items.Add(columns[row][1]); 
    coll.Add(columns[row][1]); 
} 

然后,当用户在组合框中更改所选的项目,使用此事件处理程序,以获取正确的图像:

private void comboBoxEdit2_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    pictureEdit3.Image = new Bitmap(Application.StartupPath + "/jpg/" + 
         columns[comboBoxEdit2.SelectedIndex][2].ToString() + ".jpg"); 
} 
+0

感谢喜为你的答案,当我调试我有一个问题的行277意味着“coll.Add(列[行] [1]);”“代码。 – user3597342

+0

究竟是什么错误?你是否已将'columns'变量类型改为'string [] []'? –

+0

我不确定为什么在第二行(在你发布的代码上)有两个[] [],因为它没有意义。除此之外,我将列声明为string []列;在我的应用程序的顶部是。并且错误是这样的: Rappelz ~~ GM Tool.exe中出现类型'System.IndexOutOfRangeException'的未处理异常 附加信息:索引超出了数组的范围。 – user3597342