顯示具有 Windows 標籤的文章。 顯示所有文章
顯示具有 Windows 標籤的文章。 顯示所有文章

2010年7月27日 星期二

設定應用程式的啟動表單

一、設定應用程式的啟動表單
若視窗表單應用程式包含多個表單,則必須為應用程式指定一個啟動表單(Startup Form)。
所謂啟動表單是指「在應用程式執行時第一個被載入並顯示的表單。」

1、在Visual Basic的開發環境下設定啟動表單的步驟:
a、在「專案」選單下選擇對應至此專案的專案屬性。

b、將啟動表單之專案屬性的「輸出類型」設定成「Windows 應用程式」,其餘非啟動表單設定成「類別庫」。

2、在C#的開發環境下設定啟動表單的步驟:
a、在方案總管-->啟始專案-->Program.cs,按右鍵選擇「檢視程式碼」或雙擊二下檢視程式碼
///
/// 應用程式的主要進入點。
///

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new StartUpForm());
}

b、將游標移至Main方法,並找出如下所示的程式碼,其中Form所代表名稱就是目前預設的啟動表單的類別名稱

Application.Run(new StartUpForm());

表單相關(二)

一、設定表單框線的樣式FormBorderStyle
None-不顯示框線、最大化、最小化及說明按鈕或其它任何控制方塊
FixedSingle-表單的框線為單一的線條,使用者無法動態地對表單大小進行調整,有最大化、最小化及說明按鈕或其它任何控制方塊
Fix3D-表單的框線為立體的,使用者無法動態地對表單大小進行調整,有最大化、最小化及說明按鈕或其它任何控制方塊
FixDialog-表單的框線為單一的線條,使用者無法動態地對表單大小進行調整,除此之外,它亦無控制方塊,但有最大化、最小化及說明按鈕或其它任何控制方塊
FixedToolWindow-表單的框線為單一的線條,使用者無法動態地對表單大小進行調整,亦不包含任何按鈕控制按鈕方塊,唯有關閉X按鈕
Sizable-此為表單預設值,使用者可以動態地對表單大小進行調整,有最大化、最小化及說明按鈕或其它任何控制方塊
SizableToolWindow-表單的框線為單一的線條,使用者可以動態地對表單大小進行調整,有最大化、最小化及說明按鈕或其它任何控制方塊
aForm.FormBorderStyle = FormBorderStyle.Fixed3D;

二、表單起始WindowState
Normal-預設值,表單開啟時初始大小取決於Size屬性的設定
Minimized-以最小化狀態放置於工作列上
Maximzed-表單初始時被開啟至最大

三、調整表單大小
Normal-預設值,表單開啟時初始大小取決於Size屬性的設定

'VB
aForm.Width = 300
aForm.Height = 200

aForm.Size = New Size(300,200)

//C#
aForm.Width = 300;
aForm.Height = 200;

aForm.Size = new Size(300,200);

四、指定表單的起始位置(StartPosition與Location)
FormStartPosition之Enumeration(列舉)中屬的任何一值
Manual-表單的起始位置是根據Location屬性的內容值來顯示。
CenterScreen-表單的起始位置是在螢幕的正中央。
WindowsDefaultLocation-表單的起始位置為視窗本身預設的位置,而表單大小則是根據Size屬性的內容值來決定。
WindowsDefaultBounds-表單的起始位置為視窗本身預設的位置,而邊界亦是根據視窗本身的預設值來決定。
CenterParent-表單的起始位置為其父表單(Parent Form)的正中央。

五、讓表單保持在使用者介面的最上層
aForm.TopMost = True;
當多張表單屬性都設成True,無法避免表單覆蓋的情形。

六、設定表單的透明度等級(Opacity)
值:0%(透明)~100%(完全不透明)
aForm.Opacity =0.5;
ps:想讓表單保持在最上方,又可看到背景下方的動作情形。表單上的控制項都會自動繼承表單本身透明度設定。

七、其它
1、常用屬性
Text-用來指定此表單的標題文字。
Enable-是否可以接收使用者輸入的各種命令。
Visable-是否要在程式執行時顯示此表單。
Font-用來指定表單上的預設字型,表單上包含所有控制項將沿用此一字型,除非某控制項另行設定。
ForeColor-用來指定表單前景顏色,亦即文字顯示的顏色,表單上包含所有控制項將沿用此一字型,除非某控制項另行設定。
FormBorderStyle-用來指定表的框線以及標題列的外觀與行為。
Size-用來指定表單大小。
StartPosition-用來指定此表單開始顯示的起始位置。


2、看看就好
BackgroundImage-用來指定表單的背景圖案。
BackgroundImageLayout-用來指定表單的背景圖案擺放方式,如果有設定BackgroundImage屬性,則此屬性無效。
ControlBox-用來指定此表單的標題列是否包含控制方塊或是系統選單方塊。
Cursor-用來指定滑鼠在表單上移動時所顯示的游標。
HelpButton-用來指定此表單是否包含說明(Help)按鈕。
Icon-用來指定此表單的圖示。
MaximumSize-用來指表單所能調整的大小上限,若此屬性被設定為(0,0),則此表單沒有上限的限制。
MinimumSize-用來指表單所得調整的大小下限。
Opacity-表單透明度0%(透明)~100%(完全不透明)。
TopMost-是否要將此表單放置於未將TopMost屬性設置為True的表單上方。
WindowState-表單一開始顯示時的狀態。(Normal-預設值,表單開啟時初始大小取決於Size屬性的設定
Minimized-以最小化狀態放置於工作列上
Maximzed-表單初始時被開啟至最大)

