![](/img/trans.png)
[英]How to get TextEdit value to Cell in GridView in Winforms Devexpress?
[英]DevExpress select all when clicked on GridView cell (TextEdit)
我需要這樣做,以便當用戶在網格視圖中單擊帶有 TextEdit 的單元格時,它將選擇 textedit 中的所有內容。 我嘗試了很多我可以在互聯網上找到的方法,但沒有一個效果很好。
“EditorShowMode = MouseUp”方式會破壞一切,例如當您單擊具有 checkedit 的單元格時; 它選擇單元格,然后您需要再次單擊以實際單擊 CheckEdit。
“使用 EditorShowMode = MouseUp 並在 MouseDown 上手動處理其他事情”只是ew。 不適用於所有類型的控件。
“在 ShownEditor 事件上更改選擇長度等”方式也不起作用,實際上它在單擊時選擇文本,但它不會覆蓋默認功能,因此選擇會立即更改。 還嘗試了 SelectAll 方法,但它有一些我不記得的問題(可能根本不起作用)。
我真的嘗試了很多東西,但找不到一個完全好的方法。 請告訴我您是否可以在不破壞網格中其他類型控件的情況下獲得工作方式。
Pavel 在 DevExpress 支持上的回答(效果很好):
實現此目的的最簡單方法是使用 GridView.ShownEditor 事件訂閱活動編輯器的 MouseUp 事件。 然后,選擇 MouseUp 事件處理程序中的所有文本並分離此處理程序以避免后續的文本選擇。
private void GridView_ShownEditor(object sender, EventArgs e)
{
GridView view = sender as GridView;
if (view.ActiveEditor is TextEdit)
view.ActiveEditor.MouseUp += ActiveEditor_MouseUp;
}
private void ActiveEditor_MouseUp(object sender, MouseEventArgs e)
{
BaseEdit edit = sender as BaseEdit;
edit.MouseUp -= ActiveEditor_MouseUp;
edit.SelectAll();
}
您可以使用 GridView CustomRowCellEdit 事件並設置文本編輯器的事件,例如鼠標向上。 可以像示例中那樣設置 RepositoryItemTextEdit MouseUp 事件。
例子:
private void gridView1_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
{
if (e.RepositoryItem is DevExpress.XtraEditors.Repository.RepositoryItemTextEdit)
{
DevExpress.XtraEditors.Repository.RepositoryItemTextEdit rep = new DevExpress.XtraEditors.Repository.RepositoryItemTextEdit();
rep.ReadOnly = false;
rep.MouseUp += rep_MouseUp;
e.RepositoryItem = rep;
}
}
void rep_MouseUp(object sender, MouseEventArgs e)
{
DevExpress.XtraEditors.TextEdit te = sender as DevExpress.XtraEditors.TextEdit;
te.SelectAll();
}
您應該處理 TextEdit 的 Enter 事件
private void myRepositoryItemTextEdit_Enter(object sender, EventArgs e)
{
var editor = (DevExpress.XtraEditors.TextEdit)sender;
BeginInvoke(new MethodInvoker(() =>
{
editor.SelectionStart = 0;
editor.SelectionLength = editor.Text.Length;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.