RadNumericButton - Format change

1 Answer 111 Views
NumericUpDown
Sathya
Top achievements
Rank 1
Sathya asked on 03 May 2022, 11:04 AM

Hi Team,

I have two questions, I am using RadNumericUpDownButton for getting time input. So I have used two separate numeric button to get hours and minutes.

  • I want to show the hours and minutes in two numeric digits like 01, 02, 08. Any solution to achieve this?
  • And I want get AM and PM with same RadNumericUpDownButton. Is there any way to do this?

1 Answer, 1 is accepted

Sort by
0
Accepted
Stenly
Telerik team
answered on 06 May 2022, 06:21 AM

Hello Sathya,

The RadNumericUpDown control does not support the desired result out of the box, however, you could still achieve it by writing some custom code. 

Regarding the first question, create a new class that derives from the RadNumericUpDown control and override the FormatDisplay and FormatEdit methods. These methods are responsible for formatting the text when in edit and in display mode. Then, you could modify the value by calling the ToString() method with this format ("00"):

public class MyCustomUpDown : RadNumericUpDown
{
    public override string FormatDisplay()
	{
		if (this.Value.HasValue)
		{
			return this.Value.Value.ToString("00");                
		}
	
		return base.FormatDisplay();
	}
	
	public override string FormatEdit()
	{
		if (this.Value.HasValue)
		{
			return this.Value.Value.ToString("00");
		}
	
		return base.FormatEdit();
	}
}

Concerning the second question, you could add additional logic to these methods, in order to achieve the desired result. For example, the following code snippet will display am and pm:

public override string FormatDisplay()
{
    if (this.Value.HasValue)
    {
        if (this.Value.Value <= 12)
        {
            return $"{this.Value.Value.ToString("00")} am";
        }

        var calculatedValue = (this.Value.Value - 12) == 12 ? 0 : (this.Value.Value - 12);

        return $"{calculatedValue.ToString("00")} pm";
    }

    return base.FormatDisplay();
}

public override string FormatEdit()
{
    if (this.Value.HasValue)
    {
        if (this.Value.Value <= 12)
        {
            return $"{this.Value.Value.ToString("00")} am";
        }

        var calculatedValue = (this.Value.Value - 12) == 12 ? 0 : (this.Value.Value - 12);

        return $"{calculatedValue.ToString("00")} pm";
    }

    return base.FormatEdit();
}

With this said, could you give this suggestion a try?

Regards,
Stenly
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
NumericUpDown
Asked by
Sathya
Top achievements
Rank 1
Answers by
Stenly
Telerik team
Share this question
or