|
-
Apr 5th, 2008, 10:03 PM
#1
Thread Starter
Hyperactive Member
[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:
Public Class WebPageInfo
Private _title As String
Private _url As String
Public Property Title() As String
Get
Return Me._title
End Get
Set(ByVal value As String)
Me._title = value
End Set
End Property
Public Property Url() As String
Get
Return Me._url
End Get
Set(ByVal value As String)
Me._url = value
End Set
End Property
Public Sub New(ByVal title As String, ByVal url As String)
Me._title = title
Me._url = url
End Sub
End Class
Okay, thats my webpageinfo class,
Vb Code:
Dim pageinfos As New System.ComponentModel.BindingList(Of WebPageInfo)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.BindingSource1.DataSource = Me.pageinfos
Me.ListBox1.DisplayMember = "Title"
Me.ListBox1.ValueMember = "Url"
Me.ListBox1.DataSource = Me.BindingSource1
End Sub
Here comes my question.
Vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.pageinfos.Add(New WebPageInfo(WebBrowser1.DocumentTitle, WebBrowser1.Url.ToString))
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:
Dim n as object
for each n in pageinfos
Dim once as integer
once=listbox1.findstring(listbox1.displaymember)
if (once=-1) then
Me.pageinfos.Add(New WebPageInfo(WebBrowser1.DocumentTitle, WebBrowser1.Url.ToString))
it could be something like that, right ?
Thanks for your help
-
Apr 5th, 2008, 10:11 PM
#2
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:
If Me.ListBox1.FindStringExact(someValue) <> ListBox.NoMatches Then 'Add the new value. End If
-
Apr 5th, 2008, 10:22 PM
#3
Thread Starter
Hyperactive Member
Re: [2005] Adding item once
what value do i have to set ?
pageinfos ?
-
Apr 5th, 2008, 10:35 PM
#4
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.
-
Apr 5th, 2008, 10:41 PM
#5
Frenzied Member
Re: [2005] Adding item once
Could we play hangman?
Is there an "S"?
-
Apr 5th, 2008, 11:13 PM
#6
Thread Starter
Hyperactive Member
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:
dim s as string=listbox1.displaymember
if listbox1.findstringexact(s) <> listbox.nomatches then
pageinfos.add(new webpageinfo(webbrowser1.DocumentTitle, webbrowser1.url.tostring)
end if
if i have to search in pageinfos i dont know how to
-
Apr 5th, 2008, 11:31 PM
#7
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:
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"?
-
Apr 5th, 2008, 11:40 PM
#8
Thread Starter
Hyperactive Member
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:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim s As String = WebBrowser1.DocumentTitle
For Each s In ListBox1.DisplayMember
If ListBox1.FindStringExact(s) <> ListBox.NoMatches Then
pageinfos.Add(New WebPageInfo(WebBrowser1.DocumentTitle, WebBrowser1.Url.ToString))
End If
Next
End Sub
-
Apr 5th, 2008, 11:44 PM
#9
Thread Starter
Hyperactive Member
Re: [2005] Adding item once
Finally!! i got it !!
vb Code:
Dim n As Integer
n = ListBox1.FindString(WebBrowser1.DocumentTitle)
If (n = -1) Then
pageinfos.Add(New WebPageInfo(WebBrowser1.DocumentTitle, WebBrowser1.Url.ToString))
End If
-
Apr 5th, 2008, 11:53 PM
#10
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:
If Me.ListBox1.FindStringExact(Me.WebBrowser1.DocumentTitle) = ListBox.NoMatches Then 'Add the new value. Me.pageInfos.Add(New WebPageInfo(Me.WebBrowser1.DocumentTitle, Me.WebBrowser1.Url.ToString())) 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.
-
Apr 5th, 2008, 11:54 PM
#11
Thread Starter
Hyperactive Member
Re: [RESOLVED] [2005] Adding item once
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|