如何激活combobox第一次点击(Datagridview) [英] How to activate combobox on first click (Datagridview)
问题描述
在winforms中,您需要单击两次组合框以正确激活它 - 第一次对焦,第二次实际获取下拉列表。
我如何改变这种行为,使其在第一次点击激活?
编辑:哦上帝..我忘了这是为DATAGRIDVIEW组合框。我的错。对不起。
我意识到这是一个老问题,但我想我会给我的解决方案给任何人,
虽然我找不到任何答案做这个...我找到了一个 answer 到帮助我的另一个问题。
这是我的解决方案:
private void datagridview_CellEnter(object sender,DataGridViewCellEventArgs e)
{
bool validClick =(e.RowIndex!= -1& & e.ColumnIndex!= -1); //确保点击的行/列有效。
var datagridview = sender as DataGridView;
//检查以确保单击的单元格是包含组合框的单元格
if(datagridview.Columns [e.ColumnIndex]是DataGridViewComboBoxColumn&& validClick)
{
datagridview.BeginEdit(true);
((ComboBox)datagridview.EditingControl).DroppedDown = true;
}
}
上述代码必须绑定到
添加了列索引检查,以在选择整个行时防止崩溃。感谢,上所有夜晚上述编辑
edit2:绑定到CellEnter而不是CellClick事件。
In winforms, you need to click the combobox twice to properly activate it - the first time to focus it, the second time to actually get the dropdown list.
How do I change this behavior so that it activates on the very first click?
Edit: Oh god.. I forgot that this is for DATAGRIDVIEW combobox. My mistake. Sorry.
I realize this is an old question, but I figured I would give my solution to anyone out there that may need to be able to do this.
While I couldn't find any answers to do exactly this... I did find an answer to a different question that helped me.
This is my solution:
private void datagridview_CellEnter(object sender, DataGridViewCellEventArgs e)
{
bool validClick = (e.RowIndex != -1 && e.ColumnIndex != -1); //Make sure the clicked row/column is valid.
var datagridview = sender as DataGridView;
// Check to make sure the cell clicked is the cell containing the combobox
if(datagridview.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn && validClick)
{
datagridview.BeginEdit(true);
((ComboBox)datagridview.EditingControl).DroppedDown = true;
}
}
The above code must be tied into the CellEnter event of the datagridview.
I hope this helps!
edit: Added a column index check to prevent crashing when the entire row is selected.
Thanks, Up All Night for the above edit
edit2: Code is now to be tied to the CellEnter rather than the CellClick event.
Thanks, HaraldDutch for the above edit
这篇关于如何激活combobox第一次点击(Datagridview)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!