This is a migrated thread and some comments may be shown as answers.

How to set the MaxLength property of the input area of the combobox?

15 Answers 471 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
zaphod94
Top achievements
Rank 1
zaphod94 asked on 08 Jun 2010, 07:22 PM
I have followed this example

http://www.telerik.com/help/silverlight/radcombobox-howto-set-max-length-input-area.html

However, comboBox.FindName("PART_EditableTextBox") always returns null

Any other ideas?

comboBox.GetChildTemplate("PART_EditableTextBox") works from the debug window but get a compile time warned "GetChildTemplate is not accessible because of its access levels

TIA
zaphod



15 Answers, 1 is accepted

Sort by
0
Ross Wozniak
Top achievements
Rank 1
answered on 11 Jun 2010, 02:45 PM
Hi Tia,

I was able to reproduce the problem you are experiencing, and have asked our dev team to look at it, but in the meantime I tried replacing the 'FindName' line with the following and it worked for me:

var

 

 

textBox = comboBox.FindChildByType<TextBox>();

 


Ross
0
Hristo
Telerik team
answered on 14 Jun 2010, 07:42 AM
Hi zaphod,

Or you could change the code in the OnMaxLengthChanged method to this:
private static void OnMaxLengthChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
    var comboBox = obj as Telerik.Windows.Controls.RadComboBox;
    if (comboBox == null)
        return;
  
    comboBox.Dispatcher.BeginInvoke(
        delegate()
        {
            var childrenCount = VisualTreeHelper.GetChildrenCount(comboBox);
            if (childrenCount > 0)
            {
  
                var rootElement = VisualTreeHelper.GetChild(comboBox, 0) as FrameworkElement;
                TextBox textBox = (TextBox)rootElement.FindName("PART_EditableTextBox");
                if (textBox != null)
                    textBox.SetValue(TextBox.MaxLengthProperty, e.NewValue);
            }
        });
}

We will update the help article.

Best wishes,
Hristo
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
IDispose
Top achievements
Rank 1
answered on 14 Jun 2010, 02:34 PM
Hristo
Thank you for the solution. I have confirmed that it works with the version of Telerik controls we are currently using.

Best
zaphod
0
p k
Top achievements
Rank 1
answered on 08 Feb 2011, 11:16 AM
Hi,
I refered the example from http://www.telerik.com/help/silverlight/radcombobox-howto-set-max-length-input-area.html.
After adding the class and refreing the same in xaml i am getting build error.

Error 15 The property 'MaxLength' does not exist on the type 'RadComboBox' in the XML namespace . 

Please help.

Thanks
Purushottam
0
Yana
Telerik team
answered on 11 Feb 2011, 09:19 AM
Hello Purushottam,

Could you please send us your code so we can examine it ? Thanks in advance

Greetings,
Yana
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Matthias Bibo
Top achievements
Rank 1
answered on 17 Sep 2012, 03:43 PM

Hello,

the HowTo: Set MaxLength Property of the Input Area does not work for us.

Can you give us some hint? The Howto crashes, because the combobox has no children.Then I added the addition with the childrencount, but then the code gets never executed.

<bdlControls:BDLComboBox x:Name="txt_targetHpsl" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" TextSearchMode="StartsWith" CanAutocompleteSelectItems="False" IsEditable="True" IsTextSearchEnabled="False" ItemsSource="{Binding ContextSelektionsErgebnisse, Mode=TwoWay}" Text="{Binding CurrentSelectionWfStep.TargetHpslDescription, Mode=TwoWay}" HorizontalAlignment="Stretch" VerticalAlignment="Center" MinWidth="120" Foreground="#396C9C" bdlControls:BDLEditableComboBox.MaxLength="20"/>

public class BDLComboBox : RadComboBox
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="BDLComboBox" /> class.
        /// </summary>
        public BDLComboBox()
        {
            // StyleManager.SetTheme(this, new Bdl.DialogCrm.Silverlight.Themes.DialogCrmDefaultTheme());     
        }        
    }

