Skip to content

Instantly share code, notes, and snippets.

@devmnj
Created October 19, 2022 03:36
Show Gist options
  • Save devmnj/3df61add9bf6a9f2af01416c39884c19 to your computer and use it in GitHub Desktop.
Save devmnj/3df61add9bf6a9f2af01416c39884c19 to your computer and use it in GitHub Desktop.
C# custom search box control library with textbox and list boxt control - Win Form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
// add user control with a text and list control.
namespace WindowsFormsControlLibrary
{
public partial class SText : UserControl
{
private string Lwidth = "Auto";
private bool changed = false;
private bool defAll = false;
private List<string> sourceList = new List<string>() { "Apple", "Banana", "Orange", "Anar", "Dragon" };
private bool tabKeyEnabled = false;
public SText()
{
InitializeComponent();
}
[Category("Common")]
public string BoxText
{
get => txt_filter_box.Text; set { txt_filter_box.Text = value; Invalidate(); }
}
public Color BoxBackColor { get => txt_filter_box.BackColor; set { txt_filter_box.BackColor = value; Invalidate(); } }
public Color ListBackColor { get => listBox_filter.BackColor; set { listBox_filter.BackColor = value; Invalidate(); } }
public bool TabkeyEnabled { get => tabKeyEnabled; set { tabKeyEnabled = value; Invalidate(); } }
public int SelectedIndex { get; set; }
public int SelectedItem { get; set; }
[Category("Common")]
public string ListWidth { get => Lwidth; set { Lwidth = value; Invalidate(); } }
[Category("Data")]
public List<string> Source
{
get { return sourceList; }
set { sourceList = value; }
}
public bool ShowAll { get => defAll; set { defAll = value; Invalidate(); } }
private void SetBoxHeight(bool list = false)
{
try
{
if (list)
{
int.TryParse(Lwidth, out int ht);
if (ht != 0)
{
listBox_filter.Height = ht;
}
this.Height = txt_filter_box.Height + this.Padding.Top + this.Padding.Bottom + listBox_filter.Height;
this.Width = txt_filter_box.Width;
}
else
{
this.Height = txt_filter_box.Height + this.Padding.Top + this.Padding.Bottom;
this.Width = txt_filter_box.Width;
}
}
catch (Exception)
{
throw;
}
}
private void SText_Load(object sender, EventArgs e)
{
try
{
SetBoxHeight();
}
catch (Exception)
{
throw;
}
}
private void txt_filter_box_TextChanged(object sender, EventArgs e)
{
try
{
SetBoxHeight();
if (txt_filter_box.Text.Trim() != "")
changed = true;
listBox_filter.DataSource = null;
if (txt_filter_box.Text.Trim() == "")
{
if (ShowAll && changed)
if (sourceList.Count != 0)
{
listBox_filter.DataSource = sourceList;
SetBoxHeight(true);
}
}
else
{
listBox_filter.DataSource = null;
var ds = sourceList.Where(x => x.ToLower().Contains(txt_filter_box.Text.ToLower())).ToList();
if (ds.Count != 0)
{
listBox_filter.DataSource = ds;
SetBoxHeight(true);
}
}
}
catch (Exception)
{
throw;
}
}
private void BoxKeyPress(object sender, KeyPressEventArgs e)
{
try
{
if (listBox_filter.Items.Count > 0 && e.KeyChar.Equals((char)Keys.Enter))
{
txt_filter_box.Text = listBox_filter.Text;
SetBoxHeight();
}
this.OnKeyPress(e);
}
catch (Exception)
{
throw;
}
}
private void BoxKeyDown(object sender, KeyEventArgs e)
{
try
{
if (e.KeyCode == Keys.Down)
{
if (listBox_filter.Items.Count != 0)
listBox_filter.Focus();
}
if (e.KeyCode == Keys.Enter && TabkeyEnabled)
{
SendKeys.SendWait("{tab}");
e.Handled = true;
}
this.OnKeyDown(e);
}
catch (Exception)
{
throw;
}
}
private void ListKeyPress(object sender, KeyPressEventArgs e)
{
try
{
if (e.KeyChar.Equals((char)Keys.Enter))
{
txt_filter_box.Text = listBox_filter.Text.Trim();
listBox_filter.DataSource = null;
SetBoxHeight();
txt_filter_box.Focus();
}
}
catch (Exception)
{
throw;
}
}
private void ListKeyDown(object sender, KeyEventArgs e)
{
try
{
if (e.KeyCode == Keys.Left)
{
txt_filter_box.Focus();
}
}
catch (Exception)
{
throw;
}
}
private void BoxEnter(object sender, EventArgs e)
{
try
{
if (txt_filter_box.Text.Trim().Length == 0)
{
if (ShowAll)
if (sourceList.Count != 0)
{
listBox_filter.DataSource = sourceList;
SetBoxHeight(true);
}
}
txt_filter_box.SelectAll();
Invalidate();
}
catch (Exception)
{
throw;
}
}
private void SText_Leave(object sender, EventArgs e)
{
SetBoxHeight();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment