BindingSource.Position BindingSource.Position BindingSource.Position BindingSource.Position Property

Définition

Obtient ou définit l'index de l'élément actuel dans la liste sous-jacente.Gets or sets the index of the current item in the underlying list.

public:
 property int Position { int get(); void set(int value); };
[System.ComponentModel.Browsable(false)]
public int Position { get; set; }
member this.Position : int with get, set
Public Property Position As Integer

Valeur de propriété

Index de base zéro qui spécifie la position de l'élément actuel dans la liste sous-jacente.A zero-based index that specifies the position of the current item in the underlying list.

Exemples

L’exemple de code suivant illustre MoveNextles MoveFirstmembres Current,, Position et.The following code example demonstrates the MoveNext, MoveFirst, Current, and Position members. Pour exécuter cet exemple, collez le code dans un formulaire qui importe System.Drawing.Drawing2D l’espace de noms BindingSource et BindingSource1 qui contient un nommé button1et un bouton nommé.To run this example, paste the code into a form that imports the System.Drawing.Drawing2D namespace and contains a BindingSource named BindingSource1 and a button named button1. Associez Form1_Load les Form1_Paint méthodes et aux Load événements Paint et du formulaire, et associez la button1_click méthode à l' Click événement pour button1.Associate the Form1_Load and Form1_Paint methods with the Load and Paint events for the form, and associate the button1_click method with the Click event for button1. Visual Basic utilisateurs devront ajouter une référence à System. Data. dll.Visual Basic users will need to add a reference to System.Data.dll.

    void Form1_Load(Object^ sender, EventArgs^ e)
    {
        // Set the data source to the Brush type and populate
        // bindingSource1; with some brushes.
        bindingSource1->DataSource = System::Drawing::Brush::typeid;
        bindingSource1->Add(
            gcnew TextureBrush(gcnew Bitmap(Button::typeid, "Button.bmp")));
        bindingSource1->Add(gcnew HatchBrush(HatchStyle::Cross, Color::Red));
        bindingSource1->Add(gcnew SolidBrush(Color::Blue));
    }


private:
    void moveNextButton_Click(Object^ sender, EventArgs^ e)
    {
        // If you are not at the end of the list, move to the next item
        // in the BindingSource.
        if (bindingSource1->Position + 1 < bindingSource1->Count)
        {
            bindingSource1->MoveNext();
        }
        // Otherwise, move back to the first item.
        else
        {
            bindingSource1->MoveFirst();
        }
        // Force the form to repaint.
        this->Invalidate();
    }

    void Form1_Paint(Object^ sender, PaintEventArgs^ e)
    {
        // Get the current item in the BindingSource.
        Brush^ item = (Brush^) bindingSource1->Current;

        // If the current type is a TextureBrush, fill an ellipse.
        if (item->GetType() == TextureBrush::typeid)
        {
            e->Graphics->FillEllipse(item,e->ClipRectangle);
        }
        // If the current type is a HatchBrush, fill a triangle.
        else if (item->GetType() == HatchBrush::typeid)
        {

            e->Graphics->FillPolygon(item, 
                gcnew array<Point> {*gcnew Point(0, 0),
                *gcnew Point(0, 200),
                *gcnew Point(200, 0)});
        }
        // Otherwise, fill a rectangle.
        else
        {
            e->Graphics->FillRectangle(
                (Brush^)bindingSource1->Current, e->ClipRectangle);
        }
    }

void Form1_Load(object sender, EventArgs e)
{
	// Set the data source to the Brush type and populate
	// BindingSource1 with some brushes.
	BindingSource1.DataSource = typeof(System.Drawing.Brush);
	BindingSource1.Add(
		new TextureBrush(new Bitmap(typeof(Button), "Button.bmp")));
	BindingSource1.Add(new HatchBrush(HatchStyle.Cross, Color.Red));
	BindingSource1.Add(new SolidBrush(Color.Blue));
}


private void button1_Click(object sender, EventArgs e)
{
	// If you are not at the end of the list, move to the next item
	// in the BindingSource.
	if (BindingSource1.Position + 1 < BindingSource1.Count)
		BindingSource1.MoveNext();

	// Otherwise, move back to the first item.
	else
		BindingSource1.MoveFirst();

	// Force the form to repaint.
	this.Invalidate();
}

void Form1_Paint(object sender, PaintEventArgs e)
{
	// Get the current item in the BindingSource.
	Brush item = (Brush)BindingSource1.Current;

	// If the current type is a TextureBrush, fill an ellipse.
	if (item.GetType() == typeof(TextureBrush))
		e.Graphics.FillEllipse(item,
		   e.ClipRectangle);

	// If the current type is a HatchBrush, fill a triangle.
	else if (item.GetType() == typeof(HatchBrush))
		e.Graphics.FillPolygon(item,
			new Point[] { new Point(0, 0), new Point(0, 200),
                  new Point(200, 0)});

	// Otherwise, fill a rectangle.
	else
		e.Graphics.FillRectangle(
			(Brush)BindingSource1.Current, e.ClipRectangle);
}
Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) _
    Handles Me.Load

    ' Set the data source to the Brush type and populate
    ' BindingSource1 with some brushes.
    BindingSource1.DataSource = GetType(System.Drawing.Brush)
    BindingSource1.Add(New TextureBrush(New Bitmap(GetType(Button), _
        "Button.bmp")))
    BindingSource1.Add(New HatchBrush(HatchStyle.Cross, Color.Red))
    BindingSource1.Add(New SolidBrush(Color.Blue))

End Sub



Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
     Handles button1.Click

    ' If you are not at the end of the list, move to the next item
    ' in the BindingSource.
    If BindingSource1.Position + 1 < BindingSource1.Count Then
        BindingSource1.MoveNext()

        ' Otherwise, move back to the first item.
    Else
        BindingSource1.MoveFirst()
    End If

    ' Force the form to repaint.
    Me.Invalidate()

End Sub


Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs)

    ' Get the current item in the BindingSource.
    Dim item As Brush = CType(BindingSource1.Current, Brush)

    ' If the current type is a TextureBrush, fill an ellipse.
    If item.GetType().Equals(GetType(TextureBrush)) Then
        e.Graphics.FillEllipse(item, _
        e.ClipRectangle)

        ' If the current type is a HatchBrush, fill a triangle.
    ElseIf item.GetType().Equals(GetType(HatchBrush)) Then
        e.Graphics.FillPolygon(item, New Point() _
         {New Point(0, 0), New Point(0, 200), New Point(200, 0)})

        ' Otherwise, fill a rectangle.
    Else
        e.Graphics.FillRectangle(item, e.ClipRectangle)
    End If

End Sub

Remarques

Lors de la Position définition de la propriété, les valeurs hors limites sont traitées de la manière suivante:When setting the Position property, out-of-range values are treated in the following manner:

  • Les valeurs négatives sont traitées comme étant 0.Negative values are treated as 0.

  • Les valeurs supérieures ou égales Count à sont traitées Count comme moins 1.Values greater than or equal to Count are treated as Count minus 1.

La modification Position de la propriété permet Current d’ajuster la propriété de la même façon.Changing the Position property will adjust the Current property likewise.

S’applique à

Voir aussi