Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / April 2005

Tip: Looking for answers? Try searching our database.

Contains Method and IndexOf Method seem not work in Compact Framew

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Machi - 28 Apr 2005 08:36 GMT
May i know why the Contains Method always return false and IndexOf method
always return -1 although the value for "cboTableIDMain.Text.Trim())" is the
same?

Thanks you very much.
==================================================
ListViewItem lsvItem = new ListViewItem(cboTableIDMain.Text.Trim());
int index = lsvTableList.Items.IndexOf(lsvItem);
bool bln = lsvTableList.Items.Contains(lsvItem);
           
if (lsvTableList.Items.IndexOf(lsvItem) < 0)
{
    lsvTableList.BeginUpdate();
    lsvTableList.Items.Add(lsvItem);
    lsvTableList.EndUpdate();
}

===================================================
Christof Nordiek - 28 Apr 2005 09:42 GMT
Hi Machi,

simply calling new ListViewItem(...) doesn't put the new item to
lsvTableIDMain.
You have to call lsvTableList.Items.Add(lsv); after the first line.

> May i know why the Contains Method always return false and IndexOf method
> always return -1 although the value for "cboTableIDMain.Text.Trim())" is
[quoted text clipped - 15 lines]
>
> ===================================================
Machi - 28 Apr 2005 10:36 GMT
I do not get what you mean. Do you see the source codes properly? I
understand that new ListViewItem(...) doesn't put the new item to the
ListView control and i don't want to put the data into listview control until
i make sure the text i want to put it in the listview control is not
available in the listview control.

I want to make sure there is no duplication of same text in the listview
control.

I, for sure can use looping to check for that, but besides that, is there
any better way to do so?

Thanks.

> Hi Machi,
>
[quoted text clipped - 21 lines]
> >
> > ===================================================
Morten Wennevik - 28 Apr 2005 10:27 GMT
Hi Machi,

You are testing if a ListViewItem reference is already inside your ListView, not the contents of the ListViewItem.

Since you create a new ListViewItem it will have a new reference and will never be found. Contains/IndexOf will therefore always fail.

Try something like this

string s = cboTableIDMain.Text.Trim();
bool found = false;
foreach(ListViewItem lvi in lsvTableList.Items)
{
    if(lvi.Text == s)
    {
        found = true;
        break;
    }
}
if(!found)
    lsvTableList.Items.Add(new ListViewItem(s));

> May i know why the Contains Method always return false and IndexOf method
> always return -1 although the value for "cboTableIDMain.Text.Trim())" is the
[quoted text clipped - 14 lines]
>
> ===================================================

Signature

Happy coding!
Morten Wennevik [C# MVP]

Machi - 28 Apr 2005 10:42 GMT
Morten Wennevik, thanks for the information.

> Hi Machi,
>
[quoted text clipped - 35 lines]
> >
> > ===================================================

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.