the mouse scroll change the selected index of combobox - c#

There is a combo box on a form.
The Code when the form is loaded.
private void Form_Load(object sender, EventArgs e)
{
combo.Items.Clear();
combo.Items.Add("01");
combo.Items.Add("02");
combo.SelectedIndex = 1;
}
If I scroll the mouse wheel that it will change the index of the combobox.
How to avoid this?

Change selected element on mouse scroll is default behaviour of focused combobox. If you want to suppress this, you can use PreFilterMessage function as described here.

You should change the Tab Order of your controls.Set all tab-order of your controls.
Go form design.Click View in menu bar,then choose Tab Order.
Your combo tab order must not be the first one.
If u want to change from coding,
combo.TabIndex=10;
The problem is your combo is selected at form start.So you scroll mouse wheel,it's selected index changed.

Related

Editable multiline combobox WPF

I have an issue with editable combobox. I can get textbox PART_EditableTextBox and make it multiline by AcceptsReturn="True" and TextWrapping="Wrap".
The problem I have is that I cannot make it work like classic textbox for text operations.
For example key down does not navigate in text and instead just fire item selection even if there is just 1 item in my collection it effectively refresh to original value. Same for scroll bar.
I found that I can catch some events (previewkeydown on parent element/ PreviewMouseWheel) and handle them to not reset value but still missing the text navigation(moving in text by arrows) or having scroll bar.
Edit
I find solution.
Insted of using ComboBox I used ComboBoxEdit from DevExpress.
Now it gets quite easy since they have already implemented property to solve halve of my problems.
this.ComboBoxEdit.AllowSpinOnMouseWheel = false
this.ComboBoxEdit.AcceptsReturn = true;
this.ComboBoxEdit.TextWrapping = TextWrapping.Wrap;
Only problem left was text navigation by arrows.
I catched PreviewKeyDown from parent element and implemented this.
private void Grid_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.OriginalSource is TextBox tx)
{
if (e.Key == Key.Up)
{
EditingCommands.MoveUpByLine.Execute(null, tx);
e.Handled = true;
}
else if (e.Key == Key.Down)
{
EditingCommands.MoveDownByLine.Execute(null, tx);
e.Handled = true;
}
}
}
I think the plan seems like a bad idea.
There are fundamental mismatches between functionality an editable combobox gives and what you seem to require from a multiline textbox inside one.
For one, the arrows already have an inherent meaning to a combobox. That's how you navigate the autocomplete list using keyboard.
An editable combobox is intended to do autocomplete as well as possibly add a new item.
Maybe you could have a textbox and a combobox. The user switches between these using a togglebutton or some such. Depending on whether they want to add new or look up an existing item.
I think your best bet is likely to build your own usercontrol if you particularly need this to look similar to a combobox.
The editable part would be a textbox.
The list of possibles can be a listbox inside a popup or expander.
You could choose whether to use a togglebutton to show your popup like the combobox does, or not.
Add the functionality you need without the problems functionality inherent to a combobox control adds.

Select unfocusable ListViewItem by clicking

I have a ListView that gets populated by an ObservableCollection via Binding. Its ListViewItems should have Focusable="False" as I don't want a TextBox to lose keyboard focus when clicking anything. Unfortunately, this prevents ListViewItems from being selected by clicking on them. How can I make ListViewItems selectable by clicking without changing the current focus?
I tried selecting an item in code by doing
myListView.SelectedIndex = i;
but I couldn't find a way to find the index of the clicked item. I can't use VisualTreeHelper.GetChild() to check for IsMouseOver because the data binding does not actually add controls to the visual tree.
Please don't suggest resetting the focus to the TextBox. The TextBox should not lose focus at all.
I figured it out thanks to this question.
I added PreviewMouseDown="OnMouseDown" to my ListViewItem template (in my case to the Border element) which selects the corresponding item by doing:
private void OnMouseDown(object sender, MouseButtonEventArgs e)
{
var item = (sender as Border).DataContext;
myListView.SelectedIndex = myListView.Items.IndexOf(item);
}

WPF Datagrid in ScrollView Captures Row Details ComboBox Scrolling events

