Custom control field format Windown Form perform data binding
dotBlogs label: VB.NET, with the Notes, WinForm, Binding
Long time no write Windows Form program, and suddenly asked, and initially driving the wrong item class XD. The problem is this:
A field on the TextBox Form, Text attributes do bind data, corresponding to the Boolean field from a database back grab of a packet of data. The default will be displayed True / False, but the format the customer wants to see: "Yes / No", how to do?
The process is not much to talk about, I was finally disposed of through the Format event System.Windows.Forms.Binding object reference, also make a note of:
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 以清除组件清单。
_
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 设计工具进行修改。
'请不要使用程序编辑器进行修改。
_
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
Execution screen:
--------
nothing special -
but only some notes
Original: Big Box Custom Windows Form control field format to perform data binding