Listbox contain function

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 9067 Views - Last Post: 04 February 2010 - 08:02 AM Rate Topic: -----

#1 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 385
  • Joined: 14-December 08

Listbox contain function

Post icon  Posted 31 January 2010 - 02:00 PM

Well...This is how it works right: You could check to see if there is anything your listbox contain, if your listbox contains something similar to your selected item, you can either use for loop or for each loop to display the selected items.

But the question is, I only want my codes to execute only if the listbox doesn't contain the items which are the duplicated of themselves.

For instance, my listbox contains "food", "service", "payment", three items which are initialized with string type. And now if the user selects one of them, then another listbox would copy that value, but then again, if the user selects the same item, that means he or she is selecting the duplicate, and they won't be able to do that. If they are not selecting the same item, then they could do the calculation. By checking the listbox doesn't contain a certain item, then have the codes executed, shall we do this:

for(int i = 0; i < listbox2.Items.Count; i++)
	if(!(listbox2.Items.contains(listbox1.Items[i]))

// codes execute here
// statements


Is This A Good Question/Topic? 0
  • +

Replies To: Listbox contain function

#2 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Listbox contain function

Posted 31 January 2010 - 02:07 PM

Ok, not really following you.. but as I understand, you have two listboxes. The first one contains three string items ("food", "service", "payment") and the second listbox is empty. When user selects any of the three items in first listbox, this item is copied into the second listbox. But if the second listbox already contains this item, then it is not copied again? Am I correct?
Was This Post Helpful? 0
  • +
  • -

#3 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 385
  • Joined: 14-December 08

Re: Listbox contain function

Posted 31 January 2010 - 08:47 PM

Yes...They are not allowed to select the items in the listbox one which they have previously copied to the second listbox...So my question is they can only do the calculation by clicking the calculation button only if the second listbox doesn't contain the same item as in the first listbox, where the item has been previously copied.

so will this code work?
for(int i = 0; i < listbox2.Items.Count; i++)
	if(!(listbox2.Items.contains(listbox1.Items[i]))

// codes execute here
// statements



What I wanna say is if the second listbox does not contain the duplicated item copied from the first listbox, then do the calculation, or else do nothing.
Was This Post Helpful? 1
  • +
  • -

#4 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Listbox contain function

Posted 01 February 2010 - 01:48 AM

Why don't you do something like this?

Attached Image
Was This Post Helpful? 0
  • +
  • -

#5 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 385
  • Joined: 14-December 08

Re: Listbox contain function

Posted 01 February 2010 - 07:48 AM

I don't know how and we have not learned this yet...That would be great if you could help.
Was This Post Helpful? 0
  • +
  • -

#6 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Listbox contain function

Posted 01 February 2010 - 08:05 AM

I've re read your questions about 7 times and I still don't understand what you're trying to do.

Can you describe a simple use case scenario? Use actual terms like 'In ListBox1, you have 'cat', 'dog', 'mouse'; in ListBox2, you will have...'

At this point, I don't understand what you want to do.
Was This Post Helpful? 0
  • +
  • -

#7 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Listbox contain function

Posted 01 February 2010 - 08:10 AM

I believe that he wants to calculate only those categories that are selected.
Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Listbox contain function

Posted 01 February 2010 - 08:36 AM

would it be ok to remove from ListBox1 when the value is copied from ListBox1 to ListBox2? then you don't really have to worry about checking.

for example, if you have "cat", "dog", "horse" in ListBox1, and you copy "dog" to ListBox2, ListBox1 would now only contain "cat", "horse", and ListBox2 would contain "dog".

(I believe this is what FlashM was referring to)

This post has been edited by eclipsed4utoo: 01 February 2010 - 08:37 AM

Was This Post Helpful? 0
  • +
  • -

#9 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Listbox contain function

Posted 01 February 2010 - 09:23 AM

Exactly :-) This approach would be the most cleaner and simple.

More addvanced would be to implement drag and drop mechanism.

Another one would be to have only one listbox and iterate over all the selected items in this listbox. Of course the listbox multiselect property should be set on true.

Another one would be to have a checked listbox and check the items you want to be part of calculation.
Was This Post Helpful? 0
  • +
  • -

#10 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 385
  • Joined: 14-December 08

Re: Listbox contain function

Posted 01 February 2010 - 05:45 PM

Well...I think I have found a way to resolve the problem with the use of listbox.Items.Contains built-in function.

OK! I think I will now clarify the question:

The question is like:
-You have two listboxes, and one richtextbox. The first listbox contains all the courses. What the user has to do with it is to select the courses he or she wanna take. When the user clicks on the add button, all the courses which have been selected should be copied to the listbox 2 or the second listbox...And then display the information of the listbox 2 such as the courses the user has selected, along with the cost and credit of each course. Then do the calculation, including tax and total.

I manage to get it work that the user has selected the courses he or she wants to take and then copy to the second listbox, but as for the calculation and the information displayed in the richtextbox, I get stuck. Since I cannot find any easier way to track what courses the user has selected and copy them to the second listbox...Not to mention about the calculation.
Was This Post Helpful? 0
  • +
  • -

#11 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Listbox contain function

Posted 01 February 2010 - 06:04 PM

OK, now I completely understand what you are trying to do. :)

