如何:将数据绑定到 MaskedTextBox 控件
更新:2007 年 11 月
可以将数据绑定到 MaskedTextBox 控件,就像可以绑定到任何其他 Windows 窗体控件一样。但是,如果数据库中数据的格式与掩码定义所需的格式不匹配,您将需要重新设置数据的格式。下面的过程演示如何使用 Binding 类的 Format 和 Parse 事件重新设置数据的格式,以便将独立的电话号码和分机号数据库字段显示为单个可编辑字段。
下面的过程要求您对安装了 Northwind 示例数据库的 SQL Server 数据库具有访问权限。
将数据绑定到 MaskedTextBox 控件
创建新的 Windows 窗体项目。
将两个 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 控件。
打开项目默认窗体的源代码文件。
使用以上“代码”部分中列出的代码替换此文件中的源代码。
编译该应用程序。