|
-
Mar 1st, 2005, 03:55 AM
#1
Thread Starter
Addicted Member
[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...
-
Mar 1st, 2005, 04:06 AM
#2
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.
-
Mar 1st, 2005, 04:11 AM
#3
Thread Starter
Addicted Member
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.
-
Mar 1st, 2005, 04:12 AM
#4
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:
Public Function AddListItem(ByVal sText As String) As Boolean
On Error Resume Next
' I add a "K" to the Key just incase sText might be a numerical value
ListView1.ListItems.Add Text:=sText, Key:="K" & sText
'Return True if the item was added
AddListItem = (Err.Number = 0)
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).
-
Mar 1st, 2005, 04:32 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|