I have a simple Eto form that we have been using to learn about Eto Forms. I want to add a MouseWheel event to a ComboBox control which enabling scrolling of the SelectedIndex for this control. To do this I added MouseWheel = self.OnMouseWheel to the control and included an event handler:
def OnMouseWheel(self, sender, e):
i = 1 if e.Delta > 0 else -1
next = min(2, max(0, self.n_combobox.SelectedIndex + i))
self.n_combobox.SelectedIndex = next
This works with .NET forms. The problem with the Eto Form is that the Python compiler complains:
Message: ComboBox() takes no arguments (5 given)
since it does not recognize MouseWheel as a legitimate argument for the control.
What is the proper way to add a MouseWheel event to the control on this Eto Form?
You should call your event handler something different. OnMouseWheel() is already a virtual method on the Control class, and that is what is being called here. Hence the error message. Change it to OnMouseWheelHandler and it will start to work. You may want to ensure none of your event handlers have the name of an existing (virtual) function (see for instance OnMouseUp).
Additional note: MouseEventArgs.Delta is of type SizeF, you are looking for the Height property of it.
Thanks for the final fix. I was tearing my hair out trying to get my Microsoft Forms code converted to Eto Forms. Much of it is the same but the few small differences that popup can be killers. I do not like NumericUpDown controls so I have replaced them with Labels over which I use the mouse to change a value. I really like this much better but it is totally dependent upon being able to capture all mouse events. With yours and Dale’s help, it now looks very promising that I can port this functionality over to Eto Forms.
def OnWheel(self, sender, e):
print e.Delta.Height
i = 1 if e.Delta.Height > 0 else -1
next = min(2, max(0, self.n_combobox.SelectedIndex + i))
print 'Current index = {} next = {}'.format(self.n_combobox.SelectedIndex, next)
self.n_combobox.SelectedIndex = next
This has weird behavior. If your focus is outside combo box and you’re mousing over it and use scroll wheel - it scrolls with a proper one step thru the data, if you click inside combo box and scroll - it scrolls with a step of 2 items per wheel “click”.
Nah, it only allows scrolling thru the first and the second value like that.
SelectedIndex value changes automagically upon scroll.
I have this right now, and it’s only needed to prevent SelectedIndex to go out of bounds.
I don’t know how to make it scroll thru the values with focus not being on the combo box.
def OnWheel(self, sender, e):
next = min(len(self.exm_combobox.DataStore) - 1, max(0, self.exm_combobox.SelectedIndex))
self.exm_combobox.SelectedIndex = next