How i can moving object( that is include name and id) from listbox to another listbox and save it?
i wrote this:
if (lstActivity.SelectedIndex != -1)
{
int intSelectedIndex = lstActivity.SelectedIndex;
if (intSelectedIndex >= 0)
{
listbox.Items.Add(((Parking_Services.Activity)lstActivity.SelectedItem).ActivityName);
lstActivity.Items.RemoveAt(intSelectedIndex);
}
}
it is worked but when i want save this (after clicked button), it get exception : ” can not cast syste.string to (Parking_Services.Activity).”
private void btnSave_Click(object sender, EventArgs e)
{
int intActivityID;
Parking_Services.Service1 ii = new Parking_Services.Service1();
for (int i = 0; i <= listbox.Items.Count; i++) //save item from listbox is wrong
{
intActivityID = ((Parking_Services.Activity)listbox.Items[i]).ActivityID;
string strMessage = ii.AllowUserActivityByType(intUserTypeID, intActivityID, FrmLogin.intUserId);
}
The line…
…fails, because the items in
listboxareStrings and notParking_Services.Activitys. And they areStrings because, you addStrings in the line…I suppose the
ActivityNameproperty is aString. So there is a design error in either of those two lines.I suggest the following fix: Add the whole
Parking_Services.Activityobjects tolistbox……and override
ToString()in theParking_Services.Activityclass, solistboxdisplays them correctly:A
ListBoxalways callsToString()on theobjects passed to it, when painting their list entries. So, by overridingToString(), you can control how yourParking_Services.Activitys are displayed.