|
| 1 | +--- |
| 2 | +title: Limiting Characters in Entry and ComboBox Controls in UI for .NET MAUI |
| 3 | +description: Learn how to limit the number of characters entered in Telerik MAUI Entry and RadComboBox controls while typing. |
| 4 | +type: how-to |
| 5 | +page_title: Restricting Input Length in Entry and RadComboBox for UI for .NET MAUI |
| 6 | +meta_title: Restricting Input Length in Entry and RadComboBox for UI for .NET MAUI |
| 7 | +slug: restricting-input-length-entry-radcombobox-maui |
| 8 | +tags: radentry, radcombobox, input-validation, ui for .net maui, entry, combobox, maxlength, textchanged |
| 9 | +res_type: kb |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +| Version | Product | Author | |
| 15 | +| --- | --- | ---- | |
| 16 | +| 12.0.0 | Telerik UI for .NET MAUI Entry | [Dobrinka Yordanova](https://www.telerik.com/blogs/author/dobrinka-yordanova) | |
| 17 | +| 12.0.0 | Telerik UI for .NET MAUI ComboBox| [Dobrinka Yordanova](https://www.telerik.com/blogs/author/dobrinka-yordanova) | |
| 18 | + |
| 19 | +## Description |
| 20 | + |
| 21 | +I want to restrict the number of characters typed in an Entry or ComboBox control in UI for .NET MAUI applications. |
| 22 | + |
| 23 | +This knowledge base article also answers the following questions: |
| 24 | +- How to set a maximum character limit in Telerik MAUI Entry? |
| 25 | +- How to access ComboBox's input and set character restrictions? |
| 26 | +- How can I handle text changes in ComboBox or Entry? |
| 27 | + |
| 28 | +## Solution for MAUI Entry |
| 29 | + |
| 30 | +The [Telerik MAUI Entry](https://www.telerik.com/maui-ui/documentation/controls/entry/overview) control provides a `MaxLength` property to define the maximum number of characters allowed. It also supports a `TextChanged` event to track text changes. |
| 31 | + |
| 32 | +```xml |
| 33 | +<telerik:RadEntry MaxLength="10" TextChanged="OnTextChanged" /> |
| 34 | +``` |
| 35 | + |
| 36 | +## Solution for MAUI ComboBox |
| 37 | + |
| 38 | +The [ComboBox](https://www.telerik.com/maui-ui/documentation/controls/combobox/overview) uses the `RadTextInput` control for text input when in editable mode. You can access `RadTextInput` through the `Loaded` event of `RadComboBox` and set the `MaxLength` property. |
| 39 | + |
| 40 | +```xml |
| 41 | +<VerticalStackLayout> |
| 42 | + <telerik:RadComboBox ItemsSource="{Binding Locations}" |
| 43 | + DisplayMemberPath="City" |
| 44 | + IsEditable="True" |
| 45 | + x:Name="combo" |
| 46 | + Loaded="RadComboBox_Loaded" /> |
| 47 | +</VerticalStackLayout> |
| 48 | +``` |
| 49 | + |
| 50 | +In the code-behind, access `RadTextInput` and define the character limit: |
| 51 | + |
| 52 | +```csharp |
| 53 | +private void RadComboBox_Loaded(object sender, EventArgs e) |
| 54 | +{ |
| 55 | + var textInput = ChildOfType<RadTextInput>(this.combo); |
| 56 | + textInput.MaxLength = 4; |
| 57 | + textInput.TextChanged += TextInput_TextChanged; |
| 58 | +} |
| 59 | + |
| 60 | +private void TextInput_TextChanged(object? sender, TextChangedEventArgs e) |
| 61 | +{ |
| 62 | + // Handle text changes if needed. |
| 63 | +} |
| 64 | + |
| 65 | +internal static T ChildOfType<T>(View visualElement) where T : View |
| 66 | +{ |
| 67 | + if (visualElement == null) |
| 68 | + { |
| 69 | + return null; |
| 70 | + } |
| 71 | + foreach (var item in VisualTreeElementExtensions.GetVisualTreeDescendants(visualElement)) |
| 72 | + { |
| 73 | + if (item is T targetElement) |
| 74 | + { |
| 75 | + return targetElement; |
| 76 | + } |
| 77 | + } |
| 78 | + return null; |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | + |
| 83 | +## See Also |
| 84 | + |
| 85 | +- [Entry Overview](https://www.telerik.com/maui-ui/documentation/controls/entry/overview) |
| 86 | +- [ComboBox Overview](https://www.telerik.com/maui-ui/documentation/controls/combobox/overview) |
| 87 | +- [Text Input Documentation](https://www.telerik.com/maui-ui/documentation/controls/entry/text-input) |
| 88 | +- [Entry Events Documentation](https://www.telerik.com/maui-ui/documentation/controls/entry/events) |
0 commit comments