表單相關(一)

一、調整表單的外觀與整體感
1、在設計階段使用開發工作所提供的屬性視窗(Property Grid)來設定屬性內容



2、較為複雜的屬性如Font或Size

//Font
this.btn_BankBranchUpdate.Font = new System.Drawing.Font("新細明體", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(136)));
//Size
this.btn_BankBranchUpdate.Size = new System.Drawing.Size(80, 32);


Name:同一Namespase屬一無二的的類別

新增並配置視窗表單

加入表單至專案中
一、在設計的階段將表單加入至專案中
1、至「專案」按右鍵-->加入新項目-->Windows Forms,輸入Form的名稱

二、在表單所屬的程式碼中存取該表單的方法與屬性
1、VB->Me,C#->this
'VB
Me.Text = "aaa";
//C#
this.Text ="aaa";

三、在執行的階段將表單加入至專案中
'VB
Dim myForm As Form1
myForm = New Form1()

//C#
FormSmartAudit FormSmartAudit = new FormSmartAudit();
FormSmartAudit.lbl_UserID.Text = this.textUserId.Text;
FormSmartAudit.lbl_Company.Text = this.txtCompany.Text;
FormSmartAudit.lbl_CompanyID.Text = this.txtCompanyID.Text;
FormSmartAudit.oc_Stock = this.oc_Stock;
FormSmartAudit.ShowDialog();

2009年8月8日 星期六

離線環境下的編輯作業-建立DataTable 物件

DataSet物件是SystemData命名空間中的一個物件,主要用途為提供應用程式一個於記憶體之中的資料儲存機制,其概念類似於資料庫之中的資料表。

<是否一定要將DataTable放入DataSet裡????>
  • DataSet物件裡可加入DataTable物件,並利用DataRelation物件將兩個DataTable物件關聯在一起
  • 亦可單獨使用DataTable,不涉及DataSet物件。
DataTable
-Rows
-DataRow1
-DataRow2

DataTable
-Columns
-DataColumn1
-DataColumn2
一、基本語法
1、建立DataTable物件
'宣告一個新的DataTable執行個體(於表單的全域範圍裡)
Private CustomersTable As New DataTable("Customers")
2、於DataSet加入DataTable
Private ds_Northwind As New DataSet("ds_Northwind")
Dim table_Customers As New DataTable("Customers")
ds_Northwind.Tables.Add(table_Customers)
3、於DataTable物件建立運算式資料行
  • 運算式(Expression)資料行可用來為DataTable加入額外的資料行,做為儲存現有資料行的計算結果用。
Private Sub ExpressionButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExpressionButton.Click
'建立一個新的DataColumn並設定其名稱以及資料型態
Dim ds_Northwind1 As New ds_Northwind

Dim TotalPriceColumn As New DataColumn("TotalPrice", GetType(System.Double))
'在該欄位的Expression屬性設定指定的運算式
TotalPriceColumn.Expression = ("UnitPrice * Quantity")
ds_Northwind1.Order_Details.Columns.Add(TotalPriceColumn)

Dim da_Order_Details As New SqlDataAdapter("select * FROM dbo.[Order Details]", cn_SQL)
da_Order_Details.Fill(ds_Northwind1.Order_Details)
'將此欄位加入至DataTable之中

TableGrid.DataSource = ds_Northwind1.Order_Details

End Sub
4、於DataTable物件建立自動遞增的資料行
  • 常見於訂單資料中必須是唯一(Unique)的但實際內容值為不重要的「訂單編號」(OrderID)。
'此欄位為自動遞增行
CustomersTable.Columns("CustomerID").AutoIncrement = True
'為自動遞增行指定起始值
CustomersTable.Columns("CustomerID").AutoIncrementSeed = 1
'指定每次遞增的數值
CustomersTable.Columns("CustomerID").AutoIncrementStep = 5
5、於DataTable物件加入條件約束
'建立ForeignKeyConstraint
Dim ds_Northwind As New NorthwindDataSet
Dim ForeignKey As New ForeignKeyConstraint(ds_Northwind.Customers.Columns("CustmoerID"), ds_Northwind.Orders.Columns("CustmoerID"))
ds_Northwind.Orders.Constraints.Add(ForeignKey)
6、如何建立唯一的條件約束
Dim ds_Northwind As New NorthwindDataSet
Dim Unique As New UniqueConstraint(ds_Northwind.Customers.Columns("CustomerID"))
ds_Northwind.Customers.Constraints.Add(Unique)
7、建立DataTable物件練習
  • 畫面
  • 程式原始碼
Imports System.data
Imports System.Data.SqlClient


Public Class frm_Ex4
'宣告一個新的DataTable執行個體(於表單的全域範圍裡)
Private CustomersTable As New DataTable("Customers")

Private Sub CreateTableButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreateTableButton.Click
'設定DataGridView以顯示資料表
TableGrid.DataSource = CustomersTable

'建立DataColumns物件並加入資料表的Columns集合之中,為資料表定義資料結構
CustomersTable.Columns.Add("CustomerID", Type.GetType("System.String"))
CustomersTable.Columns.Add("CustomerName", Type.GetType("System.String"))
CustomersTable.Columns.Add("ContactName", Type.GetType("System.String"))
CustomersTable.Columns.Add("ContactTitle", Type.GetType("System.String"))
CustomersTable.Columns.Add("Address", Type.GetType("System.String"))
CustomersTable.Columns.Add("City", Type.GetType("System.String"))
CustomersTable.Columns.Add("Region", Type.GetType("System.String"))
CustomersTable.Columns.Add("PostalCode", Type.GetType("System.String"))
CustomersTable.Columns.Add("Country", Type.GetType("System.String"))
CustomersTable.Columns.Add("Phone", Type.GetType("System.String"))
CustomersTable.Columns.Add("Fax", Type.GetType("System.String"))

