5 Replies - 1041 Views - Last Post: 08 September 2011 - 02:20 AM Rate Topic: -----

#1 PNJLj  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 178
  • Joined: 18-May 09

Seeing if value in listbox exists

Posted 04 September 2011 - 03:18 AM

Oky, me again ....
So basically i have 2 forms, when the listbox on form one wants to add to the listbox on form 2 i want to see if the item being added is already in listbox on form 2. I am not using searchlite as i don't understand it so this is what i have.
LINE 4 IS THE LINE THAT IM TRYING TO IMPLEMENT.

Private Sub btnAddYourList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddYourList.Click
        If lstTitle.Text = "" Then
            MsgBox("Sorry, you have not selected anything.", vbOKOnly + vbCritical, "Nothing???")
        ElseIf lstTitle.SelectedItem(frmYourList.lstYourList.Items) Then
            MsgBox("Already In Your List", vbOKOnly, "Already Added")
        Else
            frmYourList.lstYourList.Items.Add(lstTitle.Text)
            lblCopied.Text = "Selected Item Copied"
            frmYourList.lblItemCount.Text = frmYourList.lstYourList.Items.Count & " Items In List"
            lblMainYourItems.Text = frmYourList.lstYourList.Items.Count & " Items In Your List"
        End If

    End Sub


hope you can understand what im doing. Thanks Will check back soon

PS, i know that i can send selected listbox value to textbox and check that way but didn't want to code it that way.

you wouldnot believe it, while writing the main post i re read it after i sent it and worked it out.
the code was around the wrong way. IE:

 ElseIf frmYourList.lstYourList.Items.Contains(lstTitle.Text) Then
            MsgBox("Already In Your List", vbOKOnly, "Already Added")


Thanks anyways

Is This A Good Question/Topic? 0
  • +

Replies To: Seeing if value in listbox exists

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4313
  • View blog
  • Posts: 12,090
  • Joined: 18-April 07

Re: Seeing if value in listbox exists

Posted 07 September 2011 - 03:24 PM

Just want you to know that you shouldn't use MsgBox anymore. It is MessageBox.Show(string). MsgBox was VB6 and while it works for now, it will probably not work in the future.

Glad you found your answer. :)
Was This Post Helpful? 0
  • +
  • -

#3 PNJLj  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 178
  • Joined: 18-May 09

Re: Seeing if value in listbox exists

Posted 07 September 2011 - 04:02 PM

cheers for the tip, but what is the difference
Was This Post Helpful? 0
  • +
  • -

#4 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,499
  • Joined: 16-March 11

Re: Seeing if value in listbox exists

Posted 07 September 2011 - 08:27 PM

One is not going to work in the future, and one is? On top of that you're probably not going to want to mix syntax from vb6 with vb.net if you can help it cause it'll bite you in the butt later. I happen to know from personal experience. As for your issue create a module, and add a function which returns a boolean. Now when you add to your listbox add to an array, or list which you declare on your module publicly, so that when your function checks you can just check the list for the recurrence of the item. If your function returns true, messagebox.show your message, otherwise false add that sucker to the list, and move on. :)

Public listofitems as List(Of String)

With listofitems
.add
.remove
.insert
'tons of awesome methods, and functions in here ;)/>
end with

Public listofitemsarray as arraylist = new arraylist()
'the arraylist has similar methods, and functions as the list, only the list of string is the more 'preferable one to use these days

Public Function CheckItemRecurrence(ByVal Item as string) as boolean
'start searching that listofitems, or listofitemsarray for the values you seek! :)/>
'return true or false etc
End function 




Hope my examples are helpful, and I hope what I wrote actually made sense. Good luck! Let me know if you have any other problems, or you can seek out help from someone else if ya don't like my teaching. ;)

Links:
List(Of T)
Arraylist
Arrays
Functions :D

Wrote this some days ago, came back posted, read your post again, feel like a numb skull. Glad you figured it out though lol.

This post has been edited by trevster344: 07 September 2011 - 08:29 PM

Was This Post Helpful? 0
  • +
  • -

#5 PNJLj  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 178
  • Joined: 18-May 09

Re: Seeing if value in listbox exists

Posted 08 September 2011 - 02:20 AM

KUDOS to you my friend, what you wrote sort of made sense. Although i class myself as an advanced beginer i don't quite understand it. It does make sense but for some reason its just to difficult for my lil' brain. anyhow, the way i have it works so i am going to stick to that method, although i am currently changing all my messageboxs. Thanks again for the input.
Was This Post Helpful? 0
  • +
  • -

#6 PNJLj  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 178
  • Joined: 18-May 09

Re: Seeing if value in listbox exists

Posted 08 September 2011 - 02:20 AM

KUDOS to you my friend, what you wrote sort of made sense. Although i class myself as an advanced beginer i don't quite understand it. It does make sense but for some reason its just to difficult for my lil' brain. anyhow, the way i have it works so i am going to stick to that method, although i am currently changing all my messageboxs. Thanks again for the input.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1