0
votes

I added 3 items to the comboBox in the form, using the Items property. These items are: Item1, Item2, Item3.

When I select any of these 3 items in the comboBox, I want it to show a messagebox that contains the value of the first attribute of the corresponding object. For example when i click Item1, I want it to show me attribute "CNP1" from object a1, when I click Item2, to show me attribute CNP2 from object a2 and so on.

I think that I might connect each item in the comboBox with one of the 3 object created, not just write down these names(Item1,Item2,Item3) but I don't know how.

Also, these 3 items are created due to a class I created in the same project. I only have a class, a form and the main Program in this project.

So, how can I connect a comboBox Item to one of these objects, especially with only one attribute of that object. Thank you.

   using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IncercareEX2015
{
    public partial class PreluareDate : Form
    {
        ArrayList listaAbonati;
        AbonatTelefonic ab;

        public PreluareDate()
        {
            InitializeComponent();

            double[] vectMin = new double[4] { 12, 15, 50, 20 };
            AbonatTelefonic a1 = new AbonatTelefonic("CNP1", "Nume1", "Adresa1", "tel1", "tip1", vectMin);

            double[] vectMin3 = new double[2] { 100, 130 };
            AbonatTelefonic a3 = new AbonatTelefonic("CNP3", "Nume3", "Adresa3", "Tel3", "Tip3", vectMin3);

            double[] vectMin2 = new double[3] { 200, 80, 150 };
            AbonatTelefonic a2 = new AbonatTelefonic("CNP2", "Nume2", "Adresa2", "Tel2", "Tip2", vectMin2);

            ///GENERARE COLECTIE DE OBIECTE
            ArrayList listaAbonati = new ArrayList();
            listaAbonati.Add(a1);
            listaAbonati.Add(a3);
            listaAbonati.Add(a2);
            listaAbonati.Sort();

        }

    private void comboBox1_nume_SelectedIndexChanged(object sender, EventArgs e)
    {
        foreach (object o in listaAbonati)
            MessageBox.Show(o.ToString());
    }
}

}

3
Are you trying to add the items to the combobox? or showing attributes for an item already in the combobox?Isac
I added 3 items using the Items Property of the comboBox like this: Item1, Item2, Item3. I want that when I click Item1 to show me the first attribute of the object a1@IsacAnais
But if you added them through the designer you added strings instead of objects. Or did you add them in code?Isac

3 Answers

0
votes

Assuming that your code compiles and shows the o.ToString() when the selected index changes you would want to switch from using an ArrayList to a generic List, in your case a List which will allow you to access the properties of your entities without the need to casting in the event handler. Here's the relevant portion of your code:

List<AbonatTelefonic> listaAbonati;

public PreluareDate()
{
    ///GENERARE COLECTIE DE OBIECTE
    listaAbonati = new List<AbonatTelefonic>();
    listaAbonati.Add(a1);
    listaAbonati.Add(a3);
    listaAbonati.Add(a2);
    listaAbonati.Sort();

}

private void comboBox1_nume_SelectedIndexChanged(object sender, EventArgs e)
{
    foreach (AbonatTelefonic at in listaAbonati)
        MessageBox.Show(at.YourDesiredPropertyNameGoesHere);
}
0
votes

You can use SelectedIndex to get AbonatTelefonic. I hope it will help you.

private void comboBox1_nume_SelectedIndexChanged(object sender, EventArgs e)
{
    if (comboBox1_nume.SelectedIndex != -1)
    {
        AbonatTelefonic at = (AbonatTelefonic)listaAbonati[comboBox1_nume.SelectedIndex];
        MessageBox.Show(at.YourAttribute);
    }
}
0
votes

Add public override string ToString() on your AbonatTelefonic class and add the code return {first attrib variable};

Reference for you: https://msdn.microsoft.com/en-us/library/ms173154(v=vs.80).aspx

Hope this helps.