使用VB IDE通过帮助窗口中的“索引,输入关键字“工程”,把查到的帮助信息放入代码窗口中上传
来源:新能源网
时间:2024-08-17 13:26:19
热度:
使用VB IDE通过帮助窗口中的“索引,输入关键字“工程”,把查到的帮助信息放入代码窗口中上传【专家解说】:全部折叠全部展开 代码:全部 代码:多个 代码:Visual Basic
【专家解说】:全部折叠全部展开 代码:全部 代码:多个 代码:Visual Basic 代码:C# 代码:Visual C++ 代码:J# 代码:Jscript
Visual Basic
C#
Visual C++
J#
Jscript
Windows 窗体编程
如何:将 Windows 窗体控件绑定到 Factory 对象
示例 请参见 发送反馈意见
生成与数据交互的控件时,有时会觉得有必要将控件绑定到生成其他对象的对象或方法。这样的对象或方法称为一个工厂。例如,数据源可能是来自某个方法调用的返回值,而不是内存中的一个对象或一个类型。只要此类数据源返回一个集合,就可以将控件绑定到此数据源。
通过使用 BindingSource 控件,您可以很容易地将控件绑定到某个工厂对象。
示例
下面的示例演示如何通过使用 BindingSource 控件将 DataGridView 控件绑定到一个工厂方法。此工厂方法命名为 GetOrdersByCustomerId,它返回 Northwind 数据库中某个给定客户的所有订单。
Visual Basic 复制代码
imports System
imports System.Collections
imports System.Collections.Generic
imports System.ComponentModel
imports System.Data
imports System.Data.Common
imports System.Diagnostics
imports System.Drawing
imports System.Data.SqlClient
imports System.Windows.Forms
' This form demonstrates using a BindingSource to bind to a factory
' object.
Public Class Form1
Inherits System.Windows.Forms.Form
' This is the TextBox for entering CustomerID values.
Private WithEvents customerIdTextBox As New TextBox()
' This is the DataGridView that displays orders for the
' specified customer.
Private customersDataGridView As New DataGridView()
' This is the BindingSource for binding the database query
' result set to the DataGridView.
Private ordersBindingSource As New BindingSource()
Public Sub New()
' Set up the CustomerID TextBox.
Me.customerIdTextBox.Location = New Point(100, 200)
Me.customerIdTextBox.Size = New Size(500, 30)
Me.customerIdTextBox.Text = _
"Enter a valid Northwind CustomerID, for example: ALFKI," & _
" then RETURN or click outside the TextBox"
Me.Controls.Add(Me.customerIdTextBox)
' Set up the DataGridView.
customersDataGridView.Dock = DockStyle.Top
Me.Controls.Add(customersDataGridView)
' Set up the form.
Me.Size = New Size(800, 500)
End Sub
' This event handler binds the BindingSource to the DataGridView
' control's DataSource property.
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Me.Load
' Attach the BindingSource to the DataGridView.
Me.customersDataGridView.DataSource = Me.ordersBindingSource
End Sub
' This is a static factory method. It queries the Northwind
' database for the orders belonging to the specified
' customer and returns an IEnumerable.
Public Shared Function GetOrdersByCustomerId(ByVal id As String) _
As IEnumerable
' Open a connection to the database.
Dim connectString As String = "Integrated Security=SSPI;" & _
"Persist Security Info=False;Initial Catalog=Northwind;" & _
"Data Source= localhost"
Dim connection As New SqlConnection()
connection.ConnectionString = connectString
connection.Open()
' Execute the query.
Dim queryString As String = _
String.Format("Select * From Orders where CustomerID = '{0}'", id)
Dim command As New SqlCommand(queryString, connection)
Dim reader As SqlDataReader = _
command.ExecuteReader(CommandBehavior.CloseConnection)
Return reader
End Function
' These event handlers are called when the user tabs or clicks
' out of the customerIdTextBox or hits the return key.
' The database is then queried with the CustomerID
' in the customerIdTextBox.Text property.
Private Sub customerIdTextBox_Leave(ByVal sender As Object, _
ByVal e As EventArgs) Handles customerIdTextBox.Leave
' Attach the data source to the BindingSource control.
Me.ordersBindingSource.DataSource = _
GetOrdersByCustomerId(Me.customerIdTextBox.Text)
End Sub
Private Sub customerIdTextBox_KeyDown(ByVal sender As Object, _
ByVal e As KeyEventArgs) Handles customerIdTextBox.KeyDown
If e.KeyCode = Keys.Return Then
' Attach the data source to the BindingSource control.
Me.ordersBindingSource.DataSource = _
GetOrdersByCustomerId(Me.customerIdTextBox.Text)
End If
End Sub
<STAThread()> _
Shared Sub Main()
Application.EnableVisualStyles()
Application.Run(New Form1())
End Sub
End Class
C# 复制代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Diagnostics;
using System.Drawing;
using System.Data.SqlClient;
using System.Windows.Forms;
// This form demonstrates using a BindingSource to bind to a factory
// object.
public class Form1 : System.Windows.Forms.Form
{
// This is the TextBox for entering CustomerID values.
private TextBox customerIdTextBox = new TextBox();
// This is the DataGridView that displays orders for the
// specified customer.
private DataGridView customersDataGridView = new DataGridView();
// This is the BindingSource for binding the database query
// result set to the DataGridView.
private BindingSource ordersBindingSource = new BindingSource();
public Form1()
{
// Set up the CustomerID TextBox.
this.customerIdTextBox.Location = new Point(100, 200);
this.customerIdTextBox.Size = new Size(500, 30);
this.customerIdTextBox.Text =
"Enter a valid Northwind CustomerID, for example: ALFKI," +
" then RETURN or click outside the TextBox";
this.customerIdTextBox.Leave +=
new EventHandler(customerIdTextBox_Leave);
this.customerIdTextBox.KeyDown +=
new KeyEventHandler(customerIdTextBox_KeyDown);
this.Controls.Add(this.customerIdTextBox);
// Set up the DataGridView.
customersDataGridView.Dock = DockStyle.Top;
this.Controls.Add(customersDataGridView);
// Set up the form.
this.Size = new Size(800, 500);
this.Load += new EventHandler(Form1_Load);
}
// This event handler binds the BindingSource to the DataGridView
// control's DataSource property.
private void Form1_Load(
System.Object sender,
System.EventArgs e)
{
// Attach the BindingSource to the DataGridView.
this.customersDataGridView.DataSource =
this.ordersBindingSource;
}
// This is a static factory method. It queries the Northwind
// database for the orders belonging to the specified
// customer and returns an IEnumerable.
public static IEnumerable GetOrdersByCustomerId(string id)
{
// Open a connection to the database.
string connectString = "Integrated Security=SSPI;" +
"Persist Security Info=False;Initial Catalog=Northwind;" +
"Data Source= localhost";
SqlConnection connection = new SqlConnection();
connection.ConnectionString = connectString;
connection.Open();
// Execute the query.
string queryString =
String.Format("Select * From Orders where CustomerID = '{0}'",
id);
SqlCommand command = new SqlCommand(queryString, connection);
SqlDataReader reader =
command.ExecuteReader(CommandBehavior.CloseConnection);
return reader;
}
// These event handlers are called when the user tabs or clicks
// out of the customerIdTextBox or hits the return key.
// The database is then queried with the CustomerID
// in the customerIdTextBox.Text property.
void customerIdTextBox_Leave(object sender, EventArgs e)
{
// Attach the data source to the BindingSource control.
this.ordersBindingSource.DataSource =
GetOrdersByCustomerId(this.customerIdTextBox.Text);
}
void customerIdTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Return)
{
// Attach the data source to the BindingSource control.
this.ordersBindingSource.DataSource =
GetOrdersByCustomerId(this.customerIdTextBox.Text);
}
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
Visual C++ 复制代码
#using <System.dll>
#using <System.Data.dll>
#using <System.Drawing.dll>
#using <System.EnterpriseServices.dll>
#using <System.Transactions.dll>
#using <System.Windows.Forms.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Generic;
using namespace System::ComponentModel;
using namespace System::Data;
using namespace System::data::Common;
using namespace System::data::SqlClient;
using namespace System::Diagnostics;
using namespace System::Drawing;
using namespace System::Windows::Forms;
// This form demonstrates using a BindingSource to bind to a factory
// object.
public ref class Form1: public System::Windows::Forms::Form
{
private:
// This is the TextBox for entering CustomerID values.
static TextBox^ customerIdTextBox = gcnew TextBox;
// This is the DataGridView that displays orders for the
// specified customer.
static DataGridView^ customersDataGridView = gcnew DataGridView;
// This is the BindingSource for binding the database query
// result set to the DataGridView.
static BindingSource^ ordersBindingSource = gcnew BindingSource;
public:
Form1()
{
// Set up the CustomerID TextBox.
this->customerIdTextBox->Dock = DockStyle::Bottom;
this->customerIdTextBox->Text =
L"Enter a valid Northwind CustomerID, for example: ALFKI,"
L" then TAB or click outside the TextBox";
this->customerIdTextBox->Leave += gcnew EventHandler(
this, &Form1::customerIdTextBox_Leave );
this->Controls->Add( this->customerIdTextBox );
// Set up the DataGridView.
customersDataGridView->Dock = DockStyle::Top;
this->Controls->Add( customersDataGridView );
// Set up the form.
this->Size = System::Drawing::Size( 800, 800 );
this->Load += gcnew EventHandler( this, &Form1::Form1_Load );
}
private:
// This event handler binds the BindingSource to the DataGridView
// control's DataSource property.
void Form1_Load(
System::Object^ ,
System::EventArgs^ )
{
// Attach the BindingSource to the DataGridView.
this->customersDataGridView->DataSource =
this->ordersBindingSource;
}
public:
// This is a static factory method. It queries the Northwind
// database for the orders belonging to the specified
// customer and returns an IList.
static System::Collections::IList^ GetOrdersByCustomerId( String^ id )
{
// Open a connection to the database.
String^ connectString = L"Integrated Security=SSPI;"
L"Persist Security Info=False;Initial Catalog=Northwind;"
L"Data Source= localhost";
SqlConnection^ connection = gcnew SqlConnection;
connection->ConnectionString = connectString;
connection->Open();
// Execute the query.
String^ queryString = String::Format(
L"Select * From Orders where CustomerID = '{0}'", id );
SqlCommand^ command = gcnew SqlCommand( queryString,connection );
SqlDataReader^ reader = command->ExecuteReader(
CommandBehavior::CloseConnection );
// Build an IList from the result set.
List< DbDataRecord^ >^ list = gcnew List< DbDataRecord^ >;
System::Collections::IEnumerator^ e = reader->GetEnumerator();
while ( e->MoveNext() )
{
list->Add( dynamic_cast<DbDataRecord^>(e->Current) );
}
return list;
}
// This event handler is called when the user tabs or clicks
// out of the customerIdTextBox. The database is then queried
// with the CustomerID in the customerIdTextBox.Text property.
private:
void customerIdTextBox_Leave( Object^ , EventArgs^ )
{
// Attach the data source to the BindingSource control.
this->ordersBindingSource->DataSource = GetOrdersByCustomerId(
this->customerIdTextBox->Text );
}
public:
[STAThread]
static void main()
{
Application::EnableVisualStyles();
Application::Run( gcnew Form1 );
}
};
编译代码
此示例要求:
对 System、System.Data、System.Drawing 和 System.Windows.Forms 程序集的引用。
有关从 Visual Basic 或 Visual C# 的命令行生成此示例的信息,请参见从命令行生成 (Visual Basic) 或命令行生成。也可以通过将代码粘贴到新项目,在 Visual Studio 中生成此示例。 有关更多信息,请参见如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例.
请参见
概念
BindingSource 组件
如何:将 Windows 窗体控件绑定到类型
参考
BindingNavigator
DataGridView
BindingSource
发送反馈意见,就此主题向 Microsoft 发送反馈意见。
-
沼气池里能放入臭化肥做燃料吗2024-08-17
-
炼铁的还原剂CO是由焦炭和CO2反应而得.现将焦炭和CO2放入体积为2L的密闭容器中,高温下进行下列反应:C2024-08-17
-
煤气罐、热水器可以放入卫生间吗?2024-08-17
-
我刚买回来的高背金龙鱼为什么总喜欢趴缸,放入龙鱼之前水都困好了 48小时循环打氧吹气 也放了海水2024-08-17
-
修建厂房购进的钢材放入哪个会计科目2024-08-17
-
养金龙鱼前水中是否放入一些盐2024-08-17
-
小明想:将一壶水烧开究竟需要多少天然气呢?他通过实践收集如下数据:水壶里放入2000cm^3、20度水,大火加热直至沸腾,天然气热值为8*10^72024-08-17
-
利用Google搜索引擎检索“建筑施工”方面的信息,检索结果不包括flash文件类型,检索式是什么?2024-08-17
-
下列制取氢气的操作顺序正确的是( )①注入稀硫酸②放入锌粒③把仪器固定在铁架台上④塞好带漏斗和导2024-08-17
-
煤气烧的锅里放入白银会化吗2024-08-17
-
将氮气和氢气按体积比1:4放入密闭容器中,压强为30.3MPa.平衡后,混合气体...2024-08-17
-
自制氢气放入碱和铝的比例是多少???2024-08-17
-
太阳能热水器中的水放入锅中煮沸后能否饮用?2024-08-17
-
因特网上,搜索引擎一般提供什么型检索和关键词索引型检2024-08-17
-
搜索引擎的原理2024-08-17