• 0

[C#] Problem with double-click on ListBox item


Question

Hi there,

I have a ListBox with a few items and at sometimes, when the ListBox only has a couple of items, you'll see a blank area after the last item because there are no more items to fill the ListBox.

Now, I've implemented the MouseDoubleClick event to get the index of the double-clicked item in the ListBox like this:

private void listNotes_MouseDoubleClick(object sender, MouseEventArgs e) {
	int noteIndex = listNotes.IndexFromPoint(e.Location);

	// DO SOMETHING ELSE...
}

However, if I double-click on that blank area, noteIndex will have the index value of the last item on the list.

I need to ignore this behavior. Any suggestions?

21 answers to this question

Recommended Posts

  • 0

You are right about something, SelectedIndex is much simpler. I don't know why I didn't use it in the first place... However, it does not solve the problem.

Double-clicking the blank area will return the index from the last item in the ListBox. Forget for a minute about the double-clicking, because that's not probably the problem. Let's say that event is not implemented, when you single-click any item in the ListBox, that item will be selected, but if you single-click on the blank area below the last item, that last item will be selected.

Conclusion, the last item is being selected when I click on the blank area, that's what I want to avoid...

  • 0
  Nazgulled said:
You are right about something, SelectedIndex is much simpler. I don't know why I didn't use it in the first place... However, it does not solve the problem.

Double-clicking the blank area will return the index from the last item in the ListBox. Forget for a minute about the double-clicking, because that's not probably the problem. Let's say that event is not implemented, when you single-click any item in the ListBox, that item will be selected, but if you single-click on the blank area below the last item, that last item will be selected.

Conclusion, the last item is being selected when I click on the blank area, that's what I want to avoid...

If I click the blank region in a listbox the last item is not selected and the selectedIndex returns -1. If there is a item selected from a previous single click then that selected index will be returned because it is the item selected, and the double click is based on clicking the listbox.

ways around would be to clear the Selected Items before you get the selectedIndex. make a copy of them before, and then re-assign them after you get the selectedIndex from the mouseClick.

That way, no interference from other events should be a problem.

  • 0

I don't understand what you are saying.

I cleared the selected items, clicked the blank area and the last item was selected...

One thing I haven't told you and maybe that's where is the problem... I'm using the KryptonListBox from the Krypton Toolkit, not the ListBox from Windows Forms. Maybe I should post this on the Krypton forums...

Thanks for all your help :)

  • 0
  Nazgulled said:
I don't understand what you are saying.

I cleared the selected items, clicked the blank area and the last item was selected...

One thing I haven't told you and maybe that's where is the problem... I'm using the KryptonListBox from the Krypton Toolkit, not the ListBox from Windows Forms. Maybe I should post this on the Krypton forums...

Thanks for all your help :)

From what I understand, you were saying that clicking a the blank area in the Listbox selects the last item. I can't recreate this behaviour using the 2008 Windows Forms version. Whether the Krypton one adds this is something to investigate.

The main problem I understand is that the double click event works, just that other event handlers and functions are getting in the way, such as single click, ect. To rectify this the only way I can think of would be to save the state of the object before the click, then clear down all selected properties, and just look at the one from the MouseEventArgs.

Then you can copy the data back in. That way, any items selected before the double click will be ignored. Of course this is catering for a very small number of possibilities..

  • 0

I don't really know what to say...

Please take a look at the attached solution. It's a very simple C# app with just the KryptonListBox. Open it, press F5 and click on the blank area. You'll see the last item getting selected...

WindowsFormsApplication1.zipFetching info...

  • 0
  Nazgulled said:
I don't really know what to say...

Please take a look at the attached solution. It's a very simple C# app with just the KryptonListBox. Open it, press F5 and click on the blank area. You'll see the last item getting selected...

Not played with C# for a while, but wouldn't you be able to hook into the ondoubleclick event on the items themselves, thus avoiding this issue?

  • 0
  DaveLegg said:
Not played with C# for a while, but wouldn't you be able to hook into the ondoubleclick event on the items themselves, thus avoiding this issue?

I tried to do this earlier but I don't its possible in C#. Played with the attached solution as well and can't really get anywhere with it.

  • 0

Oh its an custom made listbox. I played with it and the same happens so I changed "SelectionMode" to None and it works... and it doesnt. MouseDoubleClick event doesnt work only SelectedIndexChanged. I guess the creator of the control left some things out.

  • 0