I would save all relevant course information to an XML file. Create something similar to this:

<?xml version="1.0" encoding="utf-8" ?>
<CourseList>
  <Class name="Biology 101" id="001" cost="75">
    <StartDate>10/10/2010</StartDate>
  </Class>
  <Class name="Physics 101" id="002" cost="75">
    <StartDate>10/12/2010</StartDate>
  </Class>
  <Class name="Math" id="003" cost="75">
    <StartDate>27/04/2010</StartDate>
  </Class>
</CourseList>



That way you can easily pull the required information and associate the disaplayed listBox item with your XML elements (with their attributes, which you would use for calculations).

You could also go for the OO approach by creating a Course class with Cost, StartDate, Requirements, etc attributes and instantiate that.

The choice is ultimately yours. :)

This post has been edited by stapia.gutierrez: 01 February 2010 - 06:05 PM

Was This Post Helpful? 0
  • +
  • -

#12 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 385
  • Joined: 14-December 08

Re: Listbox contain function

Posted 01 February 2010 - 07:20 PM

Well...I are now learning the basic window application...That's window form and we are not ready for XML or other programming codes yet...

So I have to focus on window form.

The ones you provide seem to be the elements and attributes of Java or other script languages.
Was This Post Helpful? 0
  • +
  • -

#13 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Listbox contain function

Posted 02 February 2010 - 04:41 AM

View Postkenryuakuma, on 01 February 2010 - 10:20 PM, said:

Well...I are now learning the basic window application...That's window form and we are not ready for XML or other programming codes yet...

So I have to focus on window form.

The ones you provide seem to be the elements and attributes of Java or other script languages.


Where is the data at? Let's say thaT I chose "Math". Where to you get the calculation information from? Can you give that to us?
Was This Post Helpful? 0
  • +
  • -

#14 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Listbox contain function

Posted 02 February 2010 - 07:11 AM

In that case, if you don't want to save information in an XML file, just create a Course class and save it there.

If need any help with that, let us know. :)
Was This Post Helpful? 0
  • +
  • -

#15 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 385
  • Joined: 14-December 08

Re: Listbox contain function

Posted 02 February 2010 - 05:03 PM

Well...What I am working on now during these couple of quarters are really basic...We have not even learned what class is(though I know what it is). stapia.gutierrez! Your stuff is far beyond my knowledge and what you do is pretty advanced, something like a PRO will do, not novice or newbie in Visual Intro.

Well...Assuming we had two listboxes...listbox 1 is filled with courses' names, listbox 2 is where we copy what we select from listbox 1. In this case, the courses. And I think we could do another listbox for the information display(displaying the result) and the calculation. But I do this with the RichText.

As for the calculation, we have to have a TextBox that allows the user to type in the information...In this case, the amount of money paid for each credit. Within the codes, we are made or bid to create an array that holds all the credits for the courses. If, let say, we have ten courses, we need ten separate units of credits such as {10, 10, 10, 5, 7, 8, 9, 5, 9, 1}. How many credits for each course are just up to the programmer...So we could make up the credits for each course...But the cost has to be the input from the user.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2