Events in Xamarin NumericUpDown (SfNumericUpDown)

23 Jul 20214 minutes to read

Value changed event in SfNumericUpDown

You can perform any operation when changing the value of SfNumericUpDown using the ValueChanged event. The ValueChanged event returns the changed value in NumericUpDown.

For example you can restrict the NumericUpDown value if it exceed’s greater than 3 digits using following code.

<syncfusion:SfNumericUpDown x:Name="NumericUpDown" ValueChangeMode="OnKeyFocus" ValueChanged="Handle_ValueChanged" Value="123"  />
SfNumericUpDown NumericUpDown=new SfNumericUpDown();
NumericUpDown.Value = 123;
NumericUpDown.ValueChangeMode = ValueChangeMode.OnKeyFocus;
NumericUpDown.ValueChanged += Handle_ValueChanged;
this.Content=NumericUpDown;

string updateValue = "";
void Handle_ValueChanged(object sender, Syncfusion.SfNumericUpDown.XForms.ValueEventArgs e)
{
    if (e.Value != null && e.Value.ToString().Length <= 3)
    {
        updateValue = e.Value.ToString();
    }
    else
    {
       NumericUpDown.Value = updateValue.ToString();
    }
}

Interactivity : ValueChangeMode

The ValueChangeMode property is used to mention when a value needs to be updated, either in key pressed or focus lost state. When ValueChangeMode is set to OnKeyFocus, the value is updated with each key press. When ValueChangeMode is set to OnLostFocus, the value is updated when the control loses focus or the focus is moved to the next control. ValueChangeMode includes the following options:

  1. OnKeyFocus
  2. OnLostFocus

OnKeyFocus

<syncfusion:SfNumericUpDown ValueChanged="SfNumericUpDown_ValueChanged" ValueChangeMode="OnKeyFocus"/>
SfNumericUpDown NumericUpDown = new SfNumericUpDown();
NumericUpDown.ValueChanged += SfNumericUpDown_ValueChanged;
NumericUpDown.ValueChangeMode = ValueChangeMode.OnKeyFocus;
this.Content = NumericUpDown;

private void SfNumericUpDown_ValueChanged(object sender, ValueEventArgs e)
{
    DisplayAlert("OnKeyFocus", e.Value.ToString(), "OK");
}

Display the control with OnKeyFocus

OnLostFocus

<syncfusion:SfNumericUpDown ValueChanged="SfNumericUpDown_ValueChanged" ValueChangeMode="OnLostFocus"/>
SfNumericUpDown NumericUpDown = new SfNumericUpDown();
NumericUpDown.ValueChanged += SfNumericUpDown_ValueChanged;
NumericUpDown.ValueChangeMode = ValueChangeMode.OnLostFocus;
this.Content = NumericUpDown;

private void SfNumericUpDown_ValueChanged(object sender, ValueEventArgs e)
{
    DisplayAlert("OnLostFocus", e.Value.ToString(), "OK");
}

Display the value with OnLostFocus

Completed event in SfNumericUpDown

The Completed event occurs when users finalize the text in the SfNumericUpDown by pressing the return key(enter, ok) on the keyboard.

<syncfusion:SfNumericUpDown Completed="Handle_Completed”/>
SfNumericUpDown NumericUpDown=new SfNumericUpDown();
NumericUpDown.Completed += Handle_Completed;
this.Content=NumericUpDown;

void Handle_Completed(object sender, System.EventArgs e)
{
  System.Diagnostics.Debug.WriteLine("Completed");     
}

Focus changed event in SfNumericUpDown

The FocusChanged event occurs when the control gets the focus and loses the focus.

<syncfusion:SfNumericUpDown FocusChanged="SfNumericUpDown_FocusChanged"/>
SfNumericUpDown NumericUpDown = new SfNumericUpDown();
NumericUpDown.FocusChanged += SfNumericUpDown_FocusChanged;
this.Content = NumericUpDown;

void SfNumericUpDown_FocusChanged(object sender, Syncfusion.SfNumericUpDown.XForms.FocusEventArgs e)
{
    System.Diagnostics.Debug.WriteLine("FocusChanged");
}