Click here to Skip to main content
15,890,512 members
Home / Discussions / C#
   

C#

 
AnswerRe: where to save my error.log? Pin
Mark Salsbery9-Nov-11 6:14
Mark Salsbery9-Nov-11 6:14 
QuestionHow Search Algorithms Work Pin
harsimranb9-Nov-11 4:59
harsimranb9-Nov-11 4:59 
AnswerRe: How Search Algorithms Work Pin
Maximilien9-Nov-11 5:09
Maximilien9-Nov-11 5:09 
QuestionWhy I am getting this warning? Pin
Jassim Rahma9-Nov-11 4:48
Jassim Rahma9-Nov-11 4:48 
AnswerRe: Why I am getting this warning? Pin
Bert Mitton9-Nov-11 5:52
professionalBert Mitton9-Nov-11 5:52 
GeneralRe: Why I am getting this warning? Pin
Jassim Rahma9-Nov-11 5:57
Jassim Rahma9-Nov-11 5:57 
GeneralRe: Why I am getting this warning? Pin
RichardWil459-Nov-11 11:10
RichardWil459-Nov-11 11:10 
Questioncannot bind a static List<T> as DataSource to a ListBox ? Pin
BillWoodruff9-Nov-11 4:30
professionalBillWoodruff9-Nov-11 4:30 
... edit #1 ... supplied link to critique of System.ComponentModel by SA Kruyakov as mentioned in text ...

Context: WinForms .NET 4.0 Client FrameWork, VS 2010 Pro

The goal of this question: to find a way to use a Static generic list containing instances of a non-static class to DataBind to a ListBox.Items Collection ... so the collection is dynamically updated ... without relying on using the BindingList Type in System.ComponentModel.

Background:

Back in 2007, Frans Bouma wrote[^]:
List<T>doesn't raise an event that something was changed inside it,
as it doesn't implement IBindingList. This thus means that the bound
control can't 'observe' the datasource, so it can't know if something
changed inside List<t>.
Since then, I've seen code examples where it looks like a generic List was rather easily set as the DataSource for ListBox, and I assumed the "barrier" that FB described in 2007 had been removed. Such as: [^].

And now:

Well today, I created a static class that holds a static generic list of a certain non-static class: like so:
C#
public static class People
{
    public static List<Person> ListOfEverybody;

    public static Person CurrentPerson;

    public static bool IsPersonAlreadyEntered;

    static People()
    {
        ListOfEverybody = new List<Person>();
    }

    public static void AddPerson(string pName)
    {
        // prevent duplicates : this ain't Linq, but does use a Lambda
        IsPersonAlreadyEntered = ListOfEverybody.Exists(prsn => prsn.PersonName == pName);

        if (IsPersonAlreadyEntered) return;

        CurrentPerson = new Person(pName);
    }
}
I set up the non-generic class 'Person with a Public Property I thought appropriate to use in data-binding, and a ToString over-ride:
C#
public class Person
{
    public string PersonName { get; set; }

    public Person(string newPersonName)
    {
        PersonName = newPersonName;
        People.ListOfEverybody.Add(this);
    }

    public override string ToString()
    {
        return this.PersonName;
    }
}
Results:

To sum it up, all my experiments in trying to bind a ListBox on a Form to the static ListOfEveryBody<person> in the static class People, so that the ListBox contents are updated as new entries are added ... using every combination of reasonable entries for DataSource, DisplayMember, ValueMember, fail.

My experiments with the newer 'BindingSource' component ... fail.

And then, a "bloody solution:"

Finally I trotted out the 'BindingList' from System.ComponentModel (the Library of which Sergey Kruyakov is very critical[^]), which Bouma had mentioned, and got that working to dynamically update the ListBox Items.

However, BindingList does not support the very nice System.Collections.Generic '.Exists' function, which I find so handy.

So I ended up maintaining parallel Lists, one BindingList<Person>, and one List<Person> which really takes away that mint-fresh-all-over feeling I crave Smile | :)

I'm thinking there must be a way to databind a generic List that happens to be static to a ListBox's Items so it is dynamically updated that's much more "freshy."

Appreciate any suggestions.

thanks, Bill
"Last year I went fishing with Salvador Dali. He was using a dotted
line. He caught every other fish." Steven Wright


modified 11-Nov-11 1:11am.

AnswerRe: cannot bind a static List as DataSource to a ListBox ? Pin
PIEBALDconsult9-Nov-11 4:53
mvePIEBALDconsult9-Nov-11 4:53 
GeneralRe: cannot bind a static List as DataSource to a ListBox ? Pin
BillWoodruff9-Nov-11 4:56
professionalBillWoodruff9-Nov-11 4:56 
QuestionOverriding Render in Asp.Net Pin
Neox849-Nov-11 3:51
Neox849-Nov-11 3:51 
QuestionDatagrid to display Pin
Blubbo9-Nov-11 3:31
Blubbo9-Nov-11 3:31 
QuestionRASENTRY member to designate more than one device Pin
mheinz9-Nov-11 0:56
mheinz9-Nov-11 0:56 
SuggestionRe: RASENTRY member to designate more than one device Pin
RaviRanjanKr13-Nov-11 4:42
professionalRaviRanjanKr13-Nov-11 4:42 
GeneralRe: RASENTRY member to designate more than one device Pin
mheinz24-Nov-11 3:30
mheinz24-Nov-11 3:30 
GeneralRe: RASENTRY member to designate more than one device Pin
RaviRanjanKr24-Nov-11 7:57
professionalRaviRanjanKr24-Nov-11 7:57 
Questionhow to get hardware unique serial no Pin
umeshdaiya8-Nov-11 22:18
umeshdaiya8-Nov-11 22:18 
AnswerRe: how to get hardware unique serial no Pin
Rob Philpott8-Nov-11 22:30
Rob Philpott8-Nov-11 22:30 
AnswerRe: how to get hardware unique serial no Pin
PIEBALDconsult9-Nov-11 1:40
mvePIEBALDconsult9-Nov-11 1:40 
GeneralRe: how to get hardware unique serial no Pin
GuyThiebaut9-Nov-11 1:45
professionalGuyThiebaut9-Nov-11 1:45 
AnswerRe: how to get hardware unique serial no Pin
Bernhard Hiller9-Nov-11 2:57
Bernhard Hiller9-Nov-11 2:57 
AnswerRe: how to get hardware unique serial no Pin
Alan Balkany11-Nov-11 11:14
Alan Balkany11-Nov-11 11:14 
QuestionDesigning: Multiple interfaces implementation on classes Pin
vinayvraman8-Nov-11 19:27
vinayvraman8-Nov-11 19:27 
AnswerRe: Designing: Multiple interfaces implementation on classes Pin
Ravi Sant8-Nov-11 21:15
Ravi Sant8-Nov-11 21:15 
GeneralRe: Designing: Multiple interfaces implementation on classes Pin
jschell9-Nov-11 11:13
jschell9-Nov-11 11:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.