I need to make it so when the user clicks on a cell with TextEdit in a grid view, it will select all in the textedit. I tried many many ways i could find in the internet, but none of them work well.
"EditorShowMode = MouseUp" way breaks everything, for example when you click on a cell that has checkedit; it selects the cell, then you need o click again to actually click on the CheckEdit.
"Use EditorShowMode = MouseUp and manually handle other things on MouseDown" is just ew. Won't work fine for all types of controls.
"Change selection length etc. on ShownEditor event" way doesn't work too, actually it selects the text when clicked, but it doesn't override the default function so the selection instantly changes. Also tried the SelectAll method but it had some problems that i dont remember (probably didnt work at all).
I have really tried many things, but couldn't find a totally fine way. Please tell me if you can get a working way without breaking other types of controls in the grid.
Answered by Pavel on DevExpress Support (works great):
The easiest way to achieve this is to use the GridView.ShownEditor event to subscribe to the active editor's MouseUp event. Then, select all text in the MouseUp event handler and detach this handler to avoid subsequent text selection.
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();
}
You could use GridView CustomRowCellEdit event and set an event of text editor such as Mouse Up. Setting the RepositoryItemTextEdit MouseUp event can be set as in the example.
Example:
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();
}
You should handle Enter event for TextEdit
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;
}
}
Related
I have a TextBox and I want all the text inside of it to be highlighted when the user clicks on it (so that they can replace it easily). I have the following event handler linked up the the TextBox:
private void TextBox_Enter(object sender, EventArgs e) {
SelectAll();
}
When I click on the TextBox, the text is only selected for a fraction of a second (sometime it's so fast I can't see it at all) and then it goes back to being a cursor. Does anyone know how to fix this or if there are any relatively simple workarounds?
I tried the same thing with the TextBox.MouseClick event (and it highlighted the text), but because it was the MouseClick event the text was highlighted every time I clicked the TextBox (even when the TextBox already had focus).
I have also tried SelectionStart = 0; SelectionLength = Text.Length, but the same thing happens. This leads me be believe the issue has something to do with the event.
I also tried the TextBox.GotFocus event and had the exact same problem.
I am doing this in a Windows Form application.
The reason why you didn't see the text getting selected is that the TextBox is busy when one of those events occurred (e.g., caret positioning). You actually select the text, but then the internal event handlers of the TextBox execute and remove the selection e.g. by setting the caret position.
All you have to do is to wait until the internal event handlers have completed.
You do this by using the Dispatcher. When you invoke the Dispatcher asynchronously the delegate is not immediately executed but enqueued and executed once all previously enqueued actions (like the internal event handlers) are cleared from the dispatcher queue.
So going with the TextBox.GotFocus event in WPF (or the TextBox.Enter in WinForms) and the asynchronous Dispatcher will do the trick:
WPF
private async void SelectAll_OnTextBoxGotFocus(object sender, RoutedEventArgs e)
{
await Application.Current.Dispatcher.InvokeAsync((sender as TextBox).SelectAll);
}
WinForms
private void SelectAll_OnTextBoxEnter(object sender, EventArgs e)
{
var textBox = sender as TextBox;
textBox.BeginInvoke(new Action(textBox.SelectAll));
}
Thankfully I found a solution! It turns out that the Click event is executed before the Enter event, this allowed me to set up a JustGotFocus variable and do the following:
private void myTextBox_Click(object sender, EventArgs e) {
this.JustGotFocus = true;
if (JustGotFocus) {
myTextBox.SelectAll();
}
}
private void myTextBox_Enter(object sender, EventArgs e) {
JustGotFocus = false;
}
If anyone else has this problem hopefully my solution is useful.
I have code that creates an UltraGrid with a column that has the ColumnStyle.Button as it's style.
private void Grid_InitializeLayout(object sender, InitializeLayoutEventArgs e){
Grid.ResetEmptySelectedAppearance();
var column = Grid.SetColumn("Draw Lines", "Draw Lines", 30);
column.Style = ColumnStyle.Button;
Grid.HideOtherColumns();
}
Now I would like too make it react to being clicked on. I have found this but it does not show me how to bind the variable, usually I can either double click in the visual studio editor (but in this case this only directs me too the Grid_InitializeLayout) or I can go the the item in question and add the function to an OnClick variable, but this one doesn't exist.
private void Grid_InitializeRow(object sender, InitializeRowEventArgs e)
var buttoncell= e.Row.Cells["Draw Lines"];
//something here?
this is what I want to call
private void OnDrawLine(object sender, Infragistics.Win.UltraWinGrid.CellEventArgs e)
{
Debug.Print("test test");
}
It's probably something trivial but I'm stuck.
The UltraGrid responds to clicks on a cell button as explained in the link posted using an event called ClickCellButton. You need to subscribe to this grid event in the usual ways. (Designer or code doesn't matter)
private void Grid_ClickCellButton(object sender, ClickCellEventArgs e)
{
// Check if the click happens on the required column
if (e.Cell.Column.Key == "Draw Lines")
{
... your code ...
}
}
I need to update the edit value of the CheckedComboBoxEdit control immediately after the item has been checked
The MouseUp event can be used to trigger EndEditwhich will commit the change slightly faster.
I used this to solve my simular issue when working with a CheckBox within a DateGridView. As by default the event to submit changes in a DataGridView only fires when leaving the cell.
You should subscribe Popup event of CheckedComboBoxEdit, find CheckedListBoxControl and subscribe ItemCheck event. Like this:
void _orgStructEntitesCheckedComboBoxEdit_Popup(object sender, EventArgs e)
{
var popup = (IPopupControl)sender;
var control = popup.PopupWindow.Controls.OfType<PopupContainerControl>().First().Controls.OfType<CheckedListBoxControl>().First();
control.ItemCheck += control_ItemCheck;
}
void control_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
{
var checkedListBoxControl = (CheckedListBoxControl)sender;
var current = checkedListBoxControl.Items[e.Index];
}
Use e.Index to get current item changed.
More information here and here.
I'm using a WPF DataGrid with c#/xaml in Visual Studio 2013.
With SelectionMode="Extended", I'm able to multi-select rows in the grid.
I have a requirement where clicks on one of the columns of the grid are to be ignored relative to row selection.
I setup a PreviewMouseLeftButtonDown event that gets called.
Since it's a preview event, at the time of the event is processed, the selection in the grid hasn't changed yet.
I'm able to determine the row and column of the click, so I can determine a click has been made in a column that I don't want
I want to be able to abort the click event at that point so that no change is made to the current selected items in the grid. Is that possible?
In the mouse down event I tried something like:
private void GridCtrl_MouseDown(object sender, MouseButtonEventArgs e)
{
// ... Other code
e.Handled = true;
}
But, despite being marked as handled, it still continues and performs the row selection.
I also have a 'SelectionChanged' event that I see that it later gets into.
I think you actually need to handle both tunneling events - one for PreviewLeftMOuseButtonDown and another for PreviewSelectionChanged.
My advice is create a flag, let's call it:
bool _cancelSelectionChange = false;
Then, in your Mouse handler:
private void GridCtrl_MouseDown(object sender, MouseButtonEventArgs e)
{
_cancelSelectionChange = false;
// ... Other code
_cancelSelectionChange = true;
e.Handled = true;
}
Finally, in your selection change handler for the tunneling event:
private void GridCtrl_PreviewSelectionChanged(object sender, SelectionChangedEventArgs e)
{
e.Handled = _cancelSelectionChange;
}
I'm creating listviews in a flowpanel at run time which later will accept drag and dropped files. the reason being is i want these to act as folders so a user double clicks and gets a window displaying the contents.
i'm having difficulty setting up the events for my listviews as they are added.
how do i create some events (like MouseDoubleClick and DragDrop) dynamically for each added listview? can i create a single function for both of these events and have listview1, listview2, listviewX use it?
i have a button that is adding the listviews, which works fine. please advise, i apologize if this is too conceptual and not exact enough.
private void addNewWOButton_Click(object sender, EventArgs e)
{
ListView newListView = new ListView();
newListView.AllowDrop = true;
flowPanel.Controls.Add(newListView);
}
You would have to have the routine already created in your code:
private void listView_DragDrop(object sender, DragEventArgs e) {
// do stuff
}
private void listView_DragEnter(object sender, DragEventArgs e) {
// do stuff
}
and then in your routine, your wire it up:
private void addNewWOButton_Click(object sender, EventArgs e)
{
ListView newListView = new ListView();
newListView.AllowDrop = true;
newListView.DragDrop += listView_DragDrop;
newListView.DragEnter += listView_DragEnter;
flowPanel.Controls.Add(newListView);
}
You would have to check who the "sender" is if you need to know which ListView control is firing the event.
You can also just use a lambda function for simple things:
newListView.DragEnter += (s, de) => de.Effect = DragDropEffects.Copy;
Just make sure to unwire the events with -= if you also remove the ListViews dynamically.
To answer the other half of your question, you can use a single handler for any event, from any source, that has the handler's signature. In the body of the handler, you just have to check the sender argument to determine which control raised the event.
You need a way to tell one control from a different one of the same class, however. One way to do this is to make sure to set the Name property on each control when you create it; e.g., newListView.Name = "FilesListView".
Then, before you do anything else in your event handler, check the sender.
private void listView_DragDrop(object sender, DragEventArgs e) {
ListView sendingListView = sender as ListView;
if(sendingListView == null) {
// Sender wasn't a ListView. (But bear in mind it could be any class of
// control that you've wired to this handler, so check those classes if
// need be.)
return;
}
switch(sendingListView.Name) {
case "FilesListView":
// do stuff for a dropped file
break;
case "TextListView":
// do stuff for dropped text
break;
.....
}
}