'將CustomerID資料行設定為主索引鍵
Dim KeyColumns(1) As DataColumn
KeyColumns(0) = CustomersTable.Columns("CustomerID")
CustomersTable.PrimaryKey = KeyColumns


'將CustomerID以及CompanyName資料行設定為不允許空值
CustomersTable.Columns("CustomerID").AllowDBNull = False
''於DataTable物件建立自動遞增的資料行
'CustomersTable.Columns("CustomerID").AutoIncrement = True
'CustomersTable.Columns("CustomerID").AutoIncrementSeed = 1
'CustomersTable.Columns("CustomerID").AutoIncrementStep = 5

CustomersTable.Columns("CustomerName").AllowDBNull = False


End Sub

Private Sub AddRowButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddRowButton.Click
'建立一筆新的資料列(DataRow)
Dim CusRow As DataRow = CustomersTable.NewRow
With CusRow
.Item("CustomerID") = "ALFKI"
.Item("CustomerName") = "Alfreds Futterkiste"
.Item("ContactName") = "Maria Anders"
.Item("ContactTitle") = "Sales Representative"
.Item("Address") = "Obere Str. 57"
.Item("City") = "Berlin"
.Item("Region") = "Nothing"
.Item("PostalCode") = "12209"
.Item("Country") = "Germany"
.Item("Phone") = "030-0074321"
.Item("Fax") = "030-0076546"
End With
'將新增的資料列加入至資料表之中
CustomersTable.Rows.Add(CusRow)

End Sub
Private WithEvents cn_SQL As New SqlConnection("Data Source=PC9611107;Initial Catalog=Northwind;Integrated Security = True")
Private Sub ExpressionButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExpressionButton.Click
'建立一個新的DataColumn並設定其名稱以及資料型態
Dim ds_Northwind1 As New ds_Northwind

Dim TotalPriceColumn As New DataColumn("TotalPrice", GetType(System.Double))
'在該欄位的Expression屬性設定指定的運算式
TotalPriceColumn.Expression = ("UnitPrice * Quantity")
ds_Northwind1.Order_Details.Columns.Add(TotalPriceColumn)

Dim da_Order_Details As New SqlDataAdapter("select * FROM dbo.[Order Details]", cn_SQL)
da_Order_Details.Fill(ds_Northwind1.Order_Details)
'將此欄位加入至DataTable之中

TableGrid.DataSource = ds_Northwind1.Order_Details
'Dim ds_Northwind As New NorthwindDataSet
'Dim Unique As New UniqueConstraint(ds_Northwind.Customers.Columns("CustomerID"))
'ds_Northwind.Customers.Constraints.Add(Unique)

End Sub
End Class

離線環境下的編輯作業-建立DataTable 物件

DataSet物件是SystemData命名空間中的一個物件,主要用途為提供應用程式一個於記憶體之中的資料儲存機制,其概念類似於資料庫之中的資料表。

<是否一定要將DataTable放入DataSet裡????>
  • DataSet物件裡可加入DataTable物件,並利用DataRelation物件將兩個DataTable物件關聯在一起
  • 亦可單獨使用DataTable,不涉及DataSet物件。
DataTable
-Rows
-DataRow1
-DataRow2

DataTable
-Columns
-DataColumn1
-DataColumn2
一、基本語法
1、建立DataTable物件
'宣告一個新的DataTable執行個體(於表單的全域範圍裡)
Private CustomersTable As New DataTable("Customers")
2、於DataSet加入DataTable
Private ds_Northwind As New DataSet("ds_Northwind")
Dim table_Customers As New DataTable("Customers")
ds_Northwind.Tables.Add(table_Customers)
3、於DataTable物件建立運算式資料行
  • 運算式(Expression)資料行可用來為DataTable加入額外的資料行,做為儲存現有資料行的計算結果用。
Private Sub ExpressionButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExpressionButton.Click
'建立一個新的DataColumn並設定其名稱以及資料型態
Dim ds_Northwind1 As New ds_Northwind

Dim TotalPriceColumn As New DataColumn("TotalPrice", GetType(System.Double))
'在該欄位的Expression屬性設定指定的運算式
TotalPriceColumn.Expression = ("UnitPrice * Quantity")
ds_Northwind1.Order_Details.Columns.Add(TotalPriceColumn)

Dim da_Order_Details As New SqlDataAdapter("select * FROM dbo.[Order Details]", cn_SQL)
da_Order_Details.Fill(ds_Northwind1.Order_Details)
'將此欄位加入至DataTable之中

TableGrid.DataSource = ds_Northwind1.Order_Details

End Sub
4、於DataTable物件建立自動遞增的資料行
  • 常見於訂單資料中必須是唯一(Unique)的但實際內容值為不重要的「訂單編號」(OrderID)。
'此欄位為自動遞增行
CustomersTable.Columns("CustomerID").AutoIncrement = True
'為自動遞增行指定起始值
CustomersTable.Columns("CustomerID").AutoIncrementSeed = 1
'指定每次遞增的數值
CustomersTable.Columns("CustomerID").AutoIncrementStep = 5
5、於DataTable物件加入條件約束
'建立ForeignKeyConstraint
Dim ds_Northwind As New NorthwindDataSet
Dim ForeignKey As New ForeignKeyConstraint(ds_Northwind.Customers.Columns("CustmoerID"), ds_Northwind.Orders.Columns("CustmoerID"))
ds_Northwind.Orders.Constraints.Add(ForeignKey)
6、如何建立唯一的條件約束
Dim ds_Northwind As New NorthwindDataSet
Dim Unique As New UniqueConstraint(ds_Northwind.Customers.Columns("CustomerID"))
ds_Northwind.Customers.Constraints.Add(Unique)
7、建立DataTable物件練習
  • 畫面
  • 程式原始碼