I'm not sure what exactly is going on, but I tried a simple project with a listbox, a few items and subscribed to the MouseDoubleClick event, and this worked fine;

MessageBox.Show(listBox1.IndexFromPoint(e.Location).ToString());

Which is what you were using earlier, and it works on my machine. Regardless of previously selected items, if you double click in the extra space in the listbox (the area not occupied by a list item), it successfully returns -1 :s

  • 0

The author of Krypton Toolkit answered me with this:

  "Phil Wright" said:
I think the difference in behavior is because the Krypton list box is owner drawn and your standard one is not. But you can use code to work out the answer. Compare the returned item with the display rectangle of that item to ensure it really is under the mouse...

		private void kryptonListBox1_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			int noteIndex = kryptonListBox1.IndexFromPoint(e.Location);
			if ((noteIndex >= 0) && (!kryptonListBox1.GetItemRectangle(noteIndex).Contains(e.Location)))
				noteIndex = -1;
		}

That would work for the MouseDoubleClick but I also have some buttons that perform actions on the selected listbox item. A single mouse click on the blank area also selects the last item. I also need to ignore this behavior... I've been trying a couple of things around the code above but haven't managed to do so, any ideas?

  • 0

I can override some if I created a class based on KryptonListBox, actually, already tried this but couldn't find a method that would help me with this problem, or I'm seeing how could I use that method to work around the problem...

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • No mention of the new Apple TV 8K?
    • I really hate it that I can't access the historical changes in my notes and if I accidentally delete large portions of the text which happens more often than not, sometimes, there's no recovering. There should always be a way to go backwards in time, simply because we have iCloud. And why is there no normal export capability? There should be a historical-change-back up and export capability.
    • SeaMonkey 2.53.21 by Razvan Serea The SeaMonkey project is a community effort to develop the SeaMonkey all-in-one internet application suite. Such a software suite was previously made popular by Netscape and Mozilla, and the SeaMonkey project continues to develop and deliver high-quality updates to this concept. Containing an Internet browser, email & newsgroup client with an included web feed reader, HTML editor, IRC chat and web development tools, SeaMonkey is sure to appeal to advanced users, web developers and corporate users. Under the hood, SeaMonkey uses much of the same Mozilla source code which powers such successful siblings as Firefox, Thunderbird, Camino, Sunbird and Miro. Legal backing is provided by the Mozilla Foundation. SeaMonkey 2.53.21 changelog: Unable to load JSON Bookmarks file, Open/Cancel do the same thing bug 1940204. Move replaceVars helper into menu-manager.js for cZ bug 1937379. Remove dumpObject helper from utils.js in cZ bug 1937380. Remove toOpenWindowByType helper from utils.js in cZ bug 1937382. Fix makeLogName helper to not encode twice in prefs.js in cZ bug 1937387. Remove use of escapeFileName helper and tidy up pref_mungeName helper in cZ bug 1937395. Add helper to file-utils.js for ensuring an nsIFile is returned in cZ bug 1937397. Remove unused http.js file from cZ bug 1937890. Remove unused IRC tests from static.js in cZ bug 1937896. Switch from deprecated escape/unescape in cZ bug 1938933. Tidy up use of prefBranch outside of pref-manager in cZ bug 1938935. Make use of pref fallbacks in pref-manager in cZ bug 1938937. Remove unused edit context menu from cZ bug 1939929. Use XPCOMUtils.generateQI in connection-xpcom in cZ bug 1939930. Merge menus.xul, popups.xul and scripts.xul into chatzilla.xul bug 1939958. Make use of toSOutputStream and toSInputStream helpers in DCC code in cZ bug 1939965. Stop hard-coding commandkey for reloadui in cZ bug 1939968. Use suite's FillInHTMLTooltip helper instead of having own version in cZ bug 1939969. Split custom-away from other away/back commands in cZ bug 1942655. Remove ChatZilla Homepage link from Help menu and about command in cZ bug 1942916. Re-arrange toolbar menus in cZ bug 1943783. Remove ChatZilla Homepage link from about and prefs dialogs in cZ bug 1943844. Use custom controller for userlist and tidy up some controller use in cZ bug 1945325. Make use of observes for show/hide elements in cZ bug 1945378. Don't dynamically create focus-input key element in cZ bug 1947028. Remove unused toolbar creation code in cZ bug 1947030. Remove unused updateMenus code from cZ bug 1947031. Clean up whitespaces in cZ package manifest bug 1947040. Remove unused uninstallKeys code from cZ bug 1950002. Use node.remove(), especially instead of node.parentNode.removeChild(node) in cZ bug 1951250. Remove __cz_condition from cZ bug 1951253. Remove outputWindowURL pref from cZ bug 1951256. Remove unused JS tests in cZ bug 1951297. Use includes, startsWith and endsWith instead of indexOf and substr in cZ bug 1951302. Use {} and [] instead of new Object() and new Array() in cZ bug 1951303. Fixup function naming for lint in cZ bug 1955141. Fixup method naming for lint in cZ bug 1955767. Remove old Mozilla 1.0 code from updateAppMotif in cZ bug 1955771. Use throw Components.Exception in cZ bug 1955774. Migrate output-window from HTML to XHTML to make localisation more standard in cZ bug 1955825. Merge munger.js into mungers.js in cZ bug 1956373. Remove unused tagName from mungers.js in cZ bug 1956374. Flatten directory structure in cZ bug 1956375. Tidy up about dialog page in cZ bug 1956376. Add helper to commands.js for sending CTCP commands in cZ bug 1956377. Migrate to standard menus for menu toolbar in cZ bug 1957763. Install plugin dialog broken in cZ bug 1961599. cZ change nick menu not working bug 1962112. Away status isn't reflected correctly in all channels in cZ bug 1962234. Fix dark motif userlist in cZ bug 1967072. SeaMonkey Composer adds moz-do-not-send attribute for links and images bug 1827146. Use menu_Toolbars overlay for navigatorOverlay and console bug 1945335. Remove defunct 2.53 prerelease builds from debugQA extension bug 1947043. Handling of MOZ_LANGPACK_CONTRIBUTORS in defines.inc files should be less custom bug 1951101. Control + U shortcut for underlined text is not working bug 1872514. Context menu search (with default search engine) does not work in the content area of a message compose or SM-Composer window bug 1062092. The following bugs were fixed in our branch of the Gecko source code only: on FreeBSD sqlite3 fails to link for missing math functions bug 1944954. Expand init.configure to use version_package.txt to set MOZ_PKG_VERSION bug 1952757. Change supported msvc Compilers for SeaMonkey 2.53 bug 1954176. SeaMonkey 2.53.21 contains (among other changes) the following major changes relative to SeaMonkey 2.49.5: The Bookmarks Manager has switched its name to Library, and now also includes the History list. When History is invoked, the Library will be shown with the History list selected. The extensive modifications were needed because of Mozilla Gecko platform API changes. Download Manager has been migrated to a new API. Although it looks pretty much the same as before, the search option is missing and some other minor details work differently. The previous downloads history is removed during the upgrade. The layout panel was added to the CSS Grid tools. TLS 1.3 is the default SSL version now. Support for all NPAPI plugins like Flash, Java and Silverlight has been removed. For displaying pdf files in the browser you can use pdf.js-seamonkey from Isaac Schemm. SeaMonkey now uses a new api for formatting regional data like time and date. Default is to use the application locale of the current SeaMonkey build. If you use a language pack or a different OS formatting this is usually not desired. You can change the formatting from the application locale to the regional settings locale (OS) in the preferences dialog under "Appearance". SeaMonkey 2.53.21 uses the same backend as Firefox and contains the relevant Firefox 60.8 security fixes. Download: SeaMonkey 64-bit | Portable SeaMonkey 64-bit ~60.0 MB (Freeware) Download: SeaMonkey 32-bit | Portable SeaMonkey 32-bit View: SeaMonkey Website | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • It doesn't work for the view, but the music gets me every time:  
  • Recent Achievements

    • First Post
      Uranus_enjoyer earned a badge
      First Post
    • Week One Done
      Uranus_enjoyer earned a badge
      Week One Done
    • Week One Done
      jfam earned a badge
      Week One Done
    • First Post
      survivor303 earned a badge
      First Post
    • Week One Done
      CHUNWEI earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      428
    2. 2
      +FloatingFatMan
      207
    3. 3
      snowy owl
      194
    4. 4
      ATLien_0
      191
    5. 5
      Xenon
      141
  • Tell a friend

    Love Neowin? Tell a friend!