Results 1 to 5 of 5

Thread: [Resolved] Conditionally populate a ListView control

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Guwahati, Assam, India
    Posts
    131

    Talking [Resolved] Conditionally populate a ListView control

    Hi All,

    I am stuck to a very ridiculous problem.

    I need to populate a ListView control from a function of another form (that means the code will be written in a different form than the one in which the ListView control is) conditionally. The condition is that the entry should not be already present in the ListView control. Suppose I want to add "ABCDE" to the ListView. For that I have to first check that "ABCDE" is not already there in the ListView, then add "ABCDE" to it or do nothing otherwise. All we are talking about is only the first column of the ListView.

    This should be a damn simple problem but I don't know why I could not make it.

    Please help soon.
    Last edited by arnabbandyo; Mar 1st, 2005 at 08:59 AM. Reason: Problem solved
    I am new to VB...

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Conditionally populate a ListView control

    form2.listview ?

    you have to loop through the items to see if you have a duplicate before you add it.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Guwahati, Assam, India
    Posts
    131

    Re: Conditionally populate a ListView control

    Ha ha,

    Thanx for ur prompt reply but I am not that much new to VB.

    I was using ListView1.FindItem to check that. But this another form thing is not really working. Can anybody write the 3/4 lines of code needed to solve this problem.
    I am new to VB...

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Conditionally populate a ListView control

    Is it the fact that you need the code in a different Form that causes the problem or the fact that or that you don't want the same item added twice?

    To enforce that only one item is added just set the Key property to the same value as the Text property. Since only one item can have one particular key you will get a trappable error if you try to add another.

    You can always reach a control on another form using this syntax: FormName.ControlName, however OOP purist would tell you that that's not what you should use since it breaks the rule of encapsulation. You could (in the Form that has the treeview) write the actual code like this:
    VB Code:
    1. Public Function AddListItem(ByVal sText As String) As Boolean
    2.     On Error Resume Next
    3.     ' I add a "K" to the Key just incase sText might be a numerical value
    4.     ListView1.ListItems.Add Text:=sText, Key:="K" & sText
    5.     'Return True if the item was added
    6.     AddListItem = (Err.Number = 0)
    7. End Function
    This function can then be called from the Form in which you want to populate the ListBox. (Of course you need to rewrite the above code since you probably add more columns and such. I just provided this as an idea).

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Conditionally populate a ListView control

    oops. thinking about listbox. keep forgetting about the key. too much trouble to look it up, though. next time i will.

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