Imports System.data
Imports System.Data.SqlClient


Public Class frm_Ex4
'宣告一個新的DataTable執行個體(於表單的全域範圍裡)
Private CustomersTable As New DataTable("Customers")

Private Sub CreateTableButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreateTableButton.Click
'設定DataGridView以顯示資料表
TableGrid.DataSource = CustomersTable

'建立DataColumns物件並加入資料表的Columns集合之中,為資料表定義資料結構
CustomersTable.Columns.Add("CustomerID", Type.GetType("System.String"))
CustomersTable.Columns.Add("CustomerName", Type.GetType("System.String"))
CustomersTable.Columns.Add("ContactName", Type.GetType("System.String"))
CustomersTable.Columns.Add("ContactTitle", Type.GetType("System.String"))
CustomersTable.Columns.Add("Address", Type.GetType("System.String"))
CustomersTable.Columns.Add("City", Type.GetType("System.String"))
CustomersTable.Columns.Add("Region", Type.GetType("System.String"))
CustomersTable.Columns.Add("PostalCode", Type.GetType("System.String"))
CustomersTable.Columns.Add("Country", Type.GetType("System.String"))
CustomersTable.Columns.Add("Phone", Type.GetType("System.String"))
CustomersTable.Columns.Add("Fax", Type.GetType("System.String"))

'將CustomerID資料行設定為主索引鍵
Dim KeyColumns(1) As DataColumn
KeyColumns(0) = CustomersTable.Columns("CustomerID")
CustomersTable.PrimaryKey = KeyColumns


'將CustomerID以及CompanyName資料行設定為不允許空值
CustomersTable.Columns("CustomerID").AllowDBNull = False
''於DataTable物件建立自動遞增的資料行
'CustomersTable.Columns("CustomerID").AutoIncrement = True
'CustomersTable.Columns("CustomerID").AutoIncrementSeed = 1
'CustomersTable.Columns("CustomerID").AutoIncrementStep = 5

CustomersTable.Columns("CustomerName").AllowDBNull = False


End Sub

Private Sub AddRowButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddRowButton.Click
'建立一筆新的資料列(DataRow)
Dim CusRow As DataRow = CustomersTable.NewRow
With CusRow
.Item("CustomerID") = "ALFKI"
.Item("CustomerName") = "Alfreds Futterkiste"
.Item("ContactName") = "Maria Anders"
.Item("ContactTitle") = "Sales Representative"
.Item("Address") = "Obere Str. 57"
.Item("City") = "Berlin"
.Item("Region") = "Nothing"
.Item("PostalCode") = "12209"
.Item("Country") = "Germany"
.Item("Phone") = "030-0074321"
.Item("Fax") = "030-0076546"
End With
'將新增的資料列加入至資料表之中
CustomersTable.Rows.Add(CusRow)

End Sub
Private WithEvents cn_SQL As New SqlConnection("Data Source=PC9611107;Initial Catalog=Northwind;Integrated Security = True")
Private Sub ExpressionButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExpressionButton.Click
'建立一個新的DataColumn並設定其名稱以及資料型態
Dim ds_Northwind1 As New ds_Northwind

Dim TotalPriceColumn As New DataColumn("TotalPrice", GetType(System.Double))
'在該欄位的Expression屬性設定指定的運算式
TotalPriceColumn.Expression = ("UnitPrice * Quantity")
ds_Northwind1.Order_Details.Columns.Add(TotalPriceColumn)

Dim da_Order_Details As New SqlDataAdapter("select * FROM dbo.[Order Details]", cn_SQL)
da_Order_Details.Fill(ds_Northwind1.Order_Details)
'將此欄位加入至DataTable之中

TableGrid.DataSource = ds_Northwind1.Order_Details
'Dim ds_Northwind As New NorthwindDataSet
'Dim Unique As New UniqueConstraint(ds_Northwind.Customers.Columns("CustomerID"))
'ds_Northwind.Customers.Constraints.Add(Unique)

End Sub
End Class

離線環境下的編輯作業-建立DataSet物件

DataSet物件是SystemData命名空間中的一個物件,主要用途為提供應用程式一個於記憶體之中的資料暫存機制
DataSet
-Tables
-DataTable1
-DataTable2
經由DataRelation物件來描述DataTable物件之間的關係。

一、DataSet物件
1、一般可區分為兩種不同型態的DataSet物件
  • 具型別(Typed):具型別的DataSet結構描述則是源自於.xsd檔,並包含明確的具型別集合。
  • 不 具型別(Untyped):自行建立DataTable(Untyped DataTable)物件,將DataTable加入至DataSet的Tables 集合中,手動地為DataSet建立資料結構描述(Schema)。可以職由集合的索引值來存取該DataSet中不具型別的DataTable及其 DataColumn物件。
2、Visual Studio提供了三種不同的方式建立DataSet物件
  • 於程式編輯器中撰寫程式宣告DataSet物件,建立空的DataSet物件後再另行建立DataTable物件,最後再選擇性加入DataRelation物件。
  • 選擇使用DataSet設計工具以及資料來源組態精靈(Data Source Configuration Wizard)兩者其中之一來協助建立,從選擇或建立資料連結開始逐步選取資料庫裡的資料對象,進而建立具型別的DataSet,最後再產生相關的程式碼。
  • 用滑鼠自工具箱拖曳一個DataSet物件到表單之上,再利用對應Table以及Column集合編輯為所屬的DataSet建立結構描述。
