How to detect click in GridView - c#

I have a GridView, and in this Gridview there are several Grids which contain other elements. If I click onto one element of my GridView (one Grid) I want to open details about this element, but Grid and GridView dont seem to support setting a click method. What else can I do to call a method when an element is clicked?

but Grid and GridView dont seem to support setting a click method
Use Tapped event.

you could add an invisible rectangle or other element to register an OnMouseOver event or click ect.

Each of your Grid should subscribe to 3 events and have transparent background:
<Grid Background="Transparent" PointerPressed="Grid_OnPointerPressed" PointerReleased="Grid_OnPointerReleased" PointerExited="Grid_OnPointerExited">
in code you may simulate click event like this:
private bool _isPressed;
private void Grid_OnPointerPressed(object sender, PointerRoutedEventArgs e)
{
_isPressed = true;
}
private void Grid_OnPointerReleased(object sender, PointerRoutedEventArgs e)
{
if (_isPressed)
{
//your logic on click event
}
_isPressed = false;
}
private void Grid_OnPointerExited(object sender, PointerRoutedEventArgs e)
{
_isPressed = false;
}
or the easiest way is to wrap your Grid into Button

Related

DevExpress select all when clicked on GridView cell (TextEdit)

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;
}
}

C# WinForms toggle button "enabled" property based on listView.selectedItems.count

I'd like to be able to have a button ONLY enabled when a certain listview has a selected item... such as listView1.SelectedItems.Count > 0
I can enable a button once a listViewItem is selected... but I can't figure out how to UNenable once the user clicks away from the listView.
Is there any "ListViewItem DeActivate" function? I've looked around but can't find anything.
You can do this in the SelectedIndexChanged event ...
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
button1.Enabled = listView1.SelectedItems.Count > 0;
}
use this code i am writing this code by supposing that in list at 0 index you have nothing to select or it has --select-- value
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if(listView1.SelectedItems.Count>0)
{
//this code will disable the button if it has any selection
button1.Enabled =false;
}
if(listView1.SelectedItems.Count==0)
{
//this code will enable the button if it has any selection
button1.Enabled =true;
}
}
Look into the Lostfocus event and then try something like this.
private void Lost_Focus_Ev(object sender, RoutedEventArgs e)
{
My_button.IsEnabled = false;
}
Everytime a user selects another control the button will be disabled. You can renable the button when the listview is re selected.

If touch a control, how can I get this control name?

After I touch a control, such as Button, How can I get this button's name?
If you mean "click" instead of "touch", put something like this in your View XAML file:
<Button x:Name="MyButton" MouseDown="MyButton_MouseDown">
Then add following lines to the View CODE file:
void MyButton_MouseDown(object sender, MouseButtonEventArgs e) {
base.OnMouseUp(e);
var control = sender as FrameworkElement;
Console.WriteLine(control.Name);
}
When you click on a control you are raising an event which called : Control_Click
Then you can do whatever you want in that even. As you asked about button I'm gonna show you how you can get button's name . You can have the similar code for other controls :
private void button1_Click(object sender, EventArgs e)
{
string control_name = button1.Name;
MessageBox.Show(control_name);
}

C# WPF/XAML Preview Mouse Event for DataGrid

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;
}

Data Grid View Selection c#

Recently I made the 2048 game using a DataGridView. Everything works except for the blue selection/focus that appears on the DataGrid when my form starts and I use the arrow keys. I tried to remove it with ClearSelection(), works except for the arrows. How could I disable the blue selected cell? How can I disable the arrows?
public Form1_Load (......)
{
DataGridView1.ClearSelection();
}
Image Link (I need more reputation to upload it on the site)
http://s23.postimg.org/beekn9i6z/Immagine.png
Screenshot of Datagrid during Runtime
http://s1.postimg.org/s5loh0uvj/datagrid.png
Handling selection in form load is too early because form is not shown yet. Perform datagridview clear selection in form shown event.
private void Form1_Shown(object sender, EventArgs e)
{
DataGridView1.ClearSelection();
}
OR
void dataGridView2_SelectionChanged(object sender, EventArgs e)
{
dataGridView2.ClearSelection();
}
Try this
DataGridView1.CurrentCell.Selected = false;

Categories