BindingList<T>.AllowNew Propriedade

Definição

Recebe ou define um valor que indica se pode adicionar itens à lista usando o AddNew() método.

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 Propriedade

true se conseguir adicionar itens à lista com o AddNew() método; caso contrário, false. O padrão depende do tipo subjacente contido na lista.

Exemplos

O seguinte exemplo de código demonstra como definir a AllowNew propriedade. Para o exemplo completo, veja o tópico de resumo BindingList<T> da aula.

// Declare a new BindingListOfT with the Part business object.
BindingList<Part> listOfParts;
void InitializeListOfParts()
{
    // Create the new BindingList of Part type.
    listOfParts = new BindingList<Part>
    {
        // Allow new parts to be added, but not removed once committed.        
        AllowNew = true,
        AllowRemove = false,

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

        // Do not allow parts to be edited.
        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

Observações

A AllowNew propriedade é normalmente usada por outros componentes para determinar se a criação de novos itens é permitida. AllowNew por defeito, se true o tipo contido na lista tem um construtor sem parâmetros ou se o AddingNew evento é tratado. Se o AddingNew evento não for tratado ou se o tipo de lista não tiver um construtor sem parâmetros, então AllowNew passa por defeito a false.

Se AllowNew estiver explicitamente definido, o valor de conjunto será sempre usado por objetos vinculados para determinar se novos itens podem ser adicionados à lista. Quer AllowNew seja true ou false, novos itens podem ser adicionados AddNew chamando explicitamente se o tipo de lista tiver um construtor sem parâmetros ou se o AddingNew evento for tratado. Além disso, o cenário AllowNew faz com que ocorra um ListChanged evento do tipo Reset .

Aplica-se a

Ver também