简体   繁体   中英

Show user defined data from a Queue in a ListView

I want to show the data in a listview, the data stored in a Queue and data type is user defined type(including two string and one int type).

How can i show the data inside after every process of enqueue and dequeue, what could be the command lines and syntax?

Queue<Customer> aCustomerQueue =new Queue<Customer>();// its the queue

class Customer// its the user defined type variable class
{
    public string name;
    public string complain;
    public int serialNo;

}

Stored data will be user input.

You can try this code.

    int count;
    Queue<Customer> customers = new Queue<Customer>();
    ListViewItem item;
    private void enqueueButton_Click(object sender, EventArgs e)
    {
        Customer customer = new Customer();
        count++;
        customer.serialNo += count;
        customer.name = nameTextBox.Text;
        customer.complain = complainTextBox.Text;
        customers.Enqueue(customer);


        foreach (Customer custm in customers)
        {
            item = new ListViewItem(custm.serialNo.ToString());
            item.SubItems.Add(custm.name);
            item.SubItems.Add(custm.complain);

        }
        customerQueueListView.Items.Add(item);            
    }

for dequeue

    private void dequeueButton_Click(object sender, EventArgs e)
    {
        if (customers.Count != 0)
        {
            customers.Dequeue();
            customerQueueListView.Items[0].Remove();
        }
    }

Take a look at binding-to-queuestring-ui-never-updates .

The proposed implementation is a good way to go (code taken from the linked question):

public class ObservableQueue<T> : INotifyCollectionChanged, IEnumerable<T>
{
    public event NotifyCollectionChangedEventHandler CollectionChanged;
    private readonly Queue<T> queue = new Queue<T>();

    public void Enqueue(T item)
    {
        queue.Enqueue(item);
        if (CollectionChanged != null)
            CollectionChanged(this, 
                new NotifyCollectionChangedEventArgs(
                    NotifyCollectionChangedAction.Add, item));
    }

    public T Dequeue()
    {
        var item = queue.Dequeue();
        if (CollectionChanged != null)
            CollectionChanged(this, 
                new NotifyCollectionChangedEventArgs(
                    NotifyCollectionChangedAction.Remove, item));
        return item;
    }

    public IEnumerator<T> GetEnumerator()
    {
        return queue.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM