Resolved How do I declare the generic BindingContext ?

Gloops

Well-known member
Joined
Jun 30, 2022
Messages
137
Programming Experience
10+
Hello everybody,
A method adds items in a ListBox, in a WinForms project on .Net 5.
I should like the ListBox to systematically display the last item at the moment it is inserted.
After adding the item, I could change the selectedItem, of course.
But I feel it would be more stylish, in fact clearer, to add an event to the ListBox to select the item when it is created, no matter how it is created, rather than letting the calling code doing it.
Even if it is some more work to do ...
Here, I saw the proposal to use a generic BindingList as source of the ListBox.
But by default, when I try to use System.Windows.Forms.BindingList, it happens to be non-generic, I get error CS0308, and it appears that adding "using System.Collections.Generic", "using System.ComponentModel" and [System.Serializable] is not enough. Even more, it appears that [System.Serializable] is not used at the good place.

Oh well, at least it is compatible with the project type, no?
 
Last edited:
But by default, when I try to use System.Windows.Forms.BindingList, it happens to be non-generic,
I must be missing it, but I'm not seeing BindingList even being listed in the System.Windows.Forms namespace for .NET Framework 4.8, nor .NET 6:


There is a BindingList<T> in the System.ComponentModel namespace.
 
I must be missing it, but I'm not seeing BindingList even being listed in the System.Windows.Forms namespace for .NET Framework 4.8, nor .NET 6:

Oh, it is Stackoverflow who is buggy, then :)
There is a BindingList<T> in the System.ComponentModel namespace.
I see that, going to see whether it contains the useful methods.

Thanks.
 
OK, did it.
But I have got some question.
BindingList<T> has two methods to add an item : add(item), and addnew().

Addnew() throws an event, AddingNew(object sender, AddingNewEventArgs e)

OK, but add(item) throws no event, as I saw?

So, as the item was defined in another event method, you have to store it in a variable at the module level (form), so that AddingNew can get it to write it in the new item?

Oh well, I made all this to have "beautiful" code, not sure?
 
The BindingList<T> will fire a list changed event.

BindingList<T> derives from Collection<T>.

See the code :
Collection.Add():
public void Add(T item) {
    if( items.IsReadOnly) {
        ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
    }

    int index = items.Count;
    InsertItem(index, item);
}

Looking further at the code, BindingList<T> overrides InsertItem():
BindingList.InsertItem():
protected override void InsertItem(int index, T item) {
    EndNew(addNewPos);
    base.InsertItem(index, item);

    if (this.raiseItemChangedEvents) {
        HookPropertyChanged(item);
    }

    FireListChanged(ListChangedType.ItemAdded, index);
}
 
Ah OK, so if you give the index to insert the item to, you avoid that missing event.
Well thanks anyway, it saves much time to know the good methods / events / properties to use ...
 
Add() just inserts at the end. They both fire the same kind of event.
 
The AddingNew event will be fired when the addition of an item is initiated by the UI, or when you call AddNew().
 
Nope. That answer doesn't specify a namespace at all. You just assumed the wrong one, instead of using the documentation to find the BindingList<T> class and see what namespace it is a member of.
That is right, I do not even remember how I did it.

Oh, and you have to prefix the BindingList, as there is also one in System.Windows.Forms, but it is not generic.
 
Hello,
Do you think I made a bad manipulation?
All controls are disactivated in the ToolBox for .Net5.0, I can add no more on the forms.
And if I give in using .Net6.0, I have to rewrite a good part of the project.
 
you have to prefix the BindingList, as there is also one in System.Windows.Forms, but it is not generic.
No you don't and no there isn't. There are a number of classes in the System.Windows.Forms namespace with a name that starts with "Binding" but no "BindingList". Even if there were such a class, the compiler would recognise that you weren't referring to it as soon as you provided a generic type parameter. If you want to use a type from the System.ComponentModel namespace then, as is always the case with every type and every namespace, you need to either import the namespace or qualify the type name.
 
All controls are disactivated in the ToolBox for .Net5.0, I can add no more on the forms.
And if I give in using .Net6.0, I have to rewrite a good part of the project.
.NET 5 is out of support now so you really shouldn't be using it any more anyway. I haven't tried for a while but I wouldn't have thought that that would mean disabling the Toolbox but maybe so. Regardless, there should be next to no change required because .NET 6 is simply the next version of .NET Core after .NET 5 so virtually everything you have already should be valid. Just go into the project properties and change the target framework from .NET 5 to .NET 6 and you should be good to go.

I just tried creating a .NET 5 WinForms project and was able to add controls to the default form without issue. I then upgraded the project to .NET 6 without issue. I would think that you should be able to do the same. The .NET upgrade should be no issue but, if there's corruption in your project, that may not fix your Toolbox issue.
 
Back
Top Bottom