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;
}