forwheeler

I have a listbox that I want to add a customer object to. This is my test code.

private void FillCustomerListbox()

{

Customer c = new Customer();

lbCustomers.Items.Clear();

lbCustomers.BeginUpdate();

c.CustomerID = 40;

c.FName = "test";

lbCustomers.Items.Add(c);

lbCustomers.EndUpdate();

}

This will display as a Customer object in the listbox.

I want to be able to display the FName as the item in the listbox and use the CustomerID as the value. I also want to be able to drag the customer to another listbox.

I'm not sure how to do this since I normally don't work on windows forms.

Thanks.



Re: Windows Forms General adding customer object to listbox

Rong-Chun Zhang - MSFT

Hi forwheeler,

Based on your post, you want the ListBox show FName for the Customers, don’t you If so, you need to override the ToString method for the Customer object. It will make the ListBox show the FName instead of showing the full name of the business class. Check may sample below:

Code Block

namespace ListBoxP

{

public partial class LBBusinessOB : Form

{

public LBBusinessOB()

{

InitializeComponent();

}

private void LBBusinessOB_Load(object sender, EventArgs e)

{

for (int i = 0; i < 10; i++)

{

Customer c = new Customer();

c.CustomerID = i;

c.FName = "Name" + i.ToString("00");

this.listBox1.Items.Add(c);

}

}

private void btnGetValue_Click(object sender, EventArgs e)

{

Customer c = this.listBox1.SelectedItem as Customer;

MessageBox.Show(c.CustomerID.ToString());

}

}

class Customer

{

int customerID;

public int CustomerID

{

get { return customerID; }

set { customerID = value; }

}

string fName;

public string FName

{

get { return fName; }

set { fName = value; }

}

public override string ToString()

{

return this.fName;

}

}

}

For you second question, how to drag and drop the customer to another listbox Check the following page for a sample.
http://windowsclient.net/blogs/faqs/archive/2006/05/30/how-do-i-implement-drag-and-drop-between-listboxes.aspx

Hope this helps.
Best regards.
Rong-Chun Zhang






Re: Windows Forms General adding customer object to listbox

forwheeler

Thanks for your response. After adding objects like in your example I am not able to drag them to another listbox.

But, if I add one field like this:

}

private void LBBusinessOB_Load(object sender, EventArgs e)

{

for (int i = 0; i < 10; i++)

{

Customer c = new Customer();

c.CustomerID = i;

c.FName = "Name" + i.ToString("00");

this.listBox1.Items.Add(c.FName);

}

}

I am able to drag the item to another listbox.

If I add only the one field this way then I can't get to the CustomerID property.

There must be a way to do this. Do you have any more suggestions





Re: Windows Forms General adding customer object to listbox

Rong-Chun Zhang - MSFT

Hi forwheeler,

According to your code, you are adding the string object (Customer.FName) to your ListBox’s item collection, so the SelectedItem will return the string object. You cannot convert a string object to a customer object. Thus you cannot get the CustomerID property.

I am not sure how did you implement drag and drop between ListBoxes. But I have the following code, and I can drag and drop the customer between ListBoxes.

Code Block

public class ListBoxDragAndDrop : System.Windows.Forms.ListBox

{

private int lastMouseUpItemIndex = -1;

private bool isDropSource = false;

public ListBoxDragAndDrop()

{

AllowDrop = true; // allow D&D

SelectionMode = SelectionMode.One; // single selection

DragDrop += new DragEventHandler(OnDragDrop);

DragEnter += new DragEventHandler(OnDragEnter);

MouseDown += new MouseEventHandler(OnMouseDown);

SelectedIndexChanged += new EventHandler(OnSelectedIndexChanged);

}

private void OnDragDrop(object sender, DragEventArgs e)

{

if (e.Effect == DragDropEffects.Copy)

{

System.Console.WriteLine(e.Data.GetFormats());

Point point = PointToClient(new Point(e.X, e.Y));

int index = IndexFromPoint(point);

if (index > -1 && index < Items.Count)

Items.Insert(index, e.Data.GetData("ListBoxP.Customer"));

else

Items.Add(e.Data.GetData("ListBoxP.Customer"));

}

}

private void OnDragEnter(object sender, DragEventArgs e)

{

e.Effect = (e.Data.GetDataPresent("ListBoxP.Customer") && !isDropSource)

DragDropEffects.Copy : DragDropEffects.None; //ListBoxP is the namespace.

}

private void OnMouseDown(object sender, MouseEventArgs e)

{

if (MouseButtons == MouseButtons.Left &&

SelectedIndex == lastMouseUpItemIndex)

{

isDropSource = true;

DoDragDrop(SelectedItem, DragDropEffects.Copy);

isDropSource = false;

lastMouseUpItemIndex = SelectedIndex;

}

}

private void OnSelectedIndexChanged(object sender, EventArgs e)

{

lastMouseUpItemIndex = SelectedIndex;

}

}

Hope this helps.
Best regards.
Rong-Chun Zhang






Re: Windows Forms General adding customer object to listbox

forwheeler

I used the code from this link and modified it to drag drop both directions.

http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.dodragdrop.aspx.

If I use the code in this link and substitute adding a customer object, then I can't drag drop it.

I was able to use your code and drag drop the customer.

I will look at your code more closely and see how I can incorporate this into the code I already have since I am not sure what the difference is. Maybe I can't use a standard listbox to do what I want.