C# Winform ComboBox模糊查詢實現

enif發表於2024-08-15

        public static void KeyInMatch(this ComboBox cmb, IEnumerable<string> list) {
            var templist = list.Where(x => x.Contains(cmb.Text.Trim())).ToArray();
            cmb.Items.Clear();
            cmb.Items.AddRange(templist);
            if (cmb.Items.Count == 0) cmb.Items.Add(""); 
            //至少要有一項,以避免dropdown的時候ArgumentOutOfRangeException  InvalidArgument=“0”的值對於“index”無效。
            cmb.DroppedDown = false;
            cmb.DroppedDown = true;
            cmb.Select(cmb.Text.Length, 0);
            cmb.FindForm().Cursor= Cursor.Current;  //避免興標消失
        }

  

相關文章