3、基本語法
  • 建立DataSet物件 Private ds_Northwind As New DataSet("ds_Northwind")
  • 建立DataTable並加入DataSet裡
Dim table_Customers As New DataTable("Customers")
table_Customers.Columns.Add("CustomerID", Type.GetType("System.Int32"))
table_Customers.Columns(0).AutoIncrement = True
table_Customers.Columns(0).Unique = True

table_Customers.Columns.Add("CustomerName", Type.GetType("System.String"))

Dim table_Orders As New DataTable("Orders")
table_Orders.Columns.Add("OrderID", Type.GetType("System.Int32"))
table_Orders.Columns(0).AutoIncrement = True
table_Orders.Columns.Add("OrderProduct", Type.GetType("System.String"))
table_Orders.Columns.Add("CustomerID", Type.GetType("System.Int32"))

ds_Northwind.Tables.Add(table_Customers)
ds_Northwind.Tables.Add(table_Orders)
  • 新增父表單資料
Dim newRow1 As DataRow = ds_Northwind.Tables("Customers").NewRow()
newRow1.Item("CustomerName") = "Jone"
ds_Northwind.Tables("Customers").Rows.Add(newRow1)

Dim newRow2 As DataRow = ds_Northwind.Tables("Customers").NewRow()
newRow2.Item("CustomerName") = "Amy"
ds_Northwind.Tables("Customers").Rows.Add(newRow2)

Dim newRow3 As DataRow = ds_Northwind.Tables("Customers").NewRow()
newRow3.Item("CustomerName") = "Jone"
ds_Northwind.Tables("Customers").Rows.Add(newRow3)
  • 新增子表單資料
Dim newRow4 As DataRow = ds_Northwind.Tables("Orders").NewRow()
newRow4.Item("OrderProduct") = "AProduct"
newRow4.Item("CustomerID") = 0
ds_Northwind.Tables("Orders").Rows.Add(newRow4)

Dim newRow5 As DataRow = ds_Northwind.Tables("Orders").NewRow()
newRow5.Item("OrderProduct") = "BProduct"
newRow5.Item("CustomerID") = 1
ds_Northwind.Tables("Orders").Rows.Add(newRow5)

Dim newRow6 As DataRow = ds_Northwind.Tables("Orders").NewRow()
newRow6.Item("OrderProduct") = "CProduct"
newRow6.Item("CustomerID") = 2
ds_Northwind.Tables("Orders").Rows.Add(newRow6)
  • 建立關聯
Dim customersOrders As New DataRelation("dr_customersOrders", table_Customers.Columns("CustomerID"), table_Orders.Columns("CustomerID"))

ds_Northwind.Relations.Add(customersOrders)
  • 將資料丟入DataGridView
DataGridView2.Columns.Clear()
DataGridView2.Rows.Clear()
For Each col As DataColumn In ds_Northwind.Tables("Customers").Columns

DataGridView2.Columns.Add(col.ColumnName, col.ColumnName)
Next

For Each row As DataRow In ds_Northwind.Tables("Customers").Rows
DataGridView2.Rows.Add(CInt(row(0)), row(1).ToString)
Next
  • 取得子資料列
ListView1.Items.Clear()
For Each Customer_Row As DataRow In ds_Northwind.Tables("Customers").Rows(e.RowIndex).GetChildRows("dr_customersOrders")
ListView1.Items.Add(Customer_Row("OrderProduct"))
Next
  • 其它
  • 合併DataSet:內容
ds_SaleHistory.Merge(ds_OldSales,True,MissingSchemaAction.Ignore)
  • 複製DataSet內容
Dim ds_Copy as new DataSet
ds_Copy = ds_Original.Copy

二、利用DataSet設計工具建立DataSet
1、首先建立一個名為DataSetDesignerExample的Windows Form
2、自「專案」功能表裡選擇執行「加入新項目」選項
3、選擇資料集樣版,並命名為NorthwindDataSet.xsd
4、自伺服器總管裡挑選Customers資料表及Order資料表,並將其拖曳至設計工具上。
完成拖曳Customers以及Orders資料表至DataSet設計工具上後的設計具看起來應接近下圖。

5、在進行後續步驟之前請先建置(Build)本專案
6、用滑鼠拖曳一個ListBox控制項至Frm_Ex1上,並命名為CustomersListbox
7、用滑鼠拖曳一個Button控制項至Frm_Ex1上
  • Name = GetCustomersButton
  • Text=Get Customers
8、用滑鼠雙擊Get Customers按鈕,並將下列程式碼加入GetCustomersButton_Click事件處理常式中
Imports System.Data
Imports System.Data.SqlClient

Private Sub GetCustomersButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetCustomersButton.Click
'建立一個具型別的Northwind DataSet執行個體
Dim ds_Northwind As New NorthwindDataSet

'建立一個CustomerTableAdapter的執行個體
Dim da_Customers As New NorthwindDataSetTableAdapters.CustomersTableAdapter

'呼叫Fill方法為Customers DataTable載入所有的客戶資料紀錄
da_Customers.Fill(ds_Northwind.Customers)

'將所有Customers資料表的CompanyName欄位加入ListBox之中
For Each ds_NW_customers As NorthwindDataSet.CustomersRow In ds_Northwind.Customers.Rows
CustomersListBox.Items.Add(ds_NW_customers.CompanyName)
Next