I have a datagrid inside a scrollviewer. The rows of my datagrid can be expanded to show the details and edit the row items.
In order for the "page" to scroll when the number of rows/row details view pushes the grid too long, I am handling the PreviewMouseWheel event on the datagrid. This works until I have a combobox in my row details with a droplist that has enough items that it also needs to scroll.
When using the mouse wheel, the droplist will scroll, but so does the outer datagrid, effectively scrolling the contents "behind" the droplist and leaving the droplist in the wrong place.
My PreviewMouseWheel event handler does the following:
private void StampPartsDatagrid_PreviewMouseWheelForScrolling(object sender, MouseWheelEventArgs e)
{
StampingScrollViewer.ScrollToVerticalOffset(StampingScrollViewer.VerticalOffset - e.Delta / 3);
}
I tried to trap the dropdown/drop up event on the combo, and while that works, if I move the mouse outside the drop list and scroll with the wheel, the page doesn't scroll.
I looked through many articles on MSDN and SO but they were mostly about getting the datagrid to scroll when inside a scrollviewer.
Is there a way to do a hit test on the Preview scrolling event? Should I trap the mouse/enter leave on the droplist somehow? Should I be handling my scrolling differently?
Thanks
* EDIT *
I resolved this issue. I was overthinking the mouse wheel behaviour. By looking at the Windows settings panel page for regions, which has a very long combo list to display, I saw that if the combo list was dropped the mousewheel only applied to he dropped list. Moving the mouse outside the droplist and scrolling with the wheel has no effect.
Given this, I adopted the same behaviour by trapping the drop open /drop close events and controlling the outer scrolling. My new event handler, taken from this post (https://social.msdn.microsoft.com/Forums/vstudio/en-US/6fc503a6-ba53-4395-b9b8-f56301efd097/mousescroll-of-combobox-scrolls-the-page-as-well?forum=wpf) is this:
private void StampPartsDatagrid_PreviewMouseWheelForScrolling(object sender, MouseWheelEventArgs e)
{
if (!bDetailsComboDropped)
{
StampingScrollViewer.ScrollToVerticalOffset(StampingScrollViewer.VerticalOffset - e.Delta / 3);
}
}
If you don't like to have an bDetailsComboDropped field, you can also try to determine where the preview mouse wheel routed event is coming from. Specifically, check e.OriginalSource to get a reference to the WPF element that was actually triggering the event (or e.Source if you find it better for the purpose).
Combine this with a recursive function using VisualTreeHelper.GetParent to check whether any of the parent elements is a combo box part, or it ends with your root scroll viewer.
(Note: your solution with the Boolean flag might actually provide higher performance, I just posted this in case someone needs a source-oriented solution, e.g. if there were many other controls to check against scrolling internally, etc.)

C# How to disable the "tour" of Down keys, Up, by the items?

Well, I created an editor program (football) where the main focus is a combobox.
Which constantly has to be triggered to select the desired team and make changes and etc.
Only it gets very boring and impractical always having to click with the mouse on it to focus it and select the team with the Down and Up keys,
So what I wanted was to disable the "walk" of the Down and Up keys by the other items,
For example: there are 6 radioButtons and when I select the team in the combobox and then I click on a radioButton to mark the modification and save, I would click on Down or Up and the focus will automatically return to the combobox,
And not go down or up to another radioButton or button understood?
Or if the combobox did not lose its focus ever, no matter how much it clicked on another item.
void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
MyCombo.Select();
}

How to stop a button tap event from bubbling into a listview?

I have a listview with a button as part of the datatemplate. When I click the button I want an action to happen, but I don't necessarily want that item selected. Is there any way I can stop the tap event from bubbling up to the listbox?
Thanks!
Not really sure this is the best solution but. Capture the selected index before you get to the method the button activates. Inside of it at the end set selected back to the original.
I created a sample ListView with a button in it. Clicking on the button does not select the item for me. Can you post your DataTemplate?
But, you can add the following to your click method:
myListBox.SelectedItem = null;
Where myListBox is the name of your ListBox. This will cause the listbox to have no selected items. If you want to keep the previously selected item, you have to keep the track of previously selected item or index and set the listbox to that item using either:
myListBox.SelectedItem = previousItem;
myListBox.SelectedIndex = previousIndex;

Categories

Resources