Ecco come ho risolto:
Codice PHP:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private class Elemento {
public Elemento(string Nome,string ID){
this.Nome = Nome;
this.ID = ID;
}
private string nome;
public string Nome {
get {
return nome;
}
set {
nome = value;
}
}
private string id;
public string ID {
get {
return id;
}
set {
id = value;
}
}
public override string ToString() {
return Nome;
}
}
private void Form1_Load(object sender, EventArgs e) {
Elemento ele1 = new Elemento("Gianluigi", "1");
Elemento ele2 = new Elemento("Mario", "2");
Elemento ele3 = new Elemento("Matteo", "3");
listBox1.Items.Add(ele1);
listBox1.Items.Add(ele2);
listBox1.Items.Add(ele3);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {
//Mi stampa l'id anche se non è visualizzato nella listbox, poiche non contenuto nella stringa restituita
//da ToString.
MessageBox.Show(((Elemento)listBox1.SelectedItem).ID);
}
}