End Sub
9、執行結果



三、利用資料來源組態精靈建立具型別的DataSet
1、建立一個名為DatasourceWizardExample的Windows Form
2、至「資料」功能表裡選擇執行「加入新資料來源」選項以開啟資料來源組態精靈
3、選擇「選擇資料來源類型(Choose A Data Source Type)」畫面上的預設選項「資料庫」,並按「下一步」

4、清單控制項裡顯的是伺服器總管裡所有可用的資料連接。如下圖所示。於畫面上選取Northwind範例資料庫連接,或是建立新的資料連結,並按「下一步」。再按「是」(若出現詢問對話方塊時)以確認將資料庫連接加入至此專案。

5、選擇欲使用的資料連接後,將可選擇是否將設定儲存至應用程式組態檔(Application Configuration File)之中,在此請保留預設選項並按「下一步」。
6、展開「選擇你的資料庫物件」畫面上的「資料表」節點,並選取Customers資料表


7、按「完成」以結束此精靈的操作。此具型別的DataSet將自動被加到專案中。資料來源視窗裡將出現此資料來源,利用此資料來源將可快速開發資料繫結的表單。
8、切換至frm_Ex2的設計畫面,並自「資料」功能表裡選擇「顯示資料來源」選項。
9、自資料來源視窗中將Customers節點拖曳至frm_Ex2之上

自資料來源視窗拖曳至Customer資料表後,Visual Studio將於表單之中自動載入Customers DataTable資料的程式碼
10、執行結果


四、設定不具型別的DataSet物件
1、DataSet重要屬性
  • 自工具箱-->資料區段裡拖曳一個DataSet至Form之中
  • 自「加入資料集」對話方塊裡,選擇「不具型別資料集」並按「確定」
  • 選取DataSet1執行個體,切換到屬性視窗上的Table屬性,開啟「資料表集合編輯器(Table Collection Editor)」
  • 選取DataSet1執行個體,切換到屬性視窗上的Relations屬性,開啟「關聯性集合編輯器(Relations Collection Editor)」
  • Table下選取Columns屬性,開啟「資料行集合編輯器(Columns Collection Editor)」
2、DataGridView
3、ListView
4、相關程式碼
Public Class frm_Ex3

Private Sub FillDataSetButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FillDataSetButton.Click
Dim newRow As DataRow = DataSet1.Tables("Categories").NewRow()
newRow.Item("CategoryName") = "Beverages"
DataSet1.Tables("Categories").Rows.Add(newRow)

Dim newRow1 As DataRow = DataSet1.Tables("Categories").NewRow()
newRow1.Item("CategoryName") = "Condiments"
DataSet1.Tables("Categories").Rows.Add(newRow1)

Dim newRow2 As DataRow = DataSet1.Tables("Categories").NewRow()
newRow2.Item("CategoryName") = "Seafood"
DataSet1.Tables("Categories").Rows.Add(newRow2)

Dim newRow3 As DataRow = DataSet1.Tables("Products").NewRow()
newRow3.Item("CategoryID") = 1
newRow3.Item("ProductName") = "Chai"
DataSet1.Tables("Products").Rows.Add(newRow3)

Dim newRow4 As DataRow = DataSet1.Tables("Products").NewRow()
newRow4.Item("CategoryID") = 2
newRow4.Item("ProductName") = "Aniseed Syrup"
DataSet1.Tables("Products").Rows.Add(newRow4)

Dim newRow5 As DataRow = DataSet1.Tables("Products").NewRow()
newRow5.Item("CategoryID") = 3
newRow5.Item("ProductName") = "Ikura"
DataSet1.Tables("Products").Rows.Add(newRow5)

Dim newRow6 As DataRow = DataSet1.Tables("Products").NewRow()
newRow6.Item("CategoryID") = 1
newRow6.Item("ProductName") = "Chang"
DataSet1.Tables("Products").Rows.Add(newRow6)

Dim newRow7 As DataRow = DataSet1.Tables("Products").NewRow()
newRow7.Item("CategoryID") = 2
newRow7.Item("ProductName") = "Chef Anton's Gumbo Mix"
DataSet1.Tables("Products").Rows.Add(newRow7)

Dim newRow8 As DataRow = DataSet1.Tables("Products").NewRow()
newRow8.Item("CategoryID") = 3
newRow8.Item("ProductName") = "Boston Crab Meat"
DataSet1.Tables("Products").Rows.Add(newRow8)
End Sub

Private Sub frm_Ex3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1.DataSource = DataSet1.Tables("Categories")
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End Sub

Private Sub DataGridView1_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
'取得使用者選取的資料列的CategoryID欄位資料
Dim categoryID As Integer = CInt(DataGridView1.SelectedRows(0).Cells("CategoryID").Value.ToString())

'取得該資料的DataRow
Dim rows() As DataRow = DataSet1.Tables("Categories").Select("CategoryID =" & categoryID)
ListView1.Items.Clear()

'呼叫GetChildRows方法以取得相關聯的產品資料
For Each r As DataRow In rows(0).GetChildRows("DR_CategoriesProducts")
ListView1.Items.Add(r.Item("ProductName").ToString())
Next
End Sub
End Class

視窗表單進階主題-實作拖放功能

  • DragDrop:常見用法是透過按下滑鼠左鍵對資料進行抓取。
  • 一個字串或一個物件,:滑鼠左鍵保持按下的狀態不變(Drag),並移至有接收資料能力的控制項上,放開滑鼠(Drop)以完成資料傳送。
  • 基本上是一種事件導向的作業過程。

