カスタムコントロールフィールド形式Windownフォームは、データバインディングを実行します
dotBlogsラベル:ノートとVB.NET、Winフォーム、バインディング
久しぶりの書き込みWindowsフォームプログラム、および突然尋ねた、と最初に間違った項目クラスXDを駆動します。問題はこれです:
テキストボックスフォーム上のフィールドは、テキスト属性は、データのパケットのデータベースのバックグラブからブールフィールドに対応する、データをバインドします。デフォルトはTrue / Falseの表示されますが、フォーマットは、お客様が見たい:「はい/いいえ」、実行する方法?
プロセスは、私は最終的にもメモを作成、フォーマットイベントSystem.Windows.Forms.Bindingオブジェクト参照を介して処分された、について話をあまりないです。
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
実行画面:
--------
特別なことは何もありません-
しかし、唯一のいくつかのメモ
オリジナル:ビッグボックス カスタムWindowsフォームコントロールフィールドのフォーマットは、データバインディングを実行します