![]() |
|
[C#] Limiting the number of items in a List Box - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C#] Limiting the number of items in a List Box (/showthread.php?tid=5517751) |
Limiting the number of items in a List Box - Darryn - Jan 8, 2017 Hey, stupid question here. Working on an assignment and it requires use of a loop. The task is a self-service checkout application, and we were going to use the loop to limit the number of items that can be added to the cart (the listbox displays all the items you've added) to 10. Basic stuff I'm sure, but I just can't wrap my head around how I'd do it looking at the notes provided. Anyone have a good way to do so? Here's the source and the form design: http://paste.ofcode.org/3akeryXqGzFydy3NdhhC2i5 https://i.imgur.com/ZCCtWqD.png Thanks in advance!🎉 * - Magna imperium - Jan 8, 2017 Could you please post it on github? * - Darryn - Jan 8, 2017 (Jan 8, 2017 08:44 AM)Niveus~ Wrote: Could you please post it on github? I've never used gitbhub, don't even know what I'm doing lol. Making a new Repository or what? * - Owl - Jan 8, 2017 You will want to ch eck if the listbox count + quant(ity) is greater than 10 before proceeding. :) * - debug - Jan 8, 2017 on your add button, at the top of the method add this code. if(listview.items.count == 10) return; you could also replace return with a messagebox to tell the user the maximum has been reached. * - SmoothCriminal412 - Jan 8, 2017 (Jan 8, 2017 09:07 AM)Orka Wrote: You will want to ch eck if the listbox count + quant(ity) is greater than 10 before proceeding. :) If he checks if the listbox.Count is greater than 10 then that'd assume there are 11+ objects in the listbox, which should never be the case. Each time an item is added, you could just check the count of the listbox if(listbox1.items.Count == 10) { //limit reached code [do not add item] return; } //add item Also, in your particular example if the max is 10, you want to ensure that the value the user puts inside the quantity box is less than or equal to 10, so you should be checking that value too And just to add, the OOP, and perhaps the correct way to do this would be to create your own listbox class, inheriting from listbox and adding the specifics you want, probably a bit over the top * - Owl - Jan 8, 2017 (Jan 8, 2017 09:23 AM)SmoothCriminal412 Wrote: If he checks if the listbox.Count is greater than 10 then that'd assume there are 11+ objects in the listbox, which should never be the case. I must have misread Darryn's post in that 10 items represents 10 items in the list box and not item quantities. * - Darryn - Jan 8, 2017 (Jan 8, 2017 09:23 AM)SmoothCriminal412 Wrote: -snip- This works great, thanks! Unfortunately the assignment specifies that a loop needs to be incorporated too though, just thought that this would have been the best place to add it in. Any idea what kind of loop would be best to use/how I'd go about it? * - SmoothCriminal412 - Jan 8, 2017 (Jan 8, 2017 10:17 AM)Darryn Wrote: This works great, thanks! Unfortunately the assignment specifies that a loop needs to be incorporated too though, just thought that this would have been the best place to add it in. Any idea what kind of loop would be best to use/how I'd go about it? Implementing a loop to check if there are more than ten objects inside of the listbox is unnecessary as we have a property that gives us this information, but if you insist you can add this inside of the 'Add Item' event code: //adding item logic here int count = 0; foreach(var item in listBox1.Items) { count++; if(count == 10) { //cannot add anymore items return; } //add item This will also give you the same functionality you want, but is definitely not necessary. But sometimes teachers just want to see we're capable of implementing the correct logic for a scenario rather than actually doing it the most efficient way. Note though in this case the best way I could think of doing this is creating your own listbox class and inheriting from listbox and adding the functionality you want (I like this because it follows a lot of the OOP rules) but is not appropriate if you're only planning on use that listbox once, but I still prefer that way of developing, though in this case a simple Count check will suffice. Your application has no obvious loop requirements, but perhaps if you had a button that was something like 'Calculate costs' which looped through the objects in the listBox and calculated the total costs that'd work - but again keeping the costs logged as an item is added is far better. (Jan 8, 2017 08:58 AM)Darryn Wrote: I've never used gitbhub, don't even know what I'm doing lol. Making a new Repository or what? Don't think it's necessary you post this on GitHub anyway, don't worry. GitHub is a platform where developers can share code and pull/make changes. It's a place to share projects between teams, and of course to show your code to the world ;c. * - Hawk HF - Jan 8, 2017 (Jan 8, 2017 10:45 AM)SmoothCriminal412 Wrote: Even though simply checking the Count property is the best way to go (as you said), this loop can be improved. You're using a for each loop like a for loop. Code for (int i = 0; i < listBox1.Items.Count; i++)* - SmoothCriminal412 - Jan 8, 2017 This is also fine yeah, in terms of only counting the objects yeah a for is definitely better here. (Jan 8, 2017 11:05 AM)Hawk HF Wrote: Even though simply checking the Count property is the best way to go (as you said), this loop can be improved. You're using a for each loop like a for loop. Also, assuming the adding logic is below the loop the -1 is not necessary. No need to deduct one from the count Code for (int i = 0; i < listBox1.Items.Count; i++)* - Darryn - Jan 8, 2017 That's me sorted, thanks for the help guys! +3 <3 * - SmoothCriminal412 - Jan 8, 2017 (Jan 8, 2017 11:38 AM)Darryn Wrote: That's me sorted, thanks for the help guys! +3 <3 No problem, in this case because you're just checking the count a for loop is better, though if you were planning on doing something with each object then go with the foreach, as that's easier than using a for and accessing through index. Good luck * - Darryn - Jan 8, 2017 (Jan 8, 2017 11:41 AM)SmoothCriminal412 Wrote: No problem, in this case because you're just checking the count a for loop is better, though if you were planning on doing something with each object then go with the foreach, as that's easier than using a for and accessing through index. Good luck Alright, gotcha. We haven't done foreach in class yet but I'll remember when we do. Thanks again! |