一、實作拖放功能時涉及來源控制項事件
  • MouseDown:發生於滑鼠左鍵按下,且游標位於某個控制貢上時,一般而言,都會在此對DoDragDrop方法做呼叫。
  • GiveFeedBack:提供使用者一個自定游標的機會。
  • QueryContinueDrag:允許拖曳來源可以決某一拖曳事件是否應該取消。
二、實作施放功能時涉及目標控制項的事件
  • DragEnter:發生於某個物件被施丈至某個控制項的範圍內時,此事件的處理常式將會接收到一個DragEventArg事件。
  • DragOver:發生於某個物件被拖丈經過某個控制項上時。此事件的處理常式將會接收到一個DragEventArgs物件。
  • DragDrop:發生於滑鼠按鍵在某個目標控制項上被釋放時。此事件的處理常式將會接收到一個DragEventArgs物件。
  • DragLeave:發生於某個物件被拖曳離開某個控制項範圍時。
PS:另外需呼叫DoDragDrop方法,及目標控制項的AllowDrop屬性必須設定為True。

三、拖曳作業的一般順序
1、透過呼叫來源控制項的DoDragDrop方法來初始化。
通常是在MouseDown的事件處理常式中完成。

Private Sub TextBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy)
End Sub
2、GiveFeedBack及ueryContinueDrag亦發生在這個時間點。
  • GiveFeedBack:提供使用者一個自定游標的機會。
  • QueryContinueDrag:允許拖曳來源可以決某一拖曳事件是否應該取消。
3、滑鼠游標被拖曳並在目標控制項上移動。任何AllowDrop=True皆為潛的置放目標。當滑鼠游標移入某個AllowDrop=True的控制項時,將會觸發該控制項的DragEnter事件。該事件處理常式會接收到DragEventArgs物件則可用來檢驗並決定是否適合該目標控制項。若接收,則可將DragEventArgs物件的Effect屬性設成適當的內容值。
Private Sub TextBox2_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter
If e.Data.GetData(DataFormats.Text) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
4、使用者於某一個有效的目標控制項上釋放滑鼠按鍵,進而觸發DragDrop事件。
DragDrop事件處理常式中的程式碼於獲取拖曳的資料後,即可於此目標控制項中做出適當的處理動作。
Private Sub TextBox2_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
TextBox2.Text = e.Data.GetData(DataFormats.Text).ToString
End Sub

5、DragDropEffects列舉的所有成員
使用地方:
  • TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy)
  • e.Effect = DragDropEffects.Copy
成員:
  • All:資料從拖曳來源中複製、移除,並在置放目標中捲動。
  • Dopy:複製資料到目標之中。
  • Link:連結資料至目標之中。
  • Move:將資料移至目標之中。
  • None:目標不接受資料。
  • Scroll:將要開始進行捲動,或是目前有捲動作業正在目標中進行。
1、
Private Sub SourceTreeView_ItemDrag(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles SourceTreeView.ItemDrag
SourceTreeView.DoDragDrop(e.Item, DragDropEffects.Move)
End Sub
2、
Private Sub DistTreeView_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DistTreeView.DragEnter
e.Effect = DragDropEffects.Move

End Sub
3、DistTreeView.AllowDrop=True
4
Private Sub DistTreeView_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DistTreeView.DragDrop
If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", False) Then

Dim SourceNode As TreeNode
Dim apoint As Point
Dim DictNode As TreeNode

apoint = CType(sender, TreeView).PointToClient(New Point(e.X, e.Y))
DictNode = CType(sender, TreeView).GetNodeAt(apoint)
SourceNode = CType(e.Data.GetData("System.Windows.Forms.TreeNode"), TreeNode)

DictNode.Nodes.Add(SourceNode.Clone)
DictNode.Expand()
SourceNode.Remove()

End If
End Sub

視窗表單進階主題-實作拖放功能

  • DragDrop:常見用法是透過按下滑鼠左鍵對資料進行抓取。
  • 一個字串或一個物件,:滑鼠左鍵保持按下的狀態不變(Drag),並移至有接收資料能力的控制項上,放開滑鼠(Drop)以完成資料傳送。
  • 基本上是一種事件導向的作業過程。

一、實作拖放功能時涉及來源控制項事件
  • MouseDown:發生於滑鼠左鍵按下,且游標位於某個控制貢上時,一般而言,都會在此對DoDragDrop方法做呼叫。
  • GiveFeedBack:提供使用者一個自定游標的機會。
  • QueryContinueDrag:允許拖曳來源可以決某一拖曳事件是否應該取消。
二、實作施放功能時涉及目標控制項的事件
  • DragEnter:發生於某個物件被施丈至某個控制項的範圍內時,此事件的處理常式將會接收到一個DragEventArg事件。
  • DragOver:發生於某個物件被拖丈經過某個控制項上時。此事件的處理常式將會接收到一個DragEventArgs物件。
  • DragDrop:發生於滑鼠按鍵在某個目標控制項上被釋放時。此事件的處理常式將會接收到一個DragEventArgs物件。
  • DragLeave:發生於某個物件被拖曳離開某個控制項範圍時。
PS:另外需呼叫DoDragDrop方法,及目標控制項的AllowDrop屬性必須設定為True。

三、拖曳作業的一般順序
1、透過呼叫來源控制項的DoDragDrop方法來初始化。
通常是在MouseDown的事件處理常式中完成。

Private Sub TextBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy)
End Sub
2、GiveFeedBack及ueryContinueDrag亦發生在這個時間點。
  • GiveFeedBack:提供使用者一個自定游標的機會。
  • QueryContinueDrag:允許拖曳來源可以決某一拖曳事件是否應該取消。
