4 Replies - 1950 Views - Last Post: 27 May 2010 - 03:50 PM Rate Topic: -----

#1 blank_program   User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 292
  • Joined: 22-July 09

List<T> as datasource

Posted 27 May 2010 - 02:11 PM

My program has a textbox, button, and listbox. I am using List<T> to store a list of strings of the user input. I want the listbox to update when button is clicked to add the input. What method of the listbox would I have to run to make it update when the button is pressed? This is just a test project so I left all the default names.

My code so far:
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace winListT
{
    public partial class Form1 : Form
    {
        List<string> InputList = new List<string>();

        public Form1()
        {
            InitializeComponent();
            listBox1.DataSource = InputList;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            InputList.Add(textBox1.Text);
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: List<T> as datasource

#2 MentalFloss   User is offline

  • .
  • member icon

Reputation: 577
  • View blog
  • Posts: 1,500
  • Joined: 02-September 09

Re: List<T> as datasource

Posted 27 May 2010 - 02:31 PM

Create a BindingList that is bound to your List<string> object and then set the datasource to the binding list object instead.
From then out, update the binding list.


Otherwise, you'll have to clear the databinding and reapply it on changes to the List<string> collection.


Does this make sense?
Was This Post Helpful? 1
  • +
  • -

#3 blank_program   User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 292
  • Joined: 22-July 09

Re: List<T> as datasource

Posted 27 May 2010 - 02:37 PM

Yeah that made sense and seems to work. But since it doesn't have sorting ability I would need to somehow need to go from a normal List to the Binding, if I wanted to sort. And I don't understand the difference between Add() and AddNew() of the BindingList.
Was This Post Helpful? 0
  • +
  • -

#4 MentalFloss   User is offline

  • .
  • member icon

Reputation: 577
  • View blog
  • Posts: 1,500
  • Joined: 02-September 09

Re: List<T> as datasource

Posted 27 May 2010 - 02:42 PM

http://msdn.microsof...y/ms132686.aspx
http://msdn.microsof...y/ms132435.aspx

That should clear it up for you.

EDIT: Eh, didn't realize that Items is protected. Give me a minute - I'll update with answer.

EDIT 2: I'm bowing out on this one. I'd have to do quite a bit more research than I'm currently able to. Maybe later tonight...

This post has been edited by MentalFloss: 27 May 2010 - 03:04 PM

Was This Post Helpful? 0
  • +
  • -

#5 FlashM   User is offline

  • D.I.C Lover
  • member icon

Reputation: 383
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: List<T> as datasource

Posted 27 May 2010 - 03:50 PM

I suggest you use a BindingList<T> instead of List<T>. Then just bind this BindingList<T> directly to your ListBox as it's datasource.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1