Restricting User Typing in RadAutoCompleteBox
Environment
Product Version | Product | Author |
---|---|---|
2025.2.520 | RadAutoCompleteBox for WinForms | Dinko Krastev |
Description
In the following scenario, we will demonstrate how to prevent the end user from typing in the control. The user should interact with the control only by clicking an external button (e.g., Attach), which opens a file dialog for selecting files to attach.
Solution
To disable user input in the RadAutoCompleteBox, you can override its KeyDown
method by customizing the RadAutoCompleteBoxElement
. Follow these steps:
- Create a custom class that inherits from
RadAutoCompleteBox
. - Override the
CreateTextBoxElement
method to return the customRadAutoCompleteBoxElement
. - Inside the custom
RadAutoCompleteBoxElement
, override theOnKeyDown
method to suppress key presses.
Here is the code implementation:
public class MyAutoCompleteBoxControl : RadAutoCompleteBox
{
public override string ThemeClassName
{
get
{
return typeof(RadAutoCompleteBox).FullName;
}
}
protected override RadTextBoxControlElement CreateTextBoxElement()
{
return new MyAutoCompleteBoxElement();
}
}
public class MyAutoCompleteBoxElement : RadAutoCompleteBoxElement
{
protected override Type ThemeEffectiveType
{
get
{
return typeof(RadAutoCompleteBoxElement);
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
e.SuppressKeyPress = true;
e.Handled = true;
}
}
Replace the standard RadAutoCompleteBox
with the custom MyAutoCompleteBoxControl
in your application. This will prevent users from typing in the control.