3、滑鼠游標被拖曳並在目標控制項上移動。任何AllowDrop=True皆為潛的置放目標。當滑鼠游標移入某個AllowDrop=True的控制項時,將會觸發該控制項的DragEnter事件。該事件處理常式會接收到DragEventArgs物件則可用來檢驗並決定是否適合該目標控制項。若接收,則可將DragEventArgs物件的Effect屬性設成適當的內容值。
Private Sub TextBox2_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter
If e.Data.GetData(DataFormats.Text) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
4、使用者於某一個有效的目標控制項上釋放滑鼠按鍵,進而觸發DragDrop事件。
DragDrop事件處理常式中的程式碼於獲取拖曳的資料後,即可於此目標控制項中做出適當的處理動作。
Private Sub TextBox2_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
TextBox2.Text = e.Data.GetData(DataFormats.Text).ToString
End Sub

5、DragDropEffects列舉的所有成員
使用地方:
  • TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy)
  • e.Effect = DragDropEffects.Copy
成員:
  • All:資料從拖曳來源中複製、移除,並在置放目標中捲動。
  • Dopy:複製資料到目標之中。
  • Link:連結資料至目標之中。
  • Move:將資料移至目標之中。
  • None:目標不接受資料。
  • Scroll:將要開始進行捲動,或是目前有捲動作業正在目標中進行。
1、
Private Sub SourceTreeView_ItemDrag(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles SourceTreeView.ItemDrag
SourceTreeView.DoDragDrop(e.Item, DragDropEffects.Move)
End Sub
2、
Private Sub DistTreeView_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DistTreeView.DragEnter
e.Effect = DragDropEffects.Move

End Sub
3、DistTreeView.AllowDrop=True
4
Private Sub DistTreeView_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DistTreeView.DragDrop
If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", False) Then

Dim SourceNode As TreeNode
Dim apoint As Point
Dim DictNode As TreeNode

apoint = CType(sender, TreeView).PointToClient(New Point(e.X, e.Y))
DictNode = CType(sender, TreeView).GetNodeAt(apoint)
SourceNode = CType(e.Data.GetData("System.Windows.Forms.TreeNode"), TreeNode)

DictNode.Nodes.Add(SourceNode.Clone)
DictNode.Expand()
SourceNode.Remove()

End If
End Sub

視窗表單進階主題-實作MDI表單

MDI應用程式是一種具有管理單一父表單下所有子表單的應用程式
一、建立一個MID父表單
1、建立一個新的表單應用程式
2、將設定該表單設定為父表單(MainForm)

IsMdiContainer = True
3、自工具箱拉一個MenuStrip進主表單
<=在MenuStrip設定File和New,其它可以先略過


二、建立一個MID子表單
1、建立一個新的表單應用程式(ChildForm)

2、在父表單(MainForm)的MenuStrip1的File->New中,雙點兩下滑鼠
程式設定

Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
Dim aChild As New frm_Ex3_2
'將MdiParent屬性設定成父表單
aChild.MdiParent = Me
aChild.Show()
End Sub


三、正在使用中的MDI子表單(MainForm)
1、ActiveMdiChild:取得正在使用中的MDI子表單
Dim ActiveForm As frm_Ex3_2 = Me.ActiveMdiChild
2、修改正在使用中MDI子表單
Dim ActiveForm As frm_Ex3_2 = Me.ActiveMdiChild
If ActiveForm Is Nothing = False Then
'在正在使用表單中要做的程式
ActiveForm.TextBox1.Text = "This is a Test"
End If
End Sub
3、對MDI表單進行排列
父表單可透過LayoutMdi(MdiLayout.列舉屬性)進行設定
Me.LayoutMdi(MdiLayout.Cascade)
Me.LayoutMdi(MdiLayout.TileHorizontal)
Me.LayoutMdi(MdiLayout.TileVertical)

視窗表單進階主題-全球化及當地語系化

一、文化特性:是以代表目前語言的Cultrue code做為表示方式
Cultrue code可以用表示語言及地區
ex:
en:英文
en-CA:英文-加拿大地區
可以用來指定Character Set
ex:
uz-UZ-Cyrl:分別表示英文、烏茲別克文和斯拉夫文

二、建立當地化語系表單
1、決定是否該表單被當地語系化
  • Form1.Localizable = true
2、選定適當語言
  • Form1.Language=Default:會針對表單上所有的UI屬性或控制項進行編緝,並自動建立Resource File(表單名稱.resx)
  • Form1.Language=特定語言:會針對此語言建立適當的Resource File(表單名稱.Cultrue code.resx)
3、針對表單上所有的UI屬性或控制項進行編緝Resource File
4、根據CurrentThread.CurrentUICulture的設定來管理資源檔的讀取作業

三、更改目前文化特性
Imports System.Threading.Thread
Imports System.Globalization

1、CurrentThread.CurrentCulture:用來格式化 資料的文化特性
2、CurrentThread.CurrentUICulture:用來決定執行階段載入當地語系化的表單(Localized Form)

ex1:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("zh-TW")
CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo("zh-TW")
frm_Ex2_2.Show()
End Sub
ex2:由控制台的地區語系設定
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim ci As New CultureInfo(CultureInfo.CurrentCulture.Name)
CurrentThread.CurrentCulture = ci
CurrentThread.CurrentUICulture = ci
Form2.Show()
End Sub
四、其它
利用RightToLeft= true以實現控制項由右至左的功能
RightToLeft= true和RightToLeftLayout=true以對整個表單的項制項配置進行反轉