I have created an application that uses a combo box to select a user then display a database for that user. However when you scroll WITHOUT first clicking on the panel or datagridview it scrolls on the combo box there by selecting a different users database information I proceeded

this.cmbNetworkComputers.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.cmbNetworkComputers_MouseWheel);

with

void cmbNetworkComputers_MouseWheel(object sender, MouseEventArgs e)
{
    ((HandledMouseEventArgs)e).Handled = true;
}

however these disable for my entire winform instead of just the combo box cmbNetwork computers HOW do you only disable the mouse wheel for ONLY one control

有帮助吗?

解决方案

Add an event handler to your code

private void anycontrol_MouseEnter(object sender, System.EventArgs e) 
{
    var senderControl = sender as System.Windows.Forms.Control;
    if(senderControl==null)
        return;
    senderControl.Focus();
}

And assign it to any controls you want your focus to be applied automatically.

somePanel.MouseEnter += new System.EventHandler(anycontrol_MouseEnter);
//or this way
somePanel.MouseEnter += anycontrol_MouseEnter;
someComboBox.MouseEnter += anycontrol_MouseEnter;

edit:

Including the details you provided, I'd do it this way:

bool AllowUsersScrolling;
private void usersCombobox_MouseLeave(object sender, System.EventArgs e) 
{
    AllowUsersScrolling = false;
}
private void usersCombobox_MouseEnter(object sender, System.EventArgs e) 
{
    AllowUsersScrolling = true;
}
private void usersCombobox_MouseWheel(object sender, MouseEventArgs e)
{
    if(!AllowUsersScrolling)
        ((HandledMouseEventArgs)e).Handled = true;
}

And attach those handlers to your control's events respectively.

其他提示

The MouseWheel event handler is not specific to a particular control, and instead applies to the whole form. This is likely why it doesn't show up the property grid for individual controls. You can opt-out any controls that shouldn't process the mouse wheel messages by checking the sender value against particular controls. This allows other controls to use the mouse wheel.

private void cboProfile_MouseWheel(object sender, MouseEventArgs e)
{
    if (sender == cboProfile)
    {
        ((HandledMouseEventArgs)e).Handled = true;
    }
}

I had the same problem as you, but I solved it this way:

private void strSearchBox_MouseWheel(object sender, MouseEventArgs e) {
  logList.TopIndex -= e.Delta;
  ((HandledMouseEventArgs)e).Handled = true;
}

Now the combobox scrolls for the list when the combobox is active. I think this is what you want, no?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top