Results 1 to 11 of 11

Thread: [RESOLVED] [2005] Adding item once

  1. #1

    Thread Starter
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    Resolved [RESOLVED] [2005] Adding item once

    Hi to all,

    My situation is: Im working on a webbrowser project, i created a favorites toolbar, but i didnt know how to create the custom item that has a value and a displayname kinda property, so i asked in this forum, i got help so what i have so far its this :

    This its the custom class that will contain the url and the webpage name
    Vb Code:
    1. Public Class WebPageInfo
    2.  
    3.   Private _title As String
    4.     Private _url As String
    5.     Public Property Title() As String
    6.         Get
    7.             Return Me._title
    8.         End Get
    9.         Set(ByVal value As String)
    10.             Me._title = value
    11.         End Set
    12.     End Property
    13.     Public Property Url() As String
    14.         Get
    15.             Return Me._url
    16.  
    17.         End Get
    18.         Set(ByVal value As String)
    19.             Me._url = value
    20.         End Set
    21.     End Property
    22.     Public Sub New(ByVal title As String, ByVal url As String)
    23.         Me._title = title
    24.         Me._url = url
    25.     End Sub
    26. End Class
    Okay, thats my webpageinfo class,


    Vb Code:
    1. Dim pageinfos As New System.ComponentModel.BindingList(Of WebPageInfo)
    2.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3.         Me.BindingSource1.DataSource = Me.pageinfos
    4.         Me.ListBox1.DisplayMember = "Title"
    5.         Me.ListBox1.ValueMember = "Url"
    6.         Me.ListBox1.DataSource = Me.BindingSource1
    7.     End Sub

    Here comes my question.
    Vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.  Me.pageinfos.Add(New WebPageInfo(WebBrowser1.DocumentTitle, WebBrowser1.Url.ToString))
    3.     End Sub

    In this event i would like to add the new item created just ONCE but i dont know how to, i have an idea of how it could be done, can someone give me clues on how to do this ? thanks

    my idea its something like this :

    Vb Code:
    1. Dim n as object
    2. for each n in pageinfos
    3. Dim once as integer
    4. once=listbox1.findstring(listbox1.displaymember)
    5. if (once=-1) then
    6.                 Me.pageinfos.Add(New WebPageInfo(WebBrowser1.DocumentTitle, WebBrowser1.Url.ToString))
    it could be something like that, right ?

    Thanks for your help
    C# and WPF developer
    My Website:
    http://singlebits.com/

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Adding item once

    As I already posted in your other thread, ListBox.FindString will find the index of the first item that starts with the specified value. What values is the ListBox displaying? It's displaying the titles of the Web pages, so should you not be passing the title of the current Web page to FindString to determine whether it's already there?

    Also, I think FindStringExact would be a better option as you don't care whether some other, different Web page happens to start with the same title.

    To be the "most correct" you can be you should not be comparing the result to -1 either. As the documentation says, ListBox.NoMatches is returned if no matches are found:
    vb.net Code:
    1. If Me.ListBox1.FindStringExact(someValue) <> ListBox.NoMatches Then
    2.     'Add the new value.
    3. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    Re: [2005] Adding item once

    what value do i have to set ?

    pageinfos ?
    C# and WPF developer
    My Website:
    http://singlebits.com/

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Adding item once

    This is a very simple concept and I don't think you're actually engaging your brain here. Consider this. You are keeping a list of people who are in your club. The list contains the names of the people. If a person comes to you and asks to join your club, what are you going to do? Would you ask them their name and then check the list to see if that name was already there? I think it highly likely.

    Now, you've got a ListBox that is displaying Web page titles. You've now got a new Web page that you want to add. What are you going to do to see whether that Web page is already on the list?

    One last clue. If you had a list of people's names you would check the list for a person's name. If you had a list of Web pages' titles you will check the list for a ___ ____'s _____.

    That is all I'm going to say because I'm here to help people become better programmers and thinking for them does not achieve that aim.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: [2005] Adding item once

    Could we play hangman?
    Is there an "S"?

  6. #6

    Thread Starter
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    Re: [2005] Adding item once

    This is a very simple concept and I don't think you're actually engaging your brain here. Consider this. You are keeping a list of people who are in your club. The list contains the names of the people. If a person comes to you and asks to join your club, what are you going to do? Would you ask them their name and then check the list to see if that name was already there? I think it highly likely.

    Now, you've got a ListBox that is displaying Web page titles. You've now got a new Web page that you want to add. What are you going to do to see whether that Web page is already on the list?

    One last clue. If you had a list of people's names you would check the list for a person's name. If you had a list of Web pages' titles you will check the list for a i know that i have to search a webpage name in the list

    That is all I'm going to say because I'm here to help people become better programmers and thinking for them does not achieve that aim.
    I understand it in words, but i cant translate it into code..


    Lets see, I have a list that contains the webpage names thats = to pagesinfo, right ? or Listbox1.DisplayMember ?

    then i have a listbox that displays the names in the displaymember property, right ?

    so if thats so, i think i have to do this :

    jmcilhinney, im sorry if you are loosing ur patience with me, but im new in the world of programming......

    if i have to search in displaymember its :

    vb.net Code:
    1. dim s as string=listbox1.displaymember
    2. if listbox1.findstringexact(s) <> listbox.nomatches then
    3. pageinfos.add(new webpageinfo(webbrowser1.DocumentTitle, webbrowser1.url.tostring)
    4. end if

    if i have to search in pageinfos i dont know how to
    C# and WPF developer
    My Website:
    http://singlebits.com/

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Adding item once

    Look at the code that you just posted. You are creating a new WebPageInfo object like this:
    vb.net Code:
    1. new webpageinfo(webbrowser1.DocumentTitle, webbrowser1.url.tostring)
    Now, if you add that WebPageInfo to the ListBox what value will get displayed? Will it be webbrowser1.DocumentTitle perhaps? So is that not the value you should be looking for? How many times can I say "the title of the Web page"?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    Re: [2005] Adding item once

    Thanks, now i now im looking for the Webbrowser1.documenttitle, so is this the code i must use ? or theres something i have to change ?


    vb .net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim s As String = WebBrowser1.DocumentTitle
    3.         For Each s In ListBox1.DisplayMember
    4.             If ListBox1.FindStringExact(s) <> ListBox.NoMatches Then
    5.                 pageinfos.Add(New WebPageInfo(WebBrowser1.DocumentTitle, WebBrowser1.Url.ToString))
    6.             End If
    7.         Next
    8.         End Sub
    C# and WPF developer
    My Website:
    http://singlebits.com/

  9. #9

    Thread Starter
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    Re: [2005] Adding item once

    Finally!! i got it !!

    vb Code:
    1. Dim n As Integer
    2.         n = ListBox1.FindString(WebBrowser1.DocumentTitle)
    3.         If (n = -1) Then
    4.             pageinfos.Add(New WebPageInfo(WebBrowser1.DocumentTitle, WebBrowser1.Url.ToString))
    5.         End If
    C# and WPF developer
    My Website:
    http://singlebits.com/

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [RESOLVED] [2005] Adding item once

    I made the mistake of post "<>" instead of "=" earlier but it should be a simple substitution:
    vb.net Code:
    1. If Me.ListBox1.FindStringExact(Me.WebBrowser1.DocumentTitle) = ListBox.NoMatches Then
    2.     'Add the new value.
    3.     Me.pageInfos.Add(New WebPageInfo(Me.WebBrowser1.DocumentTitle, Me.WebBrowser1.Url.ToString()))
    4. End If
    Not that your code is wrong but that's more succinct and more correct. The 'n' variable is useless and,as I said, you should compare against ListBox.NoMatches rather than -1.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    Re: [RESOLVED] [2005] Adding item once

    Thanks!
    C# and WPF developer
    My Website:
    http://singlebits.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width