RadTextBox bound to a Decimal Data Type - Empty Value

1 Answer 83 Views
TextBox
Mark
Top achievements
Rank 2
Bronze
Bronze
Veteran
Mark asked on 29 Mar 2022, 05:31 PM

I have a RadTextBox data bound to an Decimal field (12,9).   When the user hits the "DELETE" button on their keyboard to blank out that field's value, the value is cleared out, but the user cannot exit the control until the user enters a value of some kind (Alpha or Numeric). This is not the desired outcome that we would like.  What I would like to happen is for the bound field's value to be set to null.  Can anyone help me with code sample please.  I have tried also using a MaskedEditBox with a MaskType of  Numeric and Mask of F9, but this also gives us undesired results (0.0000000) when user clicks the DELETE button on their keyboard.

Thank you.Text

1 Answer, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 30 Mar 2022, 12:24 PM

Hello Mark,

You should be able to use the Binding's Parse event handler to handle the case when the user clears the text in the textbox. In the event handler, you can check if the Value property from the event arguments is an empty string. If yes, set the same property to Null. Here is a full code snippet that I used to test your case.

public partial class RadForm1 : Telerik.WinControls.UI.RadForm
{
    public RadForm1()
    {
        InitializeComponent();
        Item item = new Item();
        this.radTextBox1.DataBindings.Add("Text", item, "DiscountThreshold",true,DataSourceUpdateMode.OnPropertyChanged);
        this.radTextBox1.DataBindings[0].Parse += RadForm1_Parse;
    }

    private void RadForm1_Parse(object sender, ConvertEventArgs e)
    {
        if (e.Value != null && string.IsNullOrEmpty(e.Value.ToString()))
            e.Value = null;
    }
}

public class Item : ViewModelBase
{
    private decimal? _discountThreshold;
    public Item()
    {
        DiscountThreshold = 500; 
    }
    public decimal? DiscountThreshold
    {
        get { return _discountThreshold; }
        set 
        { 
            _discountThreshold = value;
            this.OnPropertyChanged("DiscountThreshold"); 
        }
    }
}

Hope you find this helpful.

Regards,
Dinko
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Mark
Top achievements
Rank 2
Bronze
Bronze
Veteran
commented on 30 Mar 2022, 12:27 PM

I will try this code, I tried something similar to this, but it didn't work.   I will give this a try. In the end, my goal is to create a custom control that contains this code so I don't have to replicate every where I need it.   

 

Thank you.

Dinko | Tech Support Engineer
Telerik team
commented on 30 Mar 2022, 12:55 PM

Give this approach a try and share how it goes. As for your second requirement, you could create a method that accepts several parameters used to add the binding to the control. This way you can just call for each control. Another approach is to create a custom class that derives from RadTextBox control. Inside the custom class, you can add the Parse event handler and use this custom class across your application.
Mark
Top achievements
Rank 2
Bronze
Bronze
Veteran
commented on 30 Mar 2022, 12:59 PM

Thank you Dinko.  I took a different approach to get what I needed done.  What I did, is in the KeyDown event of the control, I unbind the field, change the field value to null and then rebind the control back to the field. I know it's not pretty, but it works. Like I said, I will try the approach above, in a custom class.

 

Dess | Tech Support Engineer, Principal
Telerik team
commented on 31 Mar 2022, 01:46 PM

Hi, Mark,

I am glad that you have found a suitable solution for your case. Feel free to use this approach which suits your requirements best.

Tags
TextBox
Asked by
Mark
Top achievements
Rank 2
Bronze
Bronze
Veteran
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or