Flexible TreeView 5.1.9 Help
Data format customization

Use these methods to customize the node control text format and appearance.


DisplayFormat

All text node controls display bound data as text using the ToString method, but this is not useful in cases, when you need to customize the text appearance. To change node control's output text format, use the DisplayFormat property. To do that, enable the Enabled property and provide a text format in the FormatText property. After that, bound data will be displayed using the String.Format method, so FormatText's format is identical to the String.Format method's formatting rules. The String.Format method will be called with one parameter, so to insert a data value into the output text, use {0} placeholder in your format.
For instance, to display a number with two decimal digits, use below code example.

// Custom node class.
class NodeEx : Node
{
    public decimal Salary;
}
 
NodeTextBox tb = new NodeTextBox();
tb.AttachTo(tree);
NodeNumeric num = new NodeNumeric();
num.DataFieldName = "Salary";
num.DisplayFormat.Enabled = true;
// Display the bound number value with two decimal points.
num.DisplayFormat.FormatText = "{0:N2}";
num.AttachTo(tree);
 
NodeEx node = new NodeEx();
node.Text = "John Smith";
node.Salary = 5000.6786M;
node.AttachTo(tree);
' Custom node class.
Class NodeEx
    Inherits Node
    Public Salary As Decimal
End Class

Dim tb As New NodeTextBox()
tb.AttachTo(tree)
Dim num As New NodeNumeric()
num.DataFieldName = "Salary"
num.DisplayFormat.Enabled = True
' Display the bound number value with two decimal points.
num.DisplayFormat.FormatText = "{0:N2}"
num.AttachTo(tree)

Dim node As New NodeEx()
node.Text = "John Smith"
node.Salary = 5000.6786D
node.AttachTo(tree)

  • Your bound data will not be changed when the text formatting is enabled and it will appear unformatted in the edit control.
  • You can exclude the data placeholder {0} and show a text that's not bound to your node control.

 

NodeControlFormatValue event

The DisplayFormat node control property is useful when you want to apply a static custom text format. Also, Flexible TreeView provides the possibility to dynamically format the node control's text. To do that, use the NodeControlFormatValue treeview event as shown below. Note that the NodeControlFormatValue event will be raised only when custom formatting is disabled (DisplayFormat.Enabled=false).

Example:

// We have the NodeNumeric node control to display a file size and want to display ‘Kb' when the file size is greater than 1024.
NodeNumeric num = new NodeNumeric();
 
// 1. Disable custom formatting for the node control. Note that it is disabled by default!
Num.DisplayFormat.Enabled = false;
 
// 2. Subscribe to the NodeControlFormatValue treeview event.
tree.NodeControlFormatValue += tree_NodeControlFormatValue;
 
// 3. Event handler.
void tree_NodeControlFormatValue(FlexibleTreeView pTreeview, FormatValueEventArgs pArgs)
{
  // provide the custom formatting only for our node control.
  if (pArgs.NodeControl == num)
  {
    decimal val = (decimal)pArgs.Value;
    if (val > 1024)
    {
      // format the specified value. This text will be displayed in the node control.
      pArgs.FormattedValue = string.Format("{0} Kb.", val/1024);
    }
  }
}
' We have the NodeNumeric node control to display a file size and want to display ‘Kb' when the file size is greater than 1024.
Dim num__1 As New NodeNumeric()
' 1. Disable custom formatting for the node control. Note that it is disabled by default!
Num.DisplayFormat.Enabled = False

' 2. Subscribe to the NodeControlFormatValue treeview event.
tree.NodeControlFormatValue += tree_NodeControlFormatValue

' 3. Event handler.
Private Sub tree_NodeControlFormatValue(pTreeview As FlexibleTreeView, pArgs As FormatValueEventArgs)
    ' provide the custom formatting only for our node control.
    If pArgs.NodeControl = num Then
        Dim val As Decimal = CDec(pArgs.Value)
        If val > 1024 Then
            ' format the specified value. This text will be displayed in the node control.
            pArgs.FormattedValue = String.Format("{0} Kb.", val / 1024)
        End If
    End If
End Sub

 

 


Copyright © 2006-2016 ARMSoft

www.FlexibleTreeView.com