0
\$\begingroup\$

I use an integer field to indicate the value of the wanted year and I set a maximum and minimum limit for it and I wanted a function to be called when the wanted year value changes to display the available months for that year.

But when I click on the integer field to start writing the year, it immediately changes to the minimum limit because it considers that the value has already changed.

Example: if the minimum year is 1900 and the value of the year is 2000 and I want to write a new value, for example 2024, then when I press the number 2, It consider these a change so it will be fixed at the minimum.

So my question: Is there an event reference that let me finish writing then compare the old value and the new value to find out if there a change or not?

private void CreateGUI()
{
    VisualElement root = rootVisualElement;
    var visualTree = Resources.Load<VisualTreeAsset>("CelestialBodyPathCreatorWindow");

    VisualElement tree = visualTree.Instantiate();
    root.Add(tree);

    Year = root.Q<IntegerField>("YearInteger");
    Year.RegisterValueChangedCallback<int>(OnYearValueChanged);
}

private void OnYearValueChanged(ChangeEvent<int> evt)
{
    Year.value = Mathf.Clamp(Year.value, MinimumYear, MaximumYear);

    SetAvailableMonths();

}

private void SetAvailableMonths()
{
    if (Year.value == MinimumYear)
    {
        MinimumMonth = 2;
        MaximumMonth = 12;
    }
    else if (Year.value == MaximumYear)
    {
        MinimumMonth = 1;
        MaximumMonth = 11;
    }
    else if (Year.value != MinimumYear && Year.value != MaximumYear)
    {
        MinimumMonth = 1;
        MaximumMonth = 12;
    }

    Month.value = Mathf.Clamp(Month.value, MinimumMonth, MaximumMonth);
}
\$\endgroup\$
2
  • \$\begingroup\$ It does exactly what you tell it to do. You can't clamp the year to a min value since the year is not completed yet. It also makes not much sense to display available months (or calculate them when the year is not entered fully). You could either only clamp and calculate when the value in the field is 4 digits with the OnChangedEvent or you use a different event like FocusOutEvent \$\endgroup\$
    – Zibelas
    Commented Jul 7 at 15:19
  • \$\begingroup\$ Yes, that's it what I need, FocusOutEvent is the event that I wanted. Thank you @Zibelas \$\endgroup\$
    – Ahmed Dyaa
    Commented Jul 7 at 17:03

0

You must log in to answer this question.

Browse other questions tagged .