I wrote a custom Control
(an auto-complete TextBox
(below)) in which a ContextMenuStrip
is programmatically added to the form.
My problem is that when the control generates a list longer than the height of it's parent container (Panel
, GroupBox
, etc) the bottom section of ContextMenuStrip
is hidden.
I have tried calling .BringToFront()
but can't find any way to overcome this behaviour.
Any help would be greatly appriciated, also feel free to steal the control :)
fig 1.
/// <summary>
/// TextBox which can auto complete words found in a table column
/// Just set DataSource and DataListField and start typing - WD
/// </summary>
public class AutoComplete : TextBox
{
public DataTable DataSource { get; set; }
public string DataListField { get; set; }
private ContextMenuStrip SuggestionList = new ContextMenuStrip();
public AutoComplete()
{
this.LostFocus += new EventHandler(AutoComplete_LostFocus);
KeyUp += new KeyEventHandler(AutoComplete_KeyUp);
SuggestionList.ItemClicked += new ToolStripItemClickedEventHandler(SuggestionList_ItemClicked);
}
void AutoComplete_LostFocus(object sender, EventArgs e)
{
if (!SuggestionList.Focused)
{
SuggestionList.Visible = false;
}
}
void SuggestionList_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
this.Text = e.ClickedItem.Text;
SuggestionList.Visible = false;
this.Focus();
SuggestionList.Visible = false;
}
void AutoComplete_KeyUp(object sender, KeyEventArgs e)
{
if (null != DataSource && DataSource.Rows.Count > 0 && null != DataListField)
{
if (e.KeyCode != Keys.Enter)
{
if (SuggestionList.Items.Count > 0 && e.KeyCode == Keys.Down)
{
SuggestionList.Focus();
SuggestionList.Items[0].Select();
SuggestionList.BringToFront();
}
else if (this.Text.Length > 0)
{
SuggestionList.Items.Clear();
DataRow[] drSuggestionList = DataSource.Select("[" + DataListField + "] LIKE '" + this.Text + "%'");
foreach (DataRow dr in drSuggestionList)
{
SuggestionList.Items.Add(dr[DataListField].ToString());
}
SuggestionList.TopLevel = false;
SuggestionList.Visible = true;
SuggestionList.Top = (this.Top + this.Height);
SuggestionList.Left = this.Left;
this.Parent.Controls.Add(SuggestionList);
SuggestionList.BringToFront();
}
}
}
}
}