Don't allow removal of certain items?

1 Answer 14 Views
AutoCompleteBox
Willy
Top achievements
Rank 2
Iron
Iron
Willy asked on 04 Apr 2022, 06:38 PM

Hello, when my application adds items to the auto complete box pragmatically I turn them red. I wish to hide the X button so the user cannot remove them. Is this possible? I noticed there is a ShowRemoveButton property on the control but that seems like it is for all tokens?  Here is some of my code. Thank you for the assistance. 

Private Sub AddToken(ByVal ac As RadAutoCompleteBox, ByVal email As String)

        Dim result As EmailTokenObject = AllEmails.Find(Function(o) o.Email.ToLower = email.ToLower)
        If result IsNot Nothing Then
            Dim flag = False 'search for pre-existing items to make sure we don't add them double 

            For Each item As RadTokenizedTextItem In ac.Items
                If item.Value.ToString.ToLower = email.ToLower Then
                    flag = True
                End If
                Exit For
            Next
            If flag = False Then
                'this has to be called BEFORE we add the item to the textbox 
                PrePopulatedEmails.Add(result.Email) 'add to the prepopulated emails so we can color it in red and remove the ability to remove the item 
                'add the item 
                ac.AppendText(result.Name & ";")
            End If
        End If
    End Sub

    Private Sub acWatchlist_TokenValidating(sender As Object, e As TokenValidatingEventArgs) Handles acWatchlist.TokenValidating
        If acWatchlist.Text.Contains(e.Text) Then
            e.IsValidToken = False
        End If
    End Sub

    Private Sub acWatchlist_TextBlockFormatting(sender As Object, e As TextBlockFormattingEventArgs) Handles acWatchlist.TextBlockFormatting
        Dim token As TokenizedTextBlockElement = TryCast(e.TextBlock, TokenizedTextBlockElement)
        If token IsNot Nothing AndAlso PrePopulatedEmails.Contains(token.Item.Value, StringComparer.OrdinalIgnoreCase) Then
            token.GradientStyle = GradientStyles.Solid
            token.BackColor = Color.Red
        End If
    End Sub
Willy
Top achievements
Rank 2
Iron
Iron
commented on 04 Apr 2022, 06:47 PM

I was able to resolve part of this issue with: token.RemoveButton.Visibility = ElementVisibility.Hidden. However the user can still backspace the item, click and press delete, and right click context menu delete the item. How can I stop this is there an easy way?

1 Answer, 1 is accepted

Sort by
1
Accepted
Dinko | Tech Support Engineer
Telerik team
answered on 05 Apr 2022, 11:54 AM

Hello Bill,

Thank you for the provided code snippet.

You are right that there is not an easy way to fully prevent the removal of items. Removing the X close button is a good way to prevent the user from removing a token. Now for the Backspace and Delete keys. I have searched for a suitable solution and what I can suggest here is to handle the TextChanging event of the control. Keep in mind that this event will be called for each keystroke. When a token is removed the NewValue property from the event arguments will be an empty string, while the OldValue property will contain the removed token. Using this you can check if the removed token can be removed. Basically, you could create a collection of strings that contains items that can't be removed. Then you can Cancel the event.

private void RadAutoCompleteBox1_TextChanging(object sender, TextChangingEventArgs e)
{
    var newValue = e.NewValue;
    var oldValue = e.OldValue;
    var currentItems = this.radAutoCompleteBox1.Text.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
    var itemsThatShouldNotBeRemoved = GetItemsThatShouldNotBeRemoved();
    if (e.NewValue == "" && itemsThatShouldNotBeRemoved.Contains(e.OldValue.Replace(";", "")))
    {
        e.Cancel = true;
    }        
}

You can modify the If clause so that it covers all your scenarios. I hope this approach will work for you.

Regards,
Dinko
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Willy
Top achievements
Rank 2
Iron
Iron
commented on 05 Apr 2022, 02:02 PM | edited

Thank you. I was looking into this last night but I was subscribing to the item collection modified event. I did notice a bug in that event that if you hit the backspace on an item it does not show up in the olditems but if you click the X to remove it does. I will try what you suggest right now and report back.

 Private Sub Items_CollectionChanged(ByVal sender As Object, ByVal e As NotifyCollectionChangedEventArgs)
        If e.NewItems IsNot Nothing Then
            For Each itm In e.NewItems
                Debug.Print(e.Action.ToString & " " & itm.ToString)
            Next
        End If

        If e.OldItems IsNot Nothing Then
                For Each itm In e.OldItems
                Debug.Print(e.Action.ToString & " " & itm.ToString)
            Next
            End If
    End Sub
Willy
Top achievements
Rank 2
Iron
Iron
commented on 05 Apr 2022, 02:37 PM

The solution provided worked! Thank you very much. 
Tags
AutoCompleteBox
Asked by
Willy
Top achievements
Rank 2
Iron
Iron
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or