Windows Develop Bookmark and Share   
 index > Windows Forms General > Two problems, ToolStripDropDown
 

Two problems, ToolStripDropDown

Hi, I'm working on a multi column combobox and i use the toolstripdropdown class.

to create the combobox i'm using a textbox, a button(drop down whenclicked) and a datagridview(dropdown using toolstrip controlhost).

and now , the problems:

1.The combobox has an autocomplete option( so when the user writes text in the text box the selected row in the grid changes)the textbox cant getfocus unless the toolstripdropdown autoclose property is set to false(strange bug) but this causes the toolstripdropdown to stay open. what events should i handle in order for it to close properly?(i tried the enter and leave events but it didnt work)

2.I want the width of the contorl host to adjust to the width of the grid so here is what i do:

Code Block

m_host.AutoSize = True

m_host.AutoSize = False

If (m_host.Height > Me.m_nMaxHeight) Then

m_host.Height = Me.m_nMaxHeight

End If

this code is written before the toolstipdropdown.show call

i do this every time the grid drops down but forsome reason it doesnt work the first time the grid drops down, it works only from the second time. what could cause this?

Ithank in advance for those who will help me

EvilProject  Sunday, November 18, 2007 8:34 AM

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?

Kerem Kat  Tuesday, March 11, 2008 1:15 PM
Hai Zhi-Xin Ye

A question for you. i have some datasource refresh problem. i am loading one datagridview control based on the id of each row in the DataGridView. while on the cell enter of a particular cell. i will populate data in datagirdview and show in the toolstripdropdown control. Frist time data will show. after that datasource of the datagridview is not changing.

Any idea,

Regards
Raj
Raj - WebDev Raj  Monday, September 21, 2009 5:07 AM

You can use google to search for other answers

Custom Search

More Threads

• Make sure you are not dividing by zero.
• To Long for startup the Application
• Finding the PropertyGrid Delete Key Event in VB.NET
• always on top
• Terminating Windows forms using the X
• DataGridView: VerticalScrollingOffset readonly while HorizontalScrollingOffset is not
• datagrid
• Form_load code runs many times
• DataGridView - gray column at left
• Button.Click not working, but Button.lostFocus does