HOW TO:將資料繫結至 MaskedTextBox 控制項
更新:2007 年 11 月
您可以將資料繫結至 MaskedTextBox 控制項,如同您可以對其他任何 Windows Form 控制項所做的一樣。但是,如果資料庫中的資料格式不符合遮罩定義所預期的格式,您將需要重新格式化資料。下列程序示範如何使用 Binding 類別的 Format 和 Parse 事件執行這項作業,將個別的電話號碼和電話分機資料庫欄位顯示為可編輯的單一欄位。
下列程序需要存取 SQL Server 資料庫,並安裝 Northwind 範例資料庫。
若要將資料繫結至 MaskedTextBox 控制項
建立新的 Windows Form 專案。
將兩個 TextBox 控制項拖曳到表單上;將它們命名為 FirstName 和 LastName。
將 MaskedTextBox 控制項拖曳到表單上;將它命名為 PhoneMask。
將 PhoneMask 的 Mask 屬性設定為 (000) 000-0000 x9999。
將下列命名空間匯入加入表單。
using System.Data.SqlClient;
Imports System.Data.SqlClient
在表單上按一下滑鼠右鍵,並選擇 [檢視程式碼]。將此程式碼放置在表單類別中的任何位置。
Binding currentBinding, phoneBinding; DataSet employeesTable = new DataSet(); SqlConnection sc; SqlDataAdapter dataConnect; private void Form1_Load(object sender, EventArgs e) { DoMaskBinding(); } private void DoMaskBinding() { try { sc = new SqlConnection("Data Source=CLIENTUE;Initial Catalog=NORTHWIND;Integrated Security=SSPI"); sc.Open(); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } dataConnect = new SqlDataAdapter("SELECT * FROM Employees", sc); dataConnect.Fill(employeesTable, "Employees"); // Now bind MaskedTextBox to appropriate field. Note that we must create the Binding objects // before adding them to the control - otherwise, we won't get a Format event on the // initial load. try { currentBinding = new Binding("Text", employeesTable, "Employees.FirstName"); firstName.DataBindings.Add(currentBinding); currentBinding = new Binding("Text", employeesTable, "Employees.LastName"); lastName.DataBindings.Add(currentBinding); phoneBinding =new Binding("Text", employeesTable, "Employees.HomePhone"); // We must add the event handlers before we bind, or the Format event will not get called // for the first record. phoneBinding.Format += new ConvertEventHandler(phoneBinding_Format); phoneBinding.Parse += new ConvertEventHandler(phoneBinding_Parse); phoneMask.DataBindings.Add(phoneBinding); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } }
Dim WithEvents CurrentBinding, PhoneBinding As Binding Dim EmployeesTable As New DataSet() Dim sc As SqlConnection Dim DataConnect As SqlDataAdapter Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load DoMaskedBinding() End Sub Private Sub DoMaskedBinding() Try sc = New SqlConnection("Data Source=SERVERNAME;Initial Catalog=NORTHWIND;Integrated Security=SSPI") sc.Open() Catch ex As Exception MessageBox.Show(ex.Message) Exit Sub End Try DataConnect = New SqlDataAdapter("SELECT * FROM Employees", sc) DataConnect.Fill(EmployeesTable, "Employees") ' Now bind MaskedTextBox to appropriate field. Note that we must create the Binding objects ' before adding them to the control - otherwise, we won't get a Format event on the ' initial load. Try CurrentBinding = New Binding("Text", EmployeesTable, "Employees.FirstName") firstName.DataBindings.Add(CurrentBinding) CurrentBinding = New Binding("Text", EmployeesTable, "Employees.LastName") lastName.DataBindings.Add(CurrentBinding) PhoneBinding = New Binding("Text", EmployeesTable, "Employees.HomePhone") PhoneMask.DataBindings.Add(PhoneBinding) Catch ex As Exception MessageBox.Show(ex.Message) Application.Exit() End Try End Sub
為 Format 和 Parse 事件加入事件處理常式,從繫結的 DataSet 組合及分隔 PhoneNumber 和 Extension 欄位。
private void phoneBinding_Format(Object sender, ConvertEventArgs e) { String ext; DataRowView currentRow = (DataRowView)BindingContext[employeesTable, "Employees"].Current; if (currentRow["Extension"] == null) { ext = ""; } else { ext = currentRow["Extension"].ToString(); } e.Value = e.Value.ToString().Trim() + " x" + ext; } private void phoneBinding_Parse(Object sender, ConvertEventArgs e) { String phoneNumberAndExt = e.Value.ToString(); int extIndex = phoneNumberAndExt.IndexOf("x"); String ext = phoneNumberAndExt.Substring(extIndex).Trim(); String phoneNumber = phoneNumberAndExt.Substring(0, extIndex).Trim(); //Get the current binding object, and set the new extension manually. DataRowView currentRow = (DataRowView)BindingContext[employeesTable, "Employees"].Current; // Remove the "x" from the extension. currentRow["Extension"] = ext.Substring(1); //Return the phone number. e.Value = phoneNumber; }
Private Sub PhoneBinding_Format(ByVal sender As Object, ByVal e As ConvertEventArgs) Handles PhoneBinding.Format Dim Ext As String Dim CurrentRow As DataRowView = CType(Me.BindingContext(EmployeesTable, "Employees").Current, DataRowView) If (CurrentRow("Extension") Is Nothing) Then Ext = "" Else Ext = CurrentRow("Extension").ToString() End If e.Value = e.Value.ToString().Trim() & " x" & Ext End Sub Private Sub PhoneBinding_Parse(ByVal sender As Object, ByVal e As ConvertEventArgs) Handles PhoneBinding.Parse Dim PhoneNumberAndExt As String = e.Value.ToString() Dim ExtIndex As Integer = PhoneNumberAndExt.IndexOf("x") Dim Ext As String = PhoneNumberAndExt.Substring(ExtIndex).Trim() Dim PhoneNumber As String = PhoneNumberAndExt.Substring(0, ExtIndex).Trim() ' Get the current binding object, and set the new extension manually. Dim CurrentRow As DataRowView = CType(Me.BindingContext(EmployeesTable, "Employees").Current, DataRowView) ' Remove the "x" from the extension. CurrentRow("Extension") = CObj(Ext.Substring(1)) ' Return the phone number. e.Value = PhoneNumber End Sub
加入兩個 Button 控制項至表單。將它們命名為 previousButton 和 nextButton。按兩下每個按鈕加入 Click 事件處理常式,並依照下列程式碼範例所示,在事件處理常式中填入。
private void previousButton_Click(object sender, EventArgs e) { BindingContext[employeesTable, "Employees"].Position = BindingContext[employeesTable, "Employees"].Position - 1; } private void nextButton_Click(object sender, EventArgs e) { BindingContext[employeesTable, "Employees"].Position = BindingContext[employeesTable, "Employees"].Position + 1; }
Private Sub PreviousButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PreviousButton.Click Me.BindingContext(EmployeesTable, "Employees").Position = Me.BindingContext(EmployeesTable, "Employees").Position - 1 End Sub Private Sub NextButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NextButton.Click Me.BindingContext(EmployeesTable, "Employees").Position = Me.BindingContext(EmployeesTable, "Employees").Position + 1 End Sub
執行範例。編輯資料,並使用 [上一個] 和 [下一個] 按鈕,查看資料是否適當地保存在 DataSet。
範例
下列程式碼範例是完成前面程序所產成的完整程式碼清單。
Imports System.Data.SqlClient
Public Class Form1
Dim WithEvents CurrentBinding, PhoneBinding As Binding
Dim EmployeesTable As New DataSet()
Dim sc As SqlConnection
Dim DataConnect As SqlDataAdapter
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DoMaskedBinding()
End Sub
Private Sub DoMaskedBinding()
Try
sc = New SqlConnection("Data Source=localhost;Initial Catalog=NORTHWIND;Integrated Security=SSPI")
sc.Open()
Catch ex As Exception
MessageBox.Show(ex.Message)
Application.Exit()
End Try
DataConnect = New SqlDataAdapter("SELECT * FROM Employees", sc)
DataConnect.Fill(EmployeesTable, "Employees")
' Now bind MaskedTextBox to appropriate field. Note that we must create the Binding objects
' before adding them to the control - otherwise, we won't get a Format event on the
' initial load.
Try
CurrentBinding = New Binding("Text", EmployeesTable, "Employees.FirstName")
firstName.DataBindings.Add(CurrentBinding)
CurrentBinding = New Binding("Text", EmployeesTable, "Employees.LastName")
lastName.DataBindings.Add(CurrentBinding)
PhoneBinding = New Binding("Text", EmployeesTable, "Employees.HomePhone")
PhoneMask.DataBindings.Add(PhoneBinding)
Catch ex As Exception
MessageBox.Show(ex.Message)
Application.Exit()
End Try
End Sub
Private Sub PhoneBinding_Format(ByVal sender As Object, ByVal e As ConvertEventArgs) Handles PhoneBinding.Format
Dim Ext As String
Dim CurrentRow As DataRowView = CType(Me.BindingContext(EmployeesTable, "Employees").Current, DataRowView)
If (CurrentRow("Extension") Is Nothing) Then
Ext = ""
Else
Ext = CurrentRow("Extension").ToString()
End If
e.Value = e.Value.ToString().Trim() & " x" & Ext
End Sub
Private Sub PhoneBinding_Parse(ByVal sender As Object, ByVal e As ConvertEventArgs) Handles PhoneBinding.Parse
Dim PhoneNumberAndExt As String = e.Value.ToString()
Dim ExtIndex As Integer = PhoneNumberAndExt.IndexOf("x")
Dim Ext As String = PhoneNumberAndExt.Substring(ExtIndex).Trim()
Dim PhoneNumber As String = PhoneNumberAndExt.Substring(0, ExtIndex).Trim()
' Get the current binding object, and set the new extension manually.
Dim CurrentRow As DataRowView = CType(Me.BindingContext(EmployeesTable, "Employees").Current, DataRowView)
' Remove the "x" from the extension.
CurrentRow("Extension") = CObj(Ext.Substring(1))
' Return the phone number.
e.Value = PhoneNumber
End Sub
Private Sub NextButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NextButton.Click
Me.BindingContext(EmployeesTable, "Employees").Position = Me.BindingContext(EmployeesTable, "Employees").Position + 1
End Sub
Private Sub PreviousButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PreviousButton.Click
Me.BindingContext(EmployeesTable, "Employees").Position = Me.BindingContext(EmployeesTable, "Employees").Position - 1
End Sub
End Class
#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SqlClient;
#endregion
namespace MaskedTextBoxDataCSharp
{
partial class Form1 : Form
{
Binding currentBinding, phoneBinding;
DataSet employeesTable = new DataSet();
SqlConnection sc;
SqlDataAdapter dataConnect;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DoMaskBinding();
}
private void DoMaskBinding()
{
try
{
sc = new SqlConnection("Data Source=localhost;Initial Catalog=NORTHWIND;Integrated Security=SSPI");
sc.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
dataConnect = new SqlDataAdapter("SELECT * FROM Employees", sc);
dataConnect.Fill(employeesTable, "Employees");
// Now bind MaskedTextBox to appropriate field. Note that we must create the Binding objects
// before adding them to the control - otherwise, we won't get a Format event on the
// initial load.
try
{
currentBinding = new Binding("Text", employeesTable, "Employees.FirstName");
firstName.DataBindings.Add(currentBinding);
currentBinding = new Binding("Text", employeesTable, "Employees.LastName");
lastName.DataBindings.Add(currentBinding);
phoneBinding =new Binding("Text", employeesTable, "Employees.HomePhone");
// We must add the event handlers before we bind, or the Format event will not get called
// for the first record.
phoneBinding.Format += new ConvertEventHandler(phoneBinding_Format);
phoneBinding.Parse += new ConvertEventHandler(phoneBinding_Parse);
phoneMask.DataBindings.Add(phoneBinding);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
private void phoneBinding_Format(Object sender, ConvertEventArgs e)
{
String ext;
DataRowView currentRow = (DataRowView)BindingContext[employeesTable, "Employees"].Current;
if (currentRow["Extension"] == null)
{
ext = "";
} else
{
ext = currentRow["Extension"].ToString();
}
e.Value = e.Value.ToString().Trim() + " x" + ext;
}
private void phoneBinding_Parse(Object sender, ConvertEventArgs e)
{
String phoneNumberAndExt = e.Value.ToString();
int extIndex = phoneNumberAndExt.IndexOf("x");
String ext = phoneNumberAndExt.Substring(extIndex).Trim();
String phoneNumber = phoneNumberAndExt.Substring(0, extIndex).Trim();
//Get the current binding object, and set the new extension manually.
DataRowView currentRow = (DataRowView)BindingContext[employeesTable, "Employees"].Current;
// Remove the "x" from the extension.
currentRow["Extension"] = ext.Substring(1);
//Return the phone number.
e.Value = phoneNumber;
}
private void previousButton_Click(object sender, EventArgs e)
{
BindingContext[employeesTable, "Employees"].Position = BindingContext[employeesTable, "Employees"].Position - 1;
}
private void nextButton_Click(object sender, EventArgs e)
{
BindingContext[employeesTable, "Employees"].Position = BindingContext[employeesTable, "Employees"].Position + 1;
}
}
}
編譯程式碼
建立 Visual C# 或 Visual Basic 專案。
將 TextBox 和 MaskedTextBox 控制項加入表單,如前面程序中所述。
為專案的預設表單開啟原始程式碼檔。
以前面「程式碼」區段中所列的程式碼,取代這個檔案中的原始程式碼。
編譯應用程式。