A way to add in a combobox 2 items and hide one

G

Gian Paolo

hi all
i'm looking for a way to add 2 items in a combobox in the same line and
hide one.
i'm working on the code above but it returns me only the last value i
entered and i do not know why....

*********** CLASS
using System;

using System.Collections.Generic;

using System.Text;

namespace Interventi.Client

{

public class clscombo

{

public string Name;

public int ReferenceID;

public void Add(string name, int referenceID)

{

this.Name = name; this.ReferenceID = referenceID;

}

public override string ToString()

{

return this.Name;

}

}

}



************** FORM

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace Interventi.Client.Forms

{

public partial class frmComuni : Form

{





public frmComuni()

{

InitializeComponent();

}

private void toolStripButton7_Click(object sender, EventArgs e)

{

this.Hide();

}

private void frmComuni_Load(object sender, EventArgs e)

{

clscombo combo = new clscombo();

combo.Add("pippo", 3);

cmbParametri.Items.Add(combo);

combo.Add("prova", 6);

cmbParametri.Items.Add(combo);

cmbParametri.SelectedIndex = 0;

}

private void toolStripButton1_Click(object sender, EventArgs e)

{



}

private void cmbParametri_SelectedIndexChanged(object sender, EventArgs e)

{


}

private void button1_Click(object sender, EventArgs e)

{

txtTesto.Text = String.Format("{0}",
((clscombo)cmbParametri.SelectedItem).ReferenceID);


}

}

}
 
M

Morten Wennevik

Hi Gian Paolo,

I'm not surprised it will only display the last item you add.

// here you create a new object of type clscombo and store the referenceto the object in 'combo'
clscombo combo = new clscombo();

// the clscombo object now has the name and id parts set to "pippo" and "3"
combo.Add("pippo", 3);

// you add a REFERENCE to the clscombo object to the ComboBox
cmbParametri.Items.Add(combo);

// here you OVERWRITE the previously "pippo" and "3" to "prova" and "6"
combo.Add("prova", 6);

// and add a REFERENCE to combo to the ComboBox. Note, this is the exact same reference as you previously added
cmbParametri.Items.Add(combo);

// You now have two identical items in your ComboBox. The fact that they appear as two difference items in the ComboBox list is due to how the ComboBox buffers the item display name, so even though the object is nowprova, it is displayed as pippo.


This is how I would do it

clscombo combo = new clscombo("pippo", 3);

cmbParametri.Items.Add(combo);

combo = new clscombo("prova", 6);

cmbParametri.Items.Add(combo);

cmbParametri.SelectedIndex = 0;

In fact I would probably have written it like this instead

cmbParametri.Items.Add(new clscombo("pippo", 3));
cmbParametri.Items.Add(new clscombo("prova", 6));



public class clscombo
{
public string Name;
public int ReferenceID;

public clscombo(string name, int referenceID)
{
this.Name = name; this.ReferenceID = referenceID;
}

public override string ToString()
{
return Name;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top