user3597342
user3597342

Reputation: 69

C# ComboBox columns

Assuming I have the following text file :

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

I'm trying to load it (the text above) on my ComboBoxEdit (should be same as simple ComboBox) , I'm doing that either on Form1_Load or on the ComboBoxEdit On_click and they both works just fine .

  {

        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());
            }
        }

As you can see I'm only loading the second cell which means the names "Rogue" , "Fighter" etc ... Now on SelectedIndexChanged I'm doing this :

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

It's not working and the PictureBox image is only showing the last line image (not matter what Item I choose from the ComboBox I choose , the image of the last line shows for all cases) .

Upvotes: 2

Views: 87

Answers (1)

The variable columns in your code currently holds only a row in every iteration of your for loop. So every time the for loop is completely executed, the columns variable will hold only the last processed row, that's why you only get the last picture. After you execute the for loop in you code, the columns variable will hold this data (corresponding to the last line in you file):

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

So every time the SelectedIndexChanged event handler is called, columns[2].ToString() will always return "common_mark_job_0045". What you want is a data structure like this, so that you can retrieve the right image for each row (and not only for the last row as before):

// 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"

To make the code work, you should save the columns for each row in you file, something like this:

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]);
}

Then, when the user changes the selected item in the combobox, use this event handler to retrieve the right image:

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

Upvotes: 1

Related Questions