使用VB IDE通過幫助窗口中的“索引,輸入關鍵字“工程”,把查到的幫助信息放入代碼窗口中上傳
來源:新能源網(wǎng)
時間: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 對象
示例 請參見 發(fā)送反饋意見
生成與數(shù)據(jù)交互的控件時,有時會覺得有必要將控件綁定到生成其他對象的對象或方法。這樣的對象或方法稱為一個工廠。例如,數(shù)據(jù)源可能是來自某個方法調(diào)用的返回值,而不是內(nèi)存中的一個對象或一個類型。只要此類數(shù)據(jù)源返回一個集合,就可以將控件綁定到此數(shù)據(jù)源。
通過使用 BindingSource 控件,您可以很容易地將控件綁定到某個工廠對象。
示例
下面的示例演示如何通過使用 BindingSource 控件將 DataGridView 控件綁定到一個工廠方法。此工廠方法命名為 GetOrdersByCustomerId,它返回 Northwind 數(shù)據(jù)庫中某個給定客戶的所有訂單。
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
發(fā)送反饋意見,就此主題向 Microsoft 發(fā)送反饋意見。
-
沼氣池里能放入臭化肥做燃料嗎2024-08-17
-
煉鐵的還原劑CO是由焦炭和CO2反應而得.現(xiàn)將焦炭和CO2放入體積為2L的密閉容器中,高溫下進行下列反應:C2024-08-17
-
煤氣罐、熱水器可以放入衛(wèi)生間嗎?2024-08-17
-
我剛買回來的高背金龍魚為什么總喜歡趴缸,放入龍魚之前水都困好了 48小時循環(huán)打氧吹氣 也放了海水2024-08-17
-
修建廠房購進的鋼材放入哪個會計科目2024-08-17
-
養(yǎng)金龍魚前水中是否放入一些鹽2024-08-17
-
小明想:將一壺水燒開究竟需要多少天然氣呢?他通過實踐收集如下數(shù)據(jù):水壺里放入2000cm^3、20度水,大火加熱直至沸騰,天然氣熱值為8*10^72024-08-17
-
利用Google搜索引擎檢索“建筑施工”方面的信息,檢索結(jié)果不包括flash文件類型,檢索式是什么?2024-08-17
-
下列制取氫氣的操作順序正確的是( )①注入稀硫酸②放入鋅粒③把儀器固定在鐵架臺上④塞好帶漏斗和導2024-08-17
-
煤氣燒的鍋里放入白銀會化嗎2024-08-17
-
將氮氣和氫氣按體積比1:4放入密閉容器中,壓強為30.3MPa.平衡后,混合氣體...2024-08-17
-
自制氫氣放入堿和鋁的比例是多少???2024-08-17
-
太陽能熱水器中的水放入鍋中煮沸后能否飲用?2024-08-17
-
因特網(wǎng)上,搜索引擎一般提供什么型檢索和關鍵詞索引型檢2024-08-17
-
搜索引擎的原理2024-08-17