Windows Form控制項欄位執行資料繫結時自訂格式

  • 23464
  • 0

Windown Form控制項欄位執行資料繫結時自訂格式

dotBlogs 的標籤:,,,

好久沒寫 Windows Form 程式了,忽然被問到,一開始還開錯專案類別 XD。問題是這樣:

Form 上有一個 TextBox 欄位,Text 屬性會做資料繫結,對應從資料庫抓回來的一筆資料之 Boolean 型別欄位。預設會顯示 True/False,但客戶想要看到的格式:「是/否」,該怎麼做?

過程就不多談,最後我是透過 System.Windows.Forms.Binding 物件的 Format 事件處理掉,提供參考,也做個筆記:

Form1.vb

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim obj = New With {.IsDismissed = True}
        AddTextBoxTextBinding(txtTrue, obj)
        obj.IsDismissed = False
        AddTextBoxTextBinding(txtFalse, obj)
        obj.IsDismissed = Nothing
        AddTextBoxTextBinding(txtNothing, obj)
    End Sub

    Private Sub AddTextBoxTextBinding(txt As TextBox, obj As Object)
        Dim myBinding = New Binding("Text", obj, "IsDismissed")
        AddHandler myBinding.Format, AddressOf myBinding_Format
        txt.DataBindings.Add(myBinding)
    End Sub

    Private Sub myBinding_Format(sender As Object, e As ConvertEventArgs)
        If e.DesiredType = GetType(String) Then
            Dim result As Boolean
            Boolean.TryParse(e.Value.ToString, result)
            e.Value = If(result, "是", "否")
        End If
    End Sub

End Class

Form1.Designer.vb

Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form 覆寫 Dispose 以清除元件清單。
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    '為 Windows Form 設計工具的必要項
    Private components As System.ComponentModel.IContainer

    '注意: 以下為 Windows Form 設計工具所需的程序
    '可以使用 Windows Form 設計工具進行修改。
    '請不要使用程式碼編輯器進行修改。
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.txtTrue = New System.Windows.Forms.TextBox()
        Me.txtFalse = New System.Windows.Forms.TextBox()
        Me.txtNothing = New System.Windows.Forms.TextBox()
        Me.SuspendLayout()
        '
        'txtTrue
        '
        Me.txtTrue.Location = New System.Drawing.Point(32, 31)
        Me.txtTrue.Name = "txtTrue"
        Me.txtTrue.Size = New System.Drawing.Size(100, 25)
        Me.txtTrue.TabIndex = 0
        '
        'txtFalse
        '
        Me.txtFalse.Location = New System.Drawing.Point(32, 77)
        Me.txtFalse.Name = "txtFalse"
        Me.txtFalse.Size = New System.Drawing.Size(100, 25)
        Me.txtFalse.TabIndex = 1
        '
        'txtNothing
        '
        Me.txtNothing.Location = New System.Drawing.Point(32, 131)
        Me.txtNothing.Name = "txtNothing"
        Me.txtNothing.Size = New System.Drawing.Size(100, 25)
        Me.txtNothing.TabIndex = 2
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 15.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(282, 253)
        Me.Controls.Add(Me.txtNothing)
        Me.Controls.Add(Me.txtFalse)
        Me.Controls.Add(Me.txtTrue)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents txtTrue As System.Windows.Forms.TextBox
    Friend WithEvents txtFalse As System.Windows.Forms.TextBox
    Friend WithEvents txtNothing As System.Windows.Forms.TextBox

End Class

執行畫面:

161442

--------
沒什麼特別的~
不過是一些筆記而已