Click here to Skip to main content
15,908,264 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a button add and when i click it, the text in my textbox will go to my listview and when the user inter act with the button add and he click it again ,so the name will add the same name ,,

ex.
the user click the add button
then in my listview print

HOUSE
because in my textbox ,the user type HOUSE and when he user click it it will paste in the listview

and when the user click it again , i want to trap the same name(HOUSE) by yes and no
if Yes it will add into listview and no it will not..

please help
Posted
Comments
Mr.TMG 9-Aug-13 19:43pm    
Could you explain what yes and no represent? I don't fully understand the question.
Are you trying to determine if the word, house, should be allowed to be added to your list box more than one time? If so, How are you deciding? Please elaborate on "Trapping by yes and no".
Yes or no to what? Is your Yes/No the result of a message box button?
Member 10200452 9-Aug-13 20:13pm    
yes or no represent if the users input the same name
ex.
the user TYPE in texbox for example(Eduardo) and user click the add button the when user click it , Eduardo is in the listview and when the user type again Eduardo and click the add button,it will trap the users input and will show a message box, and when message box appear message show YES and No, and when the user Click YES the name(Eduardo ) will add in the listview else if the user click No it will not Add in the listview
Mr.TMG 9-Aug-13 20:38pm    
My codeing might not be exact key words as i'm doing it from memory, but intelliscence should pick up on it and make the corrections. ex DR = yes is actually something like DR = Messagebox.YESbutton

VB
'on button click
'the text being evaluated = txtbx.text

If Listbox.Items.contains(txtbx.text)then  '<Attentions that fill not work! instead, Create a function to cycle through each item in Listview and check the Listviewitem.text property for a match.


   'i think that this is what you are after-------------------------------------------------

     'create a dialog result in order to evaluate the response from the user
dim DR as DialogResult = Messagebox.show("The name already exists; Add it anyways?","Title",yesNo)

   if DR = YES then
        'Add it to the List again
    else
       'dont add it to the list
    end if

else
'the text isn't there so just add it

end if
 
Share this answer
 
v2
Comments
Member 10200452 9-Aug-13 20:41pm    
thank you for that idea Sir but?,I used Listview i didnt use listbox
and have an error listview1.Item.Contains(txtbx.text)
it have error in this AREA the txtbx.text is ('String' cannot be converted to 'System.Windows.Forms.ListViewItem'. )
I see the problem, You have to check the TEXT property of each item in the list
a simple For Each loop is working for me


VB
Public Class Form1



'txtName is the textbox
'LV is the listview


    'first we need a function with which we can compare the items in list view against the text that we wish to add


    ''' <summary>
    ''' Cycles through each item in list view to compare the text property to the TXT
    ''' </summary>
    ''' <param name="TXT">The text to check for</param>
    ''' <returns>true if matching text is found in listview</returns>
    ''' <remarks></remarks>
    Private Function LVContainsText(TXT As String) As Boolean
        'prepare a result to return from the function
        'setting it to false by default to simplify this process
        Dim result As Boolean = False

        'cycle through the list of items in listView in order to check it's properties
        'idividual of each other
        For Each I As ListViewItem In LV.Items
            'test to see it the text property matches
            If I.Text = TXT Then
                'if it does match then it's already in the list
                result = True
            End If
        Next

        'return the result
        Return result
    End Function

    'btnclick event handler
    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

        'check to see if the textbox conains anything
        If Not txtNAME.Text.Count < 1 Then

            'test to see if ListView already contains the text
            'run our function, if it returns true then the text is in the list
            If LVContainsText(txtNAME.Text) Then
                'Ask the user what to do
                Dim DR As DialogResult = MessageBox.Show("Thie list already contains that text, Add it anyways?", "OOPS?", MessageBoxButtons.YesNo)
                If DR = Windows.Forms.DialogResult.Yes Then
                    LV.Items.Add(txtNAME.Text)
                End If
            Else
                'if the text isn't in the list then add it
                LV.Items.Add(txtNAME.Text)
            End If
        End If
    End Sub


End Class
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900