|
| 1 | +--- |
| 2 | +title: Restricting User Typing in RadAutoCompleteBox |
| 3 | +description: Learn how to prevent users from typing in the RadAutoCompleteBox in UI for WinForms. |
| 4 | +type: how-to |
| 5 | +page_title: Prevent User Input in RadAutoCompleteBox in UI for WinForms |
| 6 | +meta_title: Prevent User Input in RadAutoCompleteBox in UI for WinForms |
| 7 | +slug: autocompletebox-restrict-user-input |
| 8 | +tags: editors, autocompletebox,keydown, customization,respect-user-input |
| 9 | +res_type: kb |
| 10 | +ticketid: 1691827 |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | + |
| 15 | +|Product Version|Product|Author| |
| 16 | +|----|----|----| |
| 17 | +|2025.2.520|RadGridView for WinForms|[Dinko Krastev](https://www.telerik.com/blogs/author/dinko-krastev)| |
| 18 | + |
| 19 | +## Description |
| 20 | + |
| 21 | +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. |
| 22 | + |
| 23 | +## Solution |
| 24 | + |
| 25 | +To disable user input in the RadAutoCompleteBox, you can override its `KeyDown` method by customizing the `RadAutoCompleteBoxElement`. Follow these steps: |
| 26 | + |
| 27 | +1. Create a custom class that inherits from `RadAutoCompleteBox`. |
| 28 | +2. Override the `CreateTextBoxElement` method to return the custom `RadAutoCompleteBoxElement`. |
| 29 | +3. Inside the custom `RadAutoCompleteBoxElement`, override the `OnKeyDown` method to suppress key presses. |
| 30 | + |
| 31 | +Here is the code implementation: |
| 32 | + |
| 33 | +````C# |
| 34 | + |
| 35 | +public class MyAutoCompleteBoxControl : RadAutoCompleteBox |
| 36 | +{ |
| 37 | + public override string ThemeClassName |
| 38 | + { |
| 39 | + get |
| 40 | + { |
| 41 | + return typeof(RadAutoCompleteBox).FullName; |
| 42 | + } |
| 43 | + } |
| 44 | + protected override RadTextBoxControlElement CreateTextBoxElement() |
| 45 | + { |
| 46 | + return new MyAutoCompleteBoxElement(); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +public class MyAutoCompleteBoxElement : RadAutoCompleteBoxElement |
| 51 | +{ |
| 52 | + protected override Type ThemeEffectiveType |
| 53 | + { |
| 54 | + get |
| 55 | + { |
| 56 | + return typeof(RadAutoCompleteBoxElement); |
| 57 | + } |
| 58 | + } |
| 59 | + protected override void OnKeyDown(KeyEventArgs e) |
| 60 | + { |
| 61 | + e.SuppressKeyPress = true; |
| 62 | + e.Handled = true; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +```` |
| 67 | + |
| 68 | +Replace the standard `RadAutoCompleteBox` with the custom `MyAutoCompleteBoxControl` in your application. This will prevent users from typing in the control. |
| 69 | + |
| 70 | +## See Also |
| 71 | + |
| 72 | +- [RadAutoCompleteBox Documentation](https://docs.telerik.com/devtools/winforms/controls/editors/autocompletebox/autocompletebox) |
0 commit comments