Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MouseClick event not firing 1

Status
Not open for further replies.

andegre

MIS
Oct 20, 2005
275
US
I have a form that holds a splitContainer where both of the panels contain a checkedListBox. The MouseEvent does not fire when clicking on the CheckedListBox, but it does fire when clicking on either the panels, or the splitcontainer, or the main form. (BTW - the Click event does not fire on the CheckedListBox either, if that matters)

Is there some property that I'm forgetting to set, something like "EnableRaisingEvents" or something like that?

Here's the code that creates one of the CheckedListBox[es].

Code:
            this.checkedListBox2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.checkedListBox2.FormattingEnabled = true;
            this.checkedListBox2.Location = new System.Drawing.Point(0, 0);
            this.checkedListBox2.Name = "checkedListBox2";
            this.checkedListBox2.Size = new System.Drawing.Size(194, 214);
            this.checkedListBox2.TabIndex = 0;
            this.checkedListBox2.MouseClick += new System.Windows.Forms.MouseEventHandler(this.checkedListBox2_MouseClick);
            this.checkedListBox2.SelectedIndexChanged += new System.EventHandler(this.checkedListBox2_SelectedIndexChanged);
            this.checkedListBox2.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.checkedListBox2_ItemCheck);
            this.checkedListBox2.Click += new System.EventHandler(checkedListBox2_Click);
 
Try using the Click event not the MouseClick event.

Code:
this.checkedListBox1.Click += new System.EventHandler(this.checkedListBox1_Click);
 
Does that still allow me to check to see if it's a right-mouse click? I want to show a context menu when it's right-clicked...I'll try that.
 
Select the checked-listbox control and from the Properties window click on the lightning icon at the top for a list of events, then see if the name of the method you want to handle the mouse click is in the MouseClick event.

If not, double click on the MouseClick event and a method will be generated for you - move your code to this method.

Hope it helps.
 
Ah, I'm getting more information...

Only a left-click works on the checkedListBoxes...

Also, I have a checkedListBox1_ItemCheck event as well as checkedListBox1_SelectedIndexChanged event.

Could those other 2 events be conflicting with just the right-mouse click event?

FYI - I added the checkedListBox1_SelectedIndexChanged event so that I could check/uncheck without having to click directly on the checkbox...I wanted to be able to click on the words also to do the check/uncheck.
 
Any other tips?

I've tried all suggestions in here and a right-click still does not work...left-click does though.
 
Try using the MouseDown event like this:

Code:
private void checkedlistbox1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        for (int i = 0; i < checkedListBox1.Items.Count; i++)
        {
            Rectangle rectangle = checkedlistbox1.GetItemRectangle(i);
    
            if ((e.Y >= rectangle.Y) && (e.Y < (rectangle.Y + rectangle.Height)))
            {
                checkedlistbox1.SelectedItem = checkedlistbox1.Items[i];
                break;
            }
        }
    }
}
 
Hooray!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Thought I tried that one already but apparently not.

Thanks PGO01!
 
If I may say so, it is generally the MouseUp event that is used for changing the screen. Try right clicking anywhere, and note that the popup menu doesn't show until you let go the mouse. You will also notice that left clicking a check box doesn't actually show the box as selected, until you release the mouse.

Using MouseUp is consistent, therefore, with standard UI practice.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top