K
k
I created a custom textbox I named "NumericTextBox" by inheriting
from System.Windows.Forms.Textbox so I could filter keystroke input..
After changing the custom property "Precision", I desire to delete a certain
amount of digits from the "me.text". However, after changing the text
programatically, moving the focus to another control changes the text back
to the previous text.
Although calling EndCurrentEdit() on the BindingManagerBase prevents the
text from reverting, using EndCurrentEdit() in this fashion is not helpful.
This is because I am using the NumericTextBox on a form with "OK" and
"Cancel" buttons which use a currencymanager to apply or abandon changes via
CM.EndCurrentEdit or CM.CancelCurrentEdit.
The NumericTextBox needs to handle the digit deletion and application in a
generic way because it is used on multiple forms accross the project.
Here is a snippet of the NumericTextBox class:
Thanks,
k
'////////////////////////////////////////////
'// In the inherited textbox
'////////////////////////////////////////////
<Description("If IntegerOnly is FALSE, this property sets _
the allowed number of digits to the right of the decimal."), _
Browsable(True), _
Category("Validation"), _
DefaultValue(2)> _
Property Precision() As Integer
Get
Return m_Precision
End Get
Set(ByVal Value As Integer)
If Value >= 0 Then
m_Precision = Value
'// Look for a decimal
Dim i As Integer = Me.Text.IndexOf(".")
'// If we've a decimal AND Precision is larger than allowed
If i > -1 AndAlso (Me.Text.Length - (i + 1)) > Me.m_Precision Then
'// Trim off extra, disallowed digits
Me.Text = Me.Text.Remove(i + Me.m_Precision + 1, _
(Me.Text.Length - i - 1) - Me.m_Precision)
End If
End If
End Set
End Property
'////////////////////////////////////////////
'// And on the containing form
'////////////////////////////////////////////
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCancel.Click
cm.CancelCurrentEdit()
Me.m_parent.Focus()
Me.Close()
End Sub
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnOK.Click
If ValidateForm() Then
cm.EndCurrentEdit()
Me.m_parent.Focus()
Me.Close()
End If
End Sub
Private Sub SetForm()
'// set a currency manager for this form to manage the appropriate
dataset tables
cm = CType(Me.BindingContext(Me.m_parent.DsMngMenu1.Taxes),
CurrencyManager)
'// If appropriate, Synchronize our Form to the record currently
selected in parent form
If m_actionToTake = formAction.editTax Then
'// set a currency manager for the owner form to find Form1's
current record position
cmOwner = CType(CType(Me.Owner, myns.mngMenu).BindingContext _
(Me.m_parent.DsMngMenu1.Taxes), CurrencyManager)
'// synchronize the table record positions between form1 and form2
Me.cm.Position = Me.cmOwner.Position
Else
'// create a new Tax
cm.AddNew()
End If
'// Set our databindings
Me.tbTaxName.DataBindings.Add("Text", Me.m_parent.DsMngMenu1.Taxes,
"TaxName")
Me.tbTaxSource.DataBindings.Add("Text", Me.m_parent.DsMngMenu1.Taxes,
"TaxSource")
Me.tbTaxValue.DataBindings.Add("Text", Me.m_parent.DsMngMenu1.Taxes,
"TaxValue")
Me.cbxTaxType.DataBindings.Add("SelectedItem",
Me.m_parent.DsMngMenu1.Taxes, "TaxType")
If Me.m_actionToTake = formAction.newTax Then
Me.cbxTaxType.SelectedIndex = 0
End Sub
from System.Windows.Forms.Textbox so I could filter keystroke input..
After changing the custom property "Precision", I desire to delete a certain
amount of digits from the "me.text". However, after changing the text
programatically, moving the focus to another control changes the text back
to the previous text.
Although calling EndCurrentEdit() on the BindingManagerBase prevents the
text from reverting, using EndCurrentEdit() in this fashion is not helpful.
This is because I am using the NumericTextBox on a form with "OK" and
"Cancel" buttons which use a currencymanager to apply or abandon changes via
CM.EndCurrentEdit or CM.CancelCurrentEdit.
The NumericTextBox needs to handle the digit deletion and application in a
generic way because it is used on multiple forms accross the project.
Here is a snippet of the NumericTextBox class:
Thanks,
k
'////////////////////////////////////////////
'// In the inherited textbox
'////////////////////////////////////////////
<Description("If IntegerOnly is FALSE, this property sets _
the allowed number of digits to the right of the decimal."), _
Browsable(True), _
Category("Validation"), _
DefaultValue(2)> _
Property Precision() As Integer
Get
Return m_Precision
End Get
Set(ByVal Value As Integer)
If Value >= 0 Then
m_Precision = Value
'// Look for a decimal
Dim i As Integer = Me.Text.IndexOf(".")
'// If we've a decimal AND Precision is larger than allowed
If i > -1 AndAlso (Me.Text.Length - (i + 1)) > Me.m_Precision Then
'// Trim off extra, disallowed digits
Me.Text = Me.Text.Remove(i + Me.m_Precision + 1, _
(Me.Text.Length - i - 1) - Me.m_Precision)
End If
End If
End Set
End Property
'////////////////////////////////////////////
'// And on the containing form
'////////////////////////////////////////////
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCancel.Click
cm.CancelCurrentEdit()
Me.m_parent.Focus()
Me.Close()
End Sub
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnOK.Click
If ValidateForm() Then
cm.EndCurrentEdit()
Me.m_parent.Focus()
Me.Close()
End If
End Sub
Private Sub SetForm()
'// set a currency manager for this form to manage the appropriate
dataset tables
cm = CType(Me.BindingContext(Me.m_parent.DsMngMenu1.Taxes),
CurrencyManager)
'// If appropriate, Synchronize our Form to the record currently
selected in parent form
If m_actionToTake = formAction.editTax Then
'// set a currency manager for the owner form to find Form1's
current record position
cmOwner = CType(CType(Me.Owner, myns.mngMenu).BindingContext _
(Me.m_parent.DsMngMenu1.Taxes), CurrencyManager)
'// synchronize the table record positions between form1 and form2
Me.cm.Position = Me.cmOwner.Position
Else
'// create a new Tax
cm.AddNew()
End If
'// Set our databindings
Me.tbTaxName.DataBindings.Add("Text", Me.m_parent.DsMngMenu1.Taxes,
"TaxName")
Me.tbTaxSource.DataBindings.Add("Text", Me.m_parent.DsMngMenu1.Taxes,
"TaxSource")
Me.tbTaxValue.DataBindings.Add("Text", Me.m_parent.DsMngMenu1.Taxes,
"TaxValue")
Me.cbxTaxType.DataBindings.Add("SelectedItem",
Me.m_parent.DsMngMenu1.Taxes, "TaxType")
If Me.m_actionToTake = formAction.newTax Then
Me.cbxTaxType.SelectedIndex = 0
End Sub