Completed
Last Updated: 15 Jan 2013 08:20 by ADMIN
ADMIN
Anton
Created on: 15 Jan 2013 08:20
Category: Editors
Type: Bug Report
0
FIX. RadTextBox - after canceling the textChanging event the cursor back at the start of the text.
IF you  canceling the textChanging event of the RadTextBox (e.Cancel = true)  the cursor back at the start of the text.

Work around:

1. Create custom text box element that inherited the RadTextBoxElement. For example:
public class MyTextBoxElement : RadTextBoxElement
{
    private bool isValueChanging = false;
    private int selectionStart = -1;
 
    public MyTextBoxElement()
    {
        this.TextBoxItem.HostedControl.TextChanged += new EventHandler(HostedControl_TextChanged);
    }
 
    protected override void OnTextChanging(Telerik.WinControls.TextChangingEventArgs e)
    {
        base.OnTextChanging(e);
 
        Regex regEx = new Regex(@"[\s]{1,}");
        if (regEx.IsMatch(e.NewValue))
        {
            isValueChanging = true;
            this.selectionStart = base.TextBoxItem.SelectionStart - 1;
        }
         
    }
 
    private void HostedControl_TextChanged(object sender, EventArgs e)
    {
        if (this.isValueChanging && selectionStart >= 0)
        {
            this.isValueChanging = false;
            this.TextBoxItem.SelectionStart = this.selectionStart;
        }
    }
 
    protected override Type ThemeEffectiveType
    {
        get
        {
            return typeof(RadTextBoxElement);
        }
    }
}

2. Create custom Text box that inherited the RadTextBox: For Example:
public class MyTextBox : RadTextBox
{
    private static readonly FieldInfo TextBoxElementFieldInfo = typeof(RadTextBox).GetField("textBoxElement", BindingFlags.NonPublic | BindingFlags.Instance);
 
    public MyTextBox()
    {
        this.ThemeClassName = typeof(RadTextBox).FullName;
    }
 
    protected RadTextBoxElement TextBox
    {
        get { return (RadTextBoxElement)TextBoxElementFieldInfo.GetValue(this); }
        set { TextBoxElementFieldInfo.SetValue(this, value); }
    }
 
    protected override void InitializeTextElement()
    {
        this.TextBox = new MyTextBoxElement();
        this.TextBox.StretchVertically = true;
        this.TextBox.ShowBorder = true;
    }
 
}

3. Use your custom Text box instead the default RadTextBox. 

4. Subscribe to text Changing event of the custom text box and add the following code snippet:

    Regex regEx = new Regex(@"[\s]{1,}");
    if (regEx.IsMatch(e.NewValue))
    {
        e.Cancel = true;
    }
0 comments