Imports Infragistics.Shared
Imports Infragistics.Win
Imports Infragistics.Win.UltraWinGrid
Imports Infragistics.Win.CalcEngine
Imports Infragistics.Win.UltraWinCalcManager
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Ensure that the form contains an UltraCalcManager and two textboxes,
' one named textBox1 and the other named textBox2.
Dim calcSettings As CalcSettings = New CalcSettings()
' This formula will multiply the value of textBox2 by 2.
calcSettings.Formula = "2 * [textBox2]"
calcSettings.PropertyName = "Text"
Me.UltraCalcManager1.SetCalcSettings(Me.TextBox1, calcSettings)
calcSettings = New CalcSettings()
calcSettings.PropertyName = "Text"
' Treat the values of the textBox2 as double. Text property of TextBox is
' a string type and since textBox2 is a source of value to a formula, indicate
' to the calc-manager that the value should be treated as a double rather
' than as a string.
calcSettings.TreatAsType = GetType(Double)
Me.UltraCalcManager1.SetCalcSettings(Me.TextBox2, calcSettings)
End Sub
Private Sub UltraCalcManager1_ParseValue(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinCalcManager.ParseValueEventArgs) Handles ultraCalcManager1.ParseValue
' ParseValue gets fired on all controls that are source of values in formulas.
If e.Control Is Me.textBox2 Then
' Get the text of the textBox2.
Dim text As String = Me.textBox2.Text
If 0 = text.Length Then
' If the textbox is empty, then you can return an error. By default the
' calc manager considers empty strings to be 0.
e.Value = New UltraCalcValue(New UltraCalcErrorValue(UltraCalcErrorCode.Num, "Not a Number"))
Else
' Otherwise parse the text to a double and return that as the value.
' The calc manager by default uses InvariantCulture to parse text into
' numeric values. Typically you want to use the current culture to parse
' text into numeric values. ParseValue event is especially useful for that.
e.Value = New UltraCalcValue(Double.Parse(text))
End If
e.Handled = True
End If
End Sub
Private Sub UltraCalcManager1_FormatValue(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinCalcManager.FormatValueEventArgs) Handles ultraCalcManager1.FormatValue
' FormatValue gets fired on all controls that are target of formula evaluations.
If e.Control Is Me.textBox1 Then
If e.CalcValue.IsError Then
' There was an error in formula evaluation.
e.Value = e.CalcValue.ToErrorValue().Message
ElseIf e.CalcValue.IsDBNull Or e.CalcValue.IsNull Then
' Result is DBNull or Null.
e.Value = "(NULL)"
Else
Dim doubleVal As Double
If e.CalcValue.ToDouble(doubleVal) Then
' ToDouble returns true if the value was a double.
e.Value = CType(doubleVal, Decimal).ToString("c")
Else
' Otherwise convert the value to a string using current culture.
e.Value = e.CalcValue.ToString(System.Globalization.CultureInfo.CurrentCulture)
End If
End If
End If
End Sub