Menu for items in a listbox? - winforms

I'm not sure what its called or what i should look up. I'm using a listbox and a ContentMenuStrip which i attached with listBox1.ContextMenuStrip = contextMenuStrip1;
I would like to have items in my listbox right click able and select things like 'remove'. Using what i have now I can click anywhere on the listbox and i apply to results to the currently selected item. Which is confusing and prone to errors.
How do i make right clicks actually map to elements my mouse is over and not show a right click menu when it isnt over an element (empty space in a listbox).

The easy solution (not completely what you requested I know), would be just to make sure an item is selected before allowing the menu to open.
You do this by registering for the ContextMenuStrip.Opening event, and cancelling it if no item is selected.
If I think of something more clever I'll update :)
[EDIT]
OK cool, there's a IndexFromPoint method on the ListBox. You can use it to determine wether the mouse is over an item! Tell me if you need a code sample.
[EDIT2]
OK, OK, couldn't help myself.. here you go:
void ContextMenuStrip_Opening(object sender, System.ComponentModel.CancelEventArgs e)
{
if (listBox1.IndexFromPoint(listBox1.PointToClient(Cursor.Position)) == -1)
e.Cancel = true;
}

Related

WPF TreeView Cancel TreeViewItem Selection

I've been looking for a solution to this problem but I still can't find an adequate solution yet. Please help. Basically, I have a treeview that's bound to a list of objects. When the user clicks on a treeviewitem, I load data for a data grid based on the treeviewitem. The user can change data in the grid. If there's a change in the data grid and the user clicks on another treeviewitem, the user is asked to save. The user can choose Yes, No, or Cancel. When the user chooses cancel, the application has to make the user stay on the current treeviewitem until he/she saves the data grid. The problem is once the user has selected another treeviewitem, I can't make it so that the previous selection is selected again. There is no "SelectedItemChanging" event. WPF treeview only has SelectedItemChanged event. I'm trying multiple solutions but I always end up with an infinitely. Please offer any tips or advices.
Thanks,
Minh
You could just use a private field in which you store the item which was selected last, just handle the SelectedItemChanged event and always update the field as required. Also as Dave Clemmer suggested having an IsSelected property to which you can bind helps with selecting items programatically.

Silverlight AutoCompleteBox

Does anyone know how to drop down the AutoCompleteBox to see all the values without guessing at an entry and starting typing.
I know I could use a ComboBox but on a data entry form where a user needs to enter lots of information it is preferable for the user to pick up the mouse as little as possible and so therefore I wanted to use the AutoCompleteBox. However, in smaller lists it is also useful to quickly be reminded of the choices which you could do in a combo with the up/down arrow.
I have seen some examples of combining the two controls' functionality into one and I may go this way but wondered if there is a simpler way.
When I did this I had an autocomplete box on top of a combobox that were both bound to the same value, with the autocomplete box having a larger right margin so you could see the combobox arrow. Then I created a got focus event that opens the list of results and I set the MinimumPrefixLength to 0 so it would search with nothing typed in.
XAML
<sdk:AutoCompleteBox IsTextCompletionEnabled="True" MinimumPrefixLength="0" GotFocus="AutoComplete_GotFocus" />
Code Behind
private void AutoComplete_GotFocus(object sender, RoutedEventArgs e)
{
AutoCompleteBox box = (AutoCompleteBox)sender;
box.IsDropDownOpen = true;
}

Adding button that will replace the scroll in listView

i want to write some control that will contain 2 button and listView.
Pressing button 1 will scroll the listview up.
Pressing button 2 will scroll the listview down.
The direct scroll of the listview will be unavailable - ( will be invisible ? ).
I don't find the listview method 'scroll-up' / 'scroll-down' that i could callon the button event.
How can i make the listview scroll to be always visible ?
Someone can help me here ?
Thanks.
You have two options here, one is easier than the other.
First option (the easier, but slightly hacky way): Using the VisualTreeHelper, get a reference to the ScrollViewer in the ListView's ControlTemplate. Then you can use the LineUp and LineDown methods to scroll the content up and down and the static SetVerticalScrollBarVisibility method to hide the scrollbar. I personally wouldn't use this approach because I don't like relying on the Visual Tree which can change.
Second option (a bit harder, but not too bad if you know how): Write a new Control Template for the ListView (might need to alter the templates for it's ScrollViewer + ScrollBar), adding in two buttons that call the ScrollBar.LineUpCommand and ScrollBar.LineDownCommand. If you want to do this, I'd suggest getting a copy of ShowMeTheTemplate, then you can just copy and paste the original(s) and modify.
Hope this helps point you in the right direction.

In WPF, when selecting a TreeViewItem, how do I Focus on another control but still complete my TreeViewItem selection?

This sounds like a tricky question... let me ellaborate...
I have a treeView. When a treeViewItem is clicked/selected, I would like another TextBox to be focused.
The problem is that as soon as I add code to Focus the Textbox, it looks like the TreeView does not Show its selected node anymore (i.e. the treeItem is not Selected at all (or at least not visibly)).
Here is my event handling code...
private void trvTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
grpEditTreeItem.DataContext = (TreeItemDefinition)e.NewValue;
txtToken.SelectAll();
txtToken.Focus();
}
Any ideas?
Distinguish between Selected and Focused. You cannot have more than 1 Control focused at any one time.
What you want is your TreeView to Show it's selectednode when it has lost the focus.
Edit:
But I can confirm the problem, setting the Focus to another Control inside SelectedItemChanged() will Cancel the selection.
So what you need is something to postpone the Focus() call. A rough (but not ideal) solution is to place txtToken.Focus() in a trvTree_MouseUp() event handler. But that will also take the Focus away when expanding a Node for example.
So you will probably have to use a one-shot timer fired from SelectedItemChanged() .
this.Dispatcher.BeginInvoke((Action)delegate
{
txtToken.SelectAll();
txtToken.Focus();
});

WPF DataGrid ContextMenu(s)

How can I define different ContextMenus for each column in the DataGrid (Microsoft's grid)? I found out that the grid provides a ContextMenu attribute, but i want different context menu items for the columns, not the same ContextMenu for the whole grid.
Edit: Thanks for your answer! I tried to listen to the ContextMenuOpeningEvent as you suggested which was a first success: the ContextMenu can be modified in the EventHandler. But it raises another (hopefully small) problem - I now have to identify the column the mouse cursor was over when the ContextMenuOpeningEvent was triggered. I'm going to research how to do (or work around) that later.
I haven't played with it at all, so this might be wrong, but you may be able to override the ContextMenuOpening event and create the appropriate menu on the fly.
add menu item to default context menu might give you a starting point.
Good luck.

Resources