public class BDLEditableComboBox
    {
        /// <summary>
        /// 
        /// </summary>
        public static readonly DependencyProperty MaxLengthProperty =
            DependencyProperty.RegisterAttached("MaxLength", typeof(int), typeof(BDLEditableComboBox), new PropertyMetadata(OnMaxLengthChanged));
  
        /// <summary>
        /// Gets the length of the max.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns></returns>
        public static int GetMaxLength(DependencyObject obj)
        {
            return (int)obj.GetValue(MaxLengthProperty);
        }
  
        /// <summary>
        /// Sets the length of the max.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <param name="value">The value.</param>
        public static void SetMaxLength(DependencyObject obj, int value)
        {
            obj.SetValue(MaxLengthProperty, value);
        }
  
        /// <summary>
        /// Called when [max length changed].
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        private static void OnMaxLengthChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            var comboBox = obj as RadComboBox; 
            if (comboBox == null)
                return;
  
            comboBox.Dispatcher.BeginInvoke(
                delegate()
                {
                    var childrenCount = VisualTreeHelper.GetChildrenCount(comboBox);
                    if (childrenCount > 0)
                    {
                        var rootElement = VisualTreeHelper.GetChild(comboBox, 0) as FrameworkElement;
  
                        if (rootElement != null)
                        {
                            TextBox textBox = (TextBox)rootElement.FindName("PART_EditableTextBox");
                            if (textBox != null)
                            {
                                textBox.SetValue(TextBox.MaxLengthProperty, e.NewValue);
                            }
                        }
                    }
                });
        }
    }

-> I´m not the first person who needs this feature on a RadCombobox and has problems with it.

The RadCombobox has an "IsEditable"  property , but without an "MaxLength" property it is useless.

What´s about integrating the "MaxLength" property in the RadCombobox in a future version to avoid such problems and support time for us and you?

Regards,

Matthias

0
Yana
Telerik team
answered on 19 Sep 2012, 10:02 AM
Hello Matthias,

I was not able to reproduce the issue, I guess that you've made some customizations to the ComboBox - the code in OnMaxLengthChanged event should work without a problem with the default template of RadComboBox. Could you please open a support ticket and send us a sample project showing the issue?

Thanks in advance

All the best,
Yana
the Telerik team

Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

0
Matthias Bibo
Top achievements
Rank 1
answered on 20 Sep 2012, 03:11 PM

Hello,

I found the issue, it comes, when the combox is not visible initially (e.g. by an converter,...), then there is no child textbox existing in the VisualTree and the code fails.

This can be solved, by setting the dependency property in the loaded event, but this is only a workaround.

I implemented my own combobox class, derived from telerik, it is more elegant:

/// <summary>
   /// Das BDLComboBox
   /// </summary>
   public class BDLComboBox : RadComboBox
   {
       private int maxLength = 0;
       private bool isMaxLengthSet = false;
 
       /// <summary>
       /// Initializes a new instance of the <see cref="BDLComboBox" /> class.
       /// </summary>
       public BDLComboBox()
       {
              
           this.Loaded += new System.Windows.RoutedEventHandler(this.BDLComboBox_Loaded);
       }
 
       /// <summary>
       /// Gets or sets the length of the max.
       /// </summary>
       /// <value>
       /// The length of the max.
       /// </value>
       public int MaxLength
       {
           get
           {
               return this.maxLength;
           }
 
           set
           {
               if (this.maxLength != value)
               {
                   this.maxLength = value;
                   this.SetMaxLength();
               }
           }
       }
 
       /// <summary>
       /// Handles the Loaded event of the BDLComboBox control.
       /// </summary>
       /// <param name="sender">The source of the event.</param>
       /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
       private void BDLComboBox_Loaded(object sender, System.Windows.RoutedEventArgs e)
       {
           this.SetMaxLength();
       }
 
       /// <summary>
       /// Sets the length of the max.
       /// </summary>
       private void SetMaxLength()
       {
           if (!this.isMaxLengthSet && this.MaxLength > 0)
           {
               this.Dispatcher.BeginInvoke(
                   delegate()
                   {
                       var childrenCount = VisualTreeHelper.GetChildrenCount(this);
                       if (childrenCount > 0)
                       {
                           var rootElement = VisualTreeHelper.GetChild(this, 0) as FrameworkElement;
 
                           if (rootElement != null)
                           {
                               TextBox textBox = (TextBox)rootElement.FindName("PART_EditableTextBox");
                               if (textBox != null)
                               {
                                   textBox.SetValue(TextBox.MaxLengthProperty, this.MaxLength);
                                   this.isMaxLengthSet = true;
                               }
                           }
                       }
                   });
           }
       }
   }

0
Brian Sayatovic
Top achievements
Rank 1
answered on 09 Oct 2012, 01:48 PM
The code at that URL, http://www.telerik.com/help/silverlight/radcombobox-howto-set-max-length-input-area.html, I think has been updated since this post was last touched.  I'm using the code as it stands and am having problems.

