Obtenir la valeur d'un control TextBox avec un format personnalisé
Bonjour à tous, j'ai 2 procédures qui se ressemblent et qui font à prêt la même chose, j'aimerais faire qu'une seule procédure ou fonction.
Dans un cas (Bind_Parse) je "parse" la valeur d'un contrôle ((L'argument sender est un objet binding), dans l'autre cas Get_TrueCtrlValue j'obtiens sa valeur directement à partir de sa propriété (L'argument Sender est un objet Control).
Et comment obtenir l'objet binding à partir du control et vice versa (obtenir l'objet Control à partir de l'objet Binding) ?
Quelle est la meilleure solution parmis les 2 qui se prêtent le mieux à ce que je voudrais faire .
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| Public Sub Bind_Parse(ByVal sender As Object, ByVal e As System.Windows.Forms.ConvertEventArgs, Optional ByVal FormatStyle As FormatStyle = FormatStyle.NotSet)
Dim Bind As Binding = CType(sender, Binding)
Dim ctrl As Control = Bind.Control
'Dim ctrl As Control = CType(sender, Control)
If TypeOf (ctrl) Is TextBox Then
Dim c As TextBox = CType(ctrl, TextBox)
If FormatStyle = Main.FormatStyle.Monnaie Then e.Value = Decimal.Parse(CType(e.Value, String), Globalization.NumberStyles.Currency)
'ColChangingValue = c.Text
ElseIf TypeOf (ctrl) Is ComboBox Then
Dim c As ComboBox = CType(ctrl, ComboBox)
ElseIf TypeOf (ctrl) Is CheckBox Then
Dim c As CheckBox = CType(ctrl, CheckBox)
ElseIf TypeOf (ctrl) Is DateTimePicker Then
Dim c As DateTimePicker = CType(ctrl, DateTimePicker)
End If
End Sub |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| Public Function Get_TrueCtrlValue(ByVal sender As Object) As Object
Dim Value As Object = Nothing
If TypeOf (sender) Is TextBox Then
Dim c As TextBox = CType(sender, TextBox)
Value = c.Text
ElseIf TypeOf (sender) Is ComboBox Then
Dim c As ComboBox = CType(sender, ComboBox)
Value = c.Text
ElseIf TypeOf (sender) Is CheckBox Then
Dim c As CheckBox = CType(sender, CheckBox)
Value = c.Checked
ElseIf TypeOf (sender) Is DateTimePicker Then
Dim c As DateTimePicker = CType(sender, DateTimePicker)
If CStr(c.Tag) = "DBNull" Then ' Astuce pour retourner Nothing si le champs correspondant = DBNull
Value = Nothing
Else
Value = c.Value
End If
End If
Return Value
End Function |
J'espère que vous pourrez m'aider.