Home > Blockchain >  Make TextBox Suggestion Filter Not Case Sensitive
Make TextBox Suggestion Filter Not Case Sensitive

Time:03-04

Good day everyone, I used this code from this post, WPF Suggestion TextBox, to suggest the text on a textbox.

Works like I wanted but there is a problem, it is case sensitive, I searched for a solution but found nothing other than this line of code "StringComparison.InvariantCultureIgnoreCase" but I don't know where to put this.

Code I used from the post (its exactly the same, just different suggestion values):

using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace Solutions
{
    public partial class MainWindow : Window
    {
        private static readonly string[] SuggestionValues = {
            "England",
            "USA",
            "France",
            "Estonia"
        };

        public MainWindow()
        {
            InitializeComponent();
            SuggestionBox.TextChanged  = SuggestionBoxOnTextChanged;
        }

        private string _currentInput = "";
        private string _currentSuggestion = "";
        private string _currentText = "";

        private int _selectionStart;
        private int _selectionLength;
        private void SuggestionBoxOnTextChanged(object sender, TextChangedEventArgs e)
        {
            var input = SuggestionBox.Text;
            if (input.Length > _currentInput.Length && input != _currentSuggestion)
            {
                _currentSuggestion = SuggestionValues.FirstOrDefault(x => x.StartsWith(input));
                if (_currentSuggestion != null)
                {
                    _currentText = _currentSuggestion;
                    _selectionStart = input.Length;
                    _selectionLength = _currentSuggestion.Length - input.Length;

                    SuggestionBox.Text = _currentText;
                    SuggestionBox.Select(_selectionStart, _selectionLength);
                }
            }
            _currentInput = input;
        }
    }
}

Appreciate any help,

thanks.

CodePudding user response:

        x.StartsWith(input, StringComparison.OrdinalIgnoreCase);

You can ignore case sensitive using this, you can improve other part like _currentSuggestion

        !input.Equals(_currentSuggestion, StringComparison.OrdinalIgnoreCase);

etc...

  • Related