System.ArgumentOutOfRangeException was unhandled by user code
  Message=Specified argument was out of the range of valid values.
Parameter name: childIndex
  StackTrace:
       at System.Windows.Media.VisualTreeHelper.GetChild(DependencyObject reference, Int32 childIndex)
       at MyExtensions.TelerikRadComboBox.Editable.<>c__DisplayClass1.<OnMaxLengthChanged>b__0()
  InnerException:

Catching it in the debugger, this is the line that is failing:

var rootElement = VisualTreeHelper.GetChild(comboBox, 0) as FrameworkElement;

The comboBox variable is not null.  Its a valid RadComboBox.  But GetChild doesn't like the index 0 being passed in.  Using the immediate window, I found that comboBox has no children (VisualTreeHelper.GetChildrenCount(comboBox)).

What gives?
0
Matthias Bibo
Top achievements
Rank 1
answered on 09 Oct 2012, 02:09 PM
Hello,
read my post carefully (including sourcecode) and you´ll propably find the answer, I had the same proplem.
I The code in the How-To is not complete.... You have to check if it has children like following...

var childrenCount = VisualTreeHelper.GetChildrenCount(this);
 
                       if (childrenCount > 0)
                       {
                             .....


But than it did not work for my either, because when the combobox is not visible initally it has not children. I called it maualyy after loading and then it worked.





0
Brian Sayatovic
Top achievements
Rank 1
answered on 09 Oct 2012, 03:39 PM
OK, I hadn't seen the whole thread.  Now I do.  That's a shame there isn't a more elegant, self-contained solution from Telerik after all of these years.
0
Matthias Bibo
Top achievements
Rank 1
answered on 10 Oct 2012, 03:48 PM

I fully agree, because an editable object needs a limitation of the textinput, so a maxlength property is not just a nice to have.

@Telerik:

-Please add the MaxLength property as feature request

-Please update the how-to, so that it is up to date

0
Vladi
Telerik team
answered on 15 Oct 2012, 12:25 PM
Hi,

Thank you for your feedback.

We believe that MaxLength property should not be a built-in feature but a feature that needs to be supported in the control which could be easily implemented if needed. With that said we do not have any future plans on implementing it in as a built-in feature in the control.

As for the "How to" help article we will see if an update is required and we will handle it as soon as possible.

Greetings,
Vladi
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Brian Sayatovic
Top achievements
Rank 1
answered on 15 Oct 2012, 01:35 PM
Vladi,

That is very disappointing.  Most controls which allow for any sort of text input allow for MaxLength.  I know RadComboBox is a composite control, so that is more difficult to expose every property for every control that comprises it.  Hence, the idea of the solution in the aforementioned article.

However, that article's solution doesn't work.  The solution doesn't need a tweak or cleanup, it needs to be reconsidered from scratch.  It does not work for a control that is not yet visible.  The MaxLength ends up never being set because you can't get to the template parts before the control is rendered.

In my specific case, the RadComboBox is on a non-focused tab in a RadTabControl.  I'll have to string together an event that listens to the control becoming visible before trying to get the template part so I can set the MaxLength.  Rube Goldberg would be delighted.

I hope you and your Telerik team reconsider simply freshening the article and consider the sizable challenge your customers face with this.
0
Vladi
Telerik team
answered on 19 Oct 2012, 08:29 AM
Hi Brian,

In this scenario if you want to set the MaxLength to a specific RadComboBox you could set the Style of the ComboBox in the Resources of the control, the next code snippet shows how to do that:
<telerik:RadComboBox IsEditable="True">
    <telerik:RadComboBox.Resources>
        <Style TargetType="TextBox">
            <Setter Property="MaxLength" Value="5" />
        </Style>
    </telerik:RadComboBox.Resources>
</telerik:RadComboBox>

If you don't want to declare the style in all RadComboBox controls in your project you could use Implicit Styles and edit the theme you are using by setting the MaxLength property of the TextBox with x:Name="PART_EditableTextBox".

We will update the "How to" help article to include the described approaches, thank you for your feedback.

Greetings,
Vladi
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
ComboBox
Asked by
zaphod94
Top achievements
Rank 1
Answers by
Ross Wozniak
Top achievements
Rank 1
Hristo
Telerik team
IDispose
Top achievements
Rank 1
p k
Top achievements
Rank 1
Yana
Telerik team
Matthias Bibo
Top achievements
Rank 1
Brian Sayatovic
Top achievements
Rank 1
Vladi
Telerik team
Share this question
or