BindingList<T>.AllowNew Propiedad

Definición

Obtiene o establece un valor que indica si se pueden agregar elementos a la lista utilizando el método AddNew().

public:
 property bool AllowNew { bool get(); void set(bool value); };
public bool AllowNew { get; set; }
member this.AllowNew : bool with get, set
Public Property AllowNew As Boolean

Valor de propiedad

true si se pueden agregar elementos a la lista mediante el método AddNew(); en caso contrario, false. El valor predeterminado depende del tipo subyacente contenido en la lista.

Ejemplos

En el ejemplo de código siguiente se muestra cómo establecer la AllowNew propiedad . Para obtener el ejemplo completo, consulte el tema de información general de la BindingList<T> clase.

    // Declare a new BindingListOfT with the Part business object.
    BindingList<Part> listOfParts; 
    private void InitializeListOfParts()
    {
        // Create the new BindingList of Part type.
        listOfParts = new BindingList<Part>();

        // Allow new parts to be added, but not removed once committed.        
        listOfParts.AllowNew = true;
        listOfParts.AllowRemove = false;

        // Raise ListChanged events when new parts are added.
        listOfParts.RaiseListChangedEvents = true;

        // Do not allow parts to be edited.
        listOfParts.AllowEdit = false;
        
        // Add a couple of parts to the list.
        listOfParts.Add(new Part("Widget", 1234));
        listOfParts.Add(new Part("Gadget", 5647));
    }
' Declare a new BindingListOfT with the Part business object.
Private WithEvents listOfParts As BindingList(Of Part)

Private Sub InitializeListOfParts()

    ' Create the new BindingList of Part type.
    listOfParts = New BindingList(Of Part)

    ' Allow new parts to be added, but not removed once committed.        
    listOfParts.AllowNew = True
    listOfParts.AllowRemove = False

    ' Raise ListChanged events when new parts are added.
    listOfParts.RaiseListChangedEvents = True

    ' Do not allow parts to be edited.
    listOfParts.AllowEdit = False

    ' Add a couple of parts to the list.
    listOfParts.Add(New Part("Widget", 1234))
    listOfParts.Add(New Part("Gadget", 5647))

End Sub

Comentarios

Normalmente, otros componentes usan la AllowNew propiedad para determinar si se permite la creación de nuevos elementos. AllowNew el valor predeterminado es true si el tipo contenido en la lista tiene un constructor sin parámetros o el AddingNew evento se controla. Si el AddingNew evento no se controla o el tipo de lista no tiene un constructor sin parámetros, AllowNew el valor predeterminado es false.

Si AllowNew se establece explícitamente, los objetos enlazados siempre usarán el valor establecido para determinar si se pueden agregar nuevos elementos a la lista. Tanto si AllowNew es true o false, se pueden agregar nuevos elementos llamando AddNew explícitamente si el tipo de lista tiene un constructor sin parámetros o el AddingNew evento se controla. Además, la configuración AllowNew hace que se produzca un ListChanged evento de tipo Reset .

Se aplica a

Consulte también