• 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

    • AMD 25.6.1 driver out with RX 9060 XT support and a lot more FSR 4 games by Pulasthi Ariyasinghe A brand-new hardware launch is happening today for AMD, and to make sure its new GPUs are running properly, a new graphics driver has also landed right alongside it. The AMD Software: Adrenalin Edition 25.6.1 driver lands with support for the RX 9060 XT and the AMD Radeon AI PRO R9700, while also finally updating the number of games that support its AMD FidelityFX Super Resolution 4 upscaling technology. The consumer space-targeted RX 9060 XT graphics card comes in 8GB and 16GB flavors starting at $300 and $350 price points, respectively. Check out our launch coverage for this RDNA 4 GPU for more details here. At the same time, the AMD Radeon AI PRO R9700 comes in for handling professional workloads with a whopping 32GB of VRAM. While support for this card has already arrived with the latest driver, AMD is expecting to ship the product sometime in July 2025. The driver has also added official support for Onimusha 2: Samurai's Destiny Remaster as well, the Capcom-developed action game from last month. As for fixes, AMD has said that it has resolved reversed Quality and Performance selections in the Radeon Boost UI, as well as Le Mans Ultimate performance issues on RX 9070 series GPUs. There are quite a few known issues AMD is still working on: Stutter and lower than expected performance may be observed when using alt-tab and streaming to Discord with multiple monitors. Intermittent application crash or driver timeout may be observed while playing Marvel Spiderman 2 with Ray Tracing enabled on Radeon™ RX 9060 XT. Intermittent application crash may be observed when first launching The Last of Us Part 1 on Radeon™ RX 9060 XT graphics products. Stutter may be observed while playing games with some VR headsets at 80Hz or 90Hz refresh rate on some AMD Radeon™ Graphics Products such as the Radeon™ RX 7000 series. Users experiencing this issue are recommended to change the refresh rate as a temporary workaround. Intermittent system or application crash may be observed while playing Cyberpunk 2077 on some AMD Radeon™ Graphics Products such as the Radeon™ RX 7000 series. Intermittent application crash or driver timeout may be observed while playing Monster Hunter Wilds with Radeon™ Anti-Lag and Instant Replay enabled. Artifacts or corruption may appear while playing Battlefield™ V on Radeon™ RX 7000 series graphics products. Stutter may be observed while playing Call of Duty®: Warzone™ Season 03 ‘Verdansk’ map on some AMD Graphics Products. Stutter and lower than expected performance may be observed while playing 4K resolution YouTube videos in Chromium. Users experiencing this issue are recommended to play videos in full screen as a temporary workaround. Texture flickering or corruption may appear while playing The Elder Scrolls IV: Oblivion Remastered with AMD FidelityFX™ Super Resolution enabled on Radeon™ RX 9070 XT. Users experiencing this issue are recommended to disable AMD FidelityFX™ Super Resolution as a temporary workaround. As for FSR 4, these games are now supported by the popular upscaling tech for gaining more frames: Deadzone: Rogue Rem Survival F1 25 Runescape: Dragonwilds Frostpunk 2 Star Wars Outlaws Legacy: Steel & Sorcery Steel Seed Lords of the Fallen Stellar Blade Planetaries Virtua Fighter 5 R.E.V.O QANGA Wild Assault The complete list of games with FSR 4 support, as well as upcoming implementations, can be found on AMD's support page here. The WHQL-certified AMD Software: Adrenalin Edition 25.6.1 driver can now be downloaded from the AMD Software app as well as the changelog page on its official website here.
    • Download Unruly: Fighting Back when Politics, AI, and Law Upend [...] (worth $18) for free by Steven Parker Claim your complimentary eBook worth $18 for free, before the offer ends on June 17. In Unruly: Fighting Back when Politics, AI, and Law Upend the Rules of Business, co-founder of software company Hence Technologies and former Global Deputy CEO of Eurasia Group, Sean West, delivers a startlingly insightful new take on how politics, technology and law are converging to upend the rules of business, generating dangerous risks and incredible opportunities. West convincingly argues that we must understand all three factors to get leverage over the future – a future filled with eroding rule of law, deepfakes that upend elections and court decisions, government pressure for businesses to be patriotic, robot lobbyists, a flood of automated legal claims pointed directly at your company and much more. Unruly offers detailed, practical advice for how to understand the world ahead, how to be resilient in the face of innumerable and complex challenges, and how to surround your business with the people and technology you need to excel in this environment. Inside the book: A framework for understanding all of the pressures on modern corporations from the convergence of geopolitics, technology and law. Strategies for turning your company's legal department into a source of enduring competitive advantage How to navigate government pressure for nationalism when you have a global footprint Approaches to winning in a world where courts are politicized and the law is increasingly automated, built on interviews with top experts Ways to deal with the backlash to ESG at a company level Perfect for executives, managers, entrepreneurs, founders, and other business leaders, Unruly is also a must-read for general counsels and the advisors who serve them. How to get it Please ensure you read the terms and conditions to claim this offer. Complete and verifiable information is required in order to receive this free offer. If you have previously made use of these free offers, you will not need to re-register. While supplies last! Download Unruly: Fighting Back when Politics, AI, and Law Upend [...] (worth $18) for free Offered by Wiley, view other free resources The below offers are also available for free in exchange for your (work) email: VideoProc Converter AI v7.5 for FREE (worth $78.90) – Expires 6/18 Winxvideo AI V3.0 Lifetime License for PC ($69.95 Value) FREE – Expires 6/8 Aiarty Image Enhancer for PC/Mac ($85 Value) FREE – Expires 6/8 Solutions Architect's Handbook, Third Edition ($42.99 Value) FREE – Expires 6/10 AI and Innovation ($21 Value) FREE – Expires 6/11 Unruly: Fighting Back when Politics, AI, and Law Upend [...] ($18 Value) FREE - Expires 6/17 SQL Essentials For Dummies ($10 Value) FREE – Expires 6/17 Continuous Testing, Quality, Security, and Feedback ($27.99 Value) FREE – Expires 6/18 Macxvideo AI ($39.95 Value) Free for a Limited Time – Expires 6/22 The Ultimate Linux Newbie Guide – Featured Free content Python Notes for Professionals – Featured Free content Learn Linux in 5 Days – Featured Free content Quick Reference Guide for Cybersecurity – Featured Free content We post these because we earn commission on each lead so as not to rely solely on advertising, which many of our readers block. It all helps toward paying staff reporters, servers and hosting costs. Other ways to support Neowin The above deal not doing it for you, but still want to help? Check out the links below. Check out our partner software in the Neowin Store Buy a T-shirt at Neowin's Threadsquad Subscribe to Neowin - for $14 a year, or $28 a year for an ad-free experience Disclosure: An account at Neowin Deals is required to participate in any deals powered by our affiliate, StackCommerce. For a full description of StackCommerce's privacy guidelines, go here. Neowin benefits from shared revenue of each sale made through the branded deals site.
    • AMD RX 9060 XT launches above MSRP and is available to buy now by Sayan Sen At Computex 2025 this year, AMD announced its RX 9060 XT mid-range desktop GPUs. The new graphics card landed in both 8GB and 16GB flavors and targets 1080p as well as light 1440p gaming. The community and some of the media criticized the 8GB VRAM model, but AMD defended the move explaining how the smaller memory buffer is not a cause of worry for the majority. Both the 8 GB and the 16 GB RX 9060 XT are now available for purchase. A new driver is out too with Adrenalin version 25.6.1. However, as always, day one stocks would likely be highly limited, similar to other GPUs or any other product, like the Nintendo Switch 2, that also landed today. Third-party AIB (add in board) vendors like Gigabyte, for example, are selling the 8GB at $329 (SEP is $299) currently on Amazon US, so expect some markup. The technical specifications of the Radeon RX 9060 XT are given below: Specification Value GPU Architecture AMD RDNA™ 4 Core Compute Units 32 Video Memory 16 GB / 8GB GDDR6 Infinity Cache 32 MB Core Boost Clock Up to 3.13 GHz Memory speed/bandwidth 20 Gbps / 320 GB/s AI Performance 821 TOPS (INT4 with sparsity) Raytracing & AI Accelerators 32 3rd Generation Raytracing Accelerators; 64 2nd Generation AI Accelerators PCIe Interface PCIe® 5.0 x16 Display Outputs DisplayPort™ 2.1a, HDMI® 2.1b Total Board Power (TBP) 160W* If you notice, we have an asterisk for the TBP value in the table above. That is because AMD says that it can vary between 150 and 182 watts. Performance-wise, we know the $349 16 GB variant is close to the Nvidia RTX 5060 Ti in rasterization but falls behind in ray tracing. Meanwhile, the 8GB model, priced the same as the GeForce RTX 5060 at $299, should be better, as both 8 Gig and 16 Gig SKUs are identical spec-wise outside of memory capacity. As an Amazon Associate we earn from qualifying purchases.
    • It actually looks decent, although trailers could make the worst nonsense look watchable sometimes. I'm not a fan of the "extended" Aliens universe (Prometheus, Covenant), but I liked Romulus so will definitely give this a shot.
  • Recent Achievements

    • Week One Done
      jbatch earned a badge
      Week One Done
    • First Post
      Yianis earned a badge
      First Post
    • Rookie
      GTRoberts went up a rank
      Rookie
    • First Post
      James courage Tabla earned a badge
      First Post
    • Reacting Well
      James courage Tabla earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      406
    2. 2
      +FloatingFatMan
      181
    3. 3
      snowy owl
      176
    4. 4
      ATLien_0
      170
    5. 5
      Xenon
      135
  • Tell a friend

    Love Neowin? Tell a friend!