C# public void MoveFirst ();
示例
下面的代码示例演示了 MoveNext 、 MoveFirst 、 Current 和 Position 成员。 若要运行此示例,请将代码粘贴到导入命名空间的窗体中,并包含名为的和名为的 System.Drawing.Drawing2D BindingSource BindingSource1 按钮 button1 。 将 Form1_Load 和 Form1_Paint 方法与 Load 窗体的和 Paint 事件关联,并将 button1_click 方法与的事件关联 Click button1 。 Visual Basic 用户将需要添加对 System.Data.dll 的引用。
C# 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);
}
注解
将属性的当前值更改 Position 为0,即基础数据源中的第一项。
适用于
适用于
产品 |
版本 |
。网 |
核心 3.0、核心 3.1、5、6 |
.NET 框架 |
2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8 |
|