You can set the AutoClose property to be FALSE at the very first of the TextBox.TextChanged event handler function, use a boolean flag to judge wether there's any item found, if no item found, reset this property to TRUE and hide the ToolStripDropDown control, see my sample below for the details and the solution for your #2 question
Code Block
partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dt = new DataTable();
dt.Columns.Add("c1");
dt.Columns.Add("c2");
dt.Columns.Add("c3");
dt.Rows.Add("ava","fbdfb","dvsd");
dt.Rows.Add("acsa","dsd","dcvsdv");
dt.Rows.Add("avsdfger","dfsd","dvsd");
dt.Rows.Add("bsdfsd","bvsd","ver");
dt.Rows.Add("befesd","vev","bgf");
dt.Rows.Add("cfbdf","nhgn","asda");
this.canDropDown = true;
this.dataGridView1 = new DataGridView();
this.Controls.Add(this.dataGridView1);
this.dataGridView1.ColumnHeadersVisible = false;
this.dataGridView1.RowHeadersVisible = false;
this.dataGridView1.AutoSize = true;
this.dataGridView1.DataSource = dt.DefaultView;
this.dataGridView1.MultiSelect = false;
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
this.dataGridView1.SelectionChanged += new EventHandler(dataGridView1_SelectionChanged);
popup = new ToolStripDropDown();
popup.AutoSize = false;
popup.Margin = Padding.Empty;
popup.Padding = Padding.Empty;
ToolStripControlHost host = new ToolStripControlHost(this.dataGridView1);
host.Margin = Padding.Empty;
host.Padding = Padding.Empty;
host.AutoSize = false;
host.Size = this.dataGridView1.Size;
popup.Size = this.dataGridView1.Size;
popup.Items.Add(host);
this.textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
this.Controls.Remove(this.dataGridView1);
}
void textBox1_TextChanged(object sender, EventArgs e)
{
popup.AutoClose = false;
bool found = false;
foreach (DataGridViewRow r in this.dataGridView1.Rows)
{
if (r.Index > -1 && r.Cells[0].Value != null)
{
if (r.Cells[0].Value.ToString().StartsWith(this.textBox1.Text))
{
this.dataGridView1.CurrentCell = r.Cells[0];
r.Selected = true;
found = true;
this.popup.Show(this, this.textBox1.Left, this.textBox1.Bottom);
canDropDown = !canDropDown;
break;
}
}
}
if (!found)
{
popup.AutoClose = true;
popup.Hide();
}
}
void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (this.dataGridView1.SelectedRows.Count > 0)
{
if (this.dataGridView1.SelectedRows[0].Cells[0].Value != null)
{
this.textBox1.Text = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
this.textBox1.SelectionStart = this.textBox1.TextLength;
this.canDropDown = true;
this.popup.Hide();
}
}
}
DataTable dt;
ToolStripDropDown popup;
private bool canDropDown;
private DataGridView dataGridView1;
private void button1_Click(object sender, EventArgs e)
{
if (canDropDown)
{
this.popup.Show(this, this.textBox1.Left, this.textBox1.Bottom);
}
else
{
this.popup.Hide();
}
canDropDown = !canDropDown;
}
}
You can set the AutoClose property to be FALSE at the very first of the TextBox.TextChanged event handler function, use a boolean flag to judge wether there's any item found, if no item found, reset this property to TRUE and hide the ToolStripDropDown control, see my sample below for the details and the solution for your #2 question
Code Block
partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dt = new DataTable();
dt.Columns.Add("c1");
dt.Columns.Add("c2");
dt.Columns.Add("c3");
dt.Rows.Add("ava","fbdfb","dvsd");
dt.Rows.Add("acsa","dsd","dcvsdv");
dt.Rows.Add("avsdfger","dfsd","dvsd");
dt.Rows.Add("bsdfsd","bvsd","ver");
dt.Rows.Add("befesd","vev","bgf");
dt.Rows.Add("cfbdf","nhgn","asda");
this.canDropDown = true;
this.dataGridView1 = new DataGridView();
this.Controls.Add(this.dataGridView1);
this.dataGridView1.ColumnHeadersVisible = false;
this.dataGridView1.RowHeadersVisible = false;
this.dataGridView1.AutoSize = true;
this.dataGridView1.DataSource = dt.DefaultView;
this.dataGridView1.MultiSelect = false;
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
this.dataGridView1.SelectionChanged += new EventHandler(dataGridView1_SelectionChanged);
popup = new ToolStripDropDown();
popup.AutoSize = false;
popup.Margin = Padding.Empty;
popup.Padding = Padding.Empty;
ToolStripControlHost host = new ToolStripControlHost(this.dataGridView1);
host.Margin = Padding.Empty;
host.Padding = Padding.Empty;
host.AutoSize = false;
host.Size = this.dataGridView1.Size;
popup.Size = this.dataGridView1.Size;
popup.Items.Add(host);
this.textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
this.Controls.Remove(this.dataGridView1);
}
void textBox1_TextChanged(object sender, EventArgs e)
{
popup.AutoClose = false;
bool found = false;
foreach (DataGridViewRow r in this.dataGridView1.Rows)
{
if (r.Index > -1 && r.Cells[0].Value != null)
{
if (r.Cells[0].Value.ToString().StartsWith(this.textBox1.Text))
{
this.dataGridView1.CurrentCell = r.Cells[0];
r.Selected = true;
found = true;
this.popup.Show(this, this.textBox1.Left, this.textBox1.Bottom);
canDropDown = !canDropDown;
break;
}
}
}
if (!found)
{
popup.AutoClose = true;
popup.Hide();
}
}
void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (this.dataGridView1.SelectedRows.Count > 0)
{
if (this.dataGridView1.SelectedRows[0].Cells[0].Value != null)
{
this.textBox1.Text = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
this.textBox1.SelectionStart = this.textBox1.TextLength;
this.canDropDown = true;
this.popup.Hide();
}
}
}
DataTable dt;
ToolStripDropDown popup;
private bool canDropDown;
private DataGridView dataGridView1;
private void button1_Click(object sender, EventArgs e)
{
if (canDropDown)
{
this.popup.Show(this, this.textBox1.Left, this.textBox1.Bottom);
}
else
{
this.popup.Hide();
}
canDropDown = !canDropDown;
}
}
Hello, I have tried the code in this post and in another two different approaches for creating a ToolStripDropDown with a DataGridView which drops down from a ComboBox. The problem is the grid only show empty and it seems to resize what size it sees fit. But the interesting thing is you can resize rows and columns of the DataGridView even you cannot see them. When you come over a boundary, cursor changes to a resize cursor but you cannot see cells or any content other than background color. What am I doing wrong?
|