In my .NET 4,5 Winforms application, the ListView control's MouseUp event is firing multiple times when I open a file from that event as follows:
private void ListView1_MouseUp(object sender, MouseEventArgs e)
{
ListViewHitTestInfo hit = ListView1.HitTest(e.Location);
if (hit.SubItem != null && hit.SubItem == hit.Item.SubItems[1])
{
System.Diagnostics.Process.Start(#"C:\Folder1\Test.pdf");
MessageBox.Show("A test");
}
}
Note: When clicking on the SubItem1 of the listview, the file opens but the message box appears at least twice. But, when I comment out the line that opens the file the message box appears only once (as it should). I do need to open the file whose name is clicked by the user in the listview. How can I achieve this without the MoueUp event firing multiple times?
Please also note that the MouseClick event for listview does not always work as also stated here. So, I have to use MouseUp event.
EDIT: I should mention the ListView is in Details mode.
Avoid HitTest() and use ListView's native function GetItemAt(). An example from MSDN looks like this:
private void ListView1_MouseDown(object sender, MouseEventArgs e)
{
ListViewItem selection = ListView1.GetItemAt(e.X, e.Y);
// If the user selects an item in the ListView, display
// the image in the PictureBox.
if (selection != null)
{
PictureBox1.Image = System.Drawing.Image.FromFile(
selection.SubItems[1].Text);
}
}
Related
I have a window form containing 2 list views (for instance, named a and b) and a button. The function of the button is supposedly to show a message box differently when there is focus between the 2 list views.
so the code for the button is
if (a.Focused)
MessageBox.Show("a");
else
MessageBox.Show("b");
However, when I selected the item in 'b' list view. It always return the MessageBox.Show(b). When I start over again and selected a item in 'a' list view, it give me an null reference exception.
Anything wrong with the code?
It's because when you click the button it's focused. So it will always show "b" because listview a is not focused.
Here's a suggestion: save the latest focused listview, and make a GotFocus event in both listviews (it's not in the designer so make it programatically), when clicking the button check what the latest focused listview is:
//Put this code after InitializeComponent();
ListViewA.GotFocus += ListViewFocus;
ListViewB.GotFocus += ListViewFocus;
...
private ListView Latest = null;
private void ListViewFocus(object sender, EventArgs e)
{
Latest = (sender as ListView);
}
private void Button_Click(object sender, EventArgs e)
{
if (Latest == null) MessageBox.Show("No listview is focused");
else if (Latest.Name == "ListViewA") MessageBox.Show("a");
else MessageBox.Show("b");
}
There's also a LostFocus event, if you want to make null again.
I don't know why you get the null reference exception. What line is causing it?
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 am quite new to Winform dev. I have two list boxes. When the user double clicks an item in the first listbox, I want this to be copied to the second list box. The problem is that my double click method never gets fired.
here's my code:
//here I register the event
this.fieldsArea.MouseDoubleClick += new MouseEventHandler(fieldsArea_MouseDoubleClick);
Then here is the double click method:
private void fieldsArea_MouseDoubleClick(object sender, MouseEventArgs e)
{
MessageBox.Show("from method");
int index = fieldsArea.IndexFromPoint(e.Location);
string s = fieldsArea.Items[index].ToString();
selectedFieldsArea.Items.Add(s);
}
So I want the element from fieldsArea to be copied to selectedFieldsArea... The messagebox never shows and in debug I see that I never enter this method...
Am I missing something here?
ps: I have drag drop implemented which works well.
UPDATE: The problem comes from the MouseDown event also being implemented. So here's my mousedown event.
private void fieldsArea_MouseDown(object sender, MouseEventArgs e)
{
if (fieldsArea.Items.Count == 0)
return;
int index = fieldsArea.IndexFromPoint(e.Location);
string s = fieldsArea.Items[index].ToString();
DragDropEffects dde1 = DoDragDrop(s,
DragDropEffects.All);
}
ps: I have drag drop implemented which works well.
That means probably that you have registered a MouseDown event, which interfere with the MouseDoubleclick.
Just for testing purpose, try to delete the Drag&Drop implementation ( unregister the MouseDown event) and then the MouseDoubleclick should work.
Make sure you don't have other Mouse event like MouseClick MouseDown event registered, which could interfere with MouseDoubleclick event.
Update:
Add following code in your MouseDown event handler, you can check if it is a double-click first.
if(e.Clicks>1)
{
int index = fieldsArea.IndexFromPoint(e.Location);
string s = fieldsArea.Items[index].ToString();
selectedFieldsArea.Items.Add(s);
}
so here is your new handler:
private void fieldsArea_MouseDown(object sender, MouseEventArgs e)
{
if (fieldsArea.Items.Count == 0)
return;
int index = fieldsArea.IndexFromPoint(e.Location);
string s = fieldsArea.Items[index].ToString();
if(e.Clicks>1)
{
selectedFieldsArea.Items.Add(s);
}
else
{
DragDropEffects dde1 = DoDragDrop(s,
DragDropEffects.All);
}
}
I believe you may have either "MouseClick/MouseDown" event or "SelectedIndexChanged" event, these events resist to get fire of "MouseDoubleclick" event, so you need to handle them properly. Thanks
Here is the problem: I have a Windows Forms application that I'm developing, and in one segment I'm using a ListView control.
What I'm trying can be simply stated as: on event ListViewItemSelectionChange show a MessageBox for user to confirm the change, if not confirmed change to let's say the first item. This change to the first item would again fire ListViewItemSelecionChange, so I unregister and re-register the event handler method, so everything should be good, right?
What actually happens is that the handler method is called twice (actually ListView should fire two events on Selection change, one for deselect, other for newly selected item, but I have an e.IsSelected statement at the beginning to catch only selected items, so actually you could say that there are four events fired).
The problem is, if I generated the first event with mouse click on ListView item, and I've unsubscribed before programatically changing to the first item, what generates the second event firing? Is it some focus change because of the MessageBox call? Is there any way to prevent the second event to fire?
I have a simple example solution here, it can't be more simlified (25 SLOC), so if you can, please take a look. Note that commenting the line "if (ShowMessageBox())" stops the second event from firing, is this some focus change problem?
http://www.filedropper.com/listviewtestwithmsgbox
Edit: the relevant code:
private void listViewWithSelection1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
// listview actually generates two ItemSelectionChanged events,
// one for deselect of a item, and another event for a newly selected item (which we want here).
if (e.IsSelected)
{
if (ShowMessageBox())
Button1_Click(null, EventArgs.Empty);
label1.Text += "item selected ";
}
}
private bool ShowMessageBox()
{
return MessageBox.Show("Change to first item instead?", "test", MessageBoxButtons.YesNo) == DialogResult.Yes;
}
private void Button1_Click(object sender, EventArgs e)
{
// change ti first ListView item
listView1.ItemSelectionChanged -= listViewWithSelection1_ItemSelectionChanged;
listView1.Items[0].Selected = true;
listView1.ItemSelectionChanged += listViewWithSelection1_ItemSelectionChanged;
}
Hmm, can you describe how the selection is being changed to begin with? If it's by the user clicking to select an item, perhaps catch the Click or DoubleClick event rather than the ItemSelectionChanged event? I have this snippet I'm using on a program currently. If the user double clicks the list box (listView, in your case), do something with the selected item.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private bool ShowMessageBox()
{
return MessageBox.Show("Change to first item instead?", "test", MessageBoxButtons.YesNo) == DialogResult.Yes;
}
private void listView1_Click(object sender, EventArgs e)
{
if (ShowMessageBox())
listView1.TopItem.Selected = true;
label1.Text += "item selected ";
}
}
Edited to include relevant code.
One way to do this is to have a flag which says should the on change code run.
In your ListViewItemSelecionChange code you check the value of the flag and run code accordingly.
I have a C# ComboBox using WPF. I have code that executes when the ComboBox's GotFocus is activated. The issue is that the GotFocus event is executed every time a selection is made from the ComboBox. For example, the GotFocus is executed when you first click on the ComboBox and then when you make a selection even though you have not click on any other control.
Is it possible to prevent this event from firing if a selection is being made in the list or is there a flag or something else in the event handler that can be used to determine if the GotFocus event handler was fired as a result of the user selecting an item in the list?
You can solve this problem with next verification:
private void myComboBox_GotFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() == typeof(ComboBoxItem))
return;
//Your code here
}
This code will filter all focus events from items (because they use bubble routing event). But there is another problem - specific behaviour of WPF ComboBox focus: when you open drop-down list with items your ComboBox losing focus and items get. When you select some item - item losing focus and ComboBox get back. Drop-down list is like another control. You can see this by simple code:
private void myComboBox_GotFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() != typeof(ComboBoxItem))
{
Trace.WriteLine("Got " + DateTime.Now);
}
}
private void myComboBox_LostFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() != typeof(ComboBoxItem))
{
Trace.WriteLine("Lost " + DateTime.Now);
}
}
So you will get anyway atleast two focus events: when you select ComboBox and when you selecting something in it (focus will return to ComboBox).
To filter returned focus after selecting item, you can try to use DropDownOpened/DropDownClosed events with some field-flag.
So the final code with only 1 event of getting focus:
private bool returnedFocus = false;
private void myComboBox_GotFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() != typeof(ComboBoxItem) && !returnedFocus)
{
//Your code.
}
}
private void myComboBox_LostFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() != typeof(ComboBoxItem))
{
ComboBox cb = (ComboBox)sender;
returnedFocus = cb.IsDropDownOpen;
}
}
Choose from this examples what you actually need more for your application.
I'm not too hot on WPF; but if you're trying to detect changes to the list (click on new value etc) you can use SelectedIndexChanged events..
On the other hand, if you really do want to know simply when the control is focussed, can you filter it by saying something like;
if (combo1.Focused && combo1.SelectedIndex == -1)
{
...
}
.. ? It really depends on what youre trying to detect, exactly.
Another solution is used is to determine whether the new focused element is an existing item in the combobox. If true then the LostFocus event should not be performed, because the combobox still has focus. Otherwise an element outside the combobox received focus.
In the code snipplet below I added the functionality in a custom combobox class
public class MyComboBox : System.Windows.Controls.Combobox
{
protected override void OnLostFocus(RoutedEventArgs e)
{
//Get the new focused element and in case this is not an existing item of the current combobox then perform a lost focus command.
//Otherwise the drop down items have been opened and is still focused on the current combobox
var focusedElement = FocusManager.GetFocusedElement(FocusManager.GetFocusScope(this));
if (!(focusedElement is ComboBoxItem && ItemsControl.ItemsControlFromItemContainer(focusedElement as ComboBoxItem) == this))
{
base.OnLostFocus(e);
/* Your code here... */
}
}
}