Results 1 to 19 of 19

Thread: [RESOLVED] [2005] problem in adding item to ListView

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2006
    Location
    Iran
    Posts
    48

    Resolved [RESOLVED] [2005] problem in adding item to ListView

    hi
    I want to add item to ListView1 with below code:

    Code:
    	   Dim Row As Integer = 1
                    Dim MyItem = ListView1.Items.Add(Row)
                    With MyItem
                        .SubItems.Add("Second Item")
                    End With
    but i get error "Item cannot be added to ListView." in "Dim MyItem = ListView1.Items.Add(Row)"

    I use this code in other place of project (other forms) without any error.
    wait for your idea
    Sorry For Bad My English

  2. #2
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2005] problem in adding item to ListView

    Your code works fine if I do as below. This creates two columns called "A" and "B" with "1" and "Second Item" in the first row. Is this what you intended?

    Code:
            ListView1.View = View.Details
            ListView1.Columns.Add("A")
            ListView1.Columns.Add("B")
    
            Dim Row As Integer = 1
            Dim MyItem = ListView1.Items.Add(Row)
            With MyItem
                .SubItems.Add("Second Item")
            End With

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2006
    Location
    Iran
    Posts
    48

    Re: [2005] problem in adding item to ListView

    Bulldog thanks for your reply but don't resolve my problem with this code
    I make Columns with Edit Columns in design mode and set properties of View to Details, too
    other idea
    Sorry For Bad My English

  4. #4
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] problem in adding item to ListView

    It semms you are transfering from vb6 to .Net. You need to set the "Option explicit and option strict" properties ON in your VS. Or you need to have these two lines on top of your code
    vb Code:
    1. Option Explicit On
    2. Option Strict On
    copy them as it is shown than you will see why it doesn't work.
    vb Code:
    1. Option Explicit On
    2. Option Strict On
    3.  
    4. Public Class Form1
    5.  
    6. End Class

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2006
    Location
    Iran
    Posts
    48

    Re: [2005] problem in adding item to ListView

    It semms you are transfering from vb6 to .Net.
    no no

    copy them as it is shown than you will see why it doesn't work.
    I have this code in my form code
    other help
    Last edited by shamsoft; Jul 28th, 2007 at 04:34 PM.
    Sorry For Bad My English

  6. #6
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] problem in adding item to ListView

    If you have the "Option Explicit On" and "Option Strict On" on top of your class then the parts that are not correct in your code should be underlined. If you move the mouse on top of them then it will tell you what is wrong.
    Attached Images Attached Images  

  7. #7
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] problem in adding item to ListView

    your code should look like this:
    vb Code:
    1. Dim MyItem As New ListViewItem("I am new")
    2. MyItem = ListView1.Items.Add(MyItem)
    3. With MyItem
    4.     .SubItems.Add("Second Item")
    5. End With

  8. #8

    Thread Starter
    Member
    Join Date
    Dec 2006
    Location
    Iran
    Posts
    48

    Re: [2005] problem in adding item to ListView

    your code should look like this:

    vb Code:
    Dim MyItem As New ListViewItem("I am new")
    MyItem = ListView1.Items.Add(MyItem)
    With MyItem
    .SubItems.Add("Second Item")
    End With
    I useing this code but i get error again
    error: "Item cannot be added to ListView." in "MyItem = ListView1.Items.Add(MyItem)"
    Sorry For Bad My English

  9. #9
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] problem in adding item to ListView

    Can you show your code?

  10. #10

    Thread Starter
    Member
    Join Date
    Dec 2006
    Location
    Iran
    Posts
    48

    Re: [2005] problem in adding item to ListView

    yes. I attached My Code

    I attached 2 code. AllD.vb work very well but ProSearch.vb have an error
    Attached Files Attached Files
    Sorry For Bad My English

  11. #11
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] problem in adding item to ListView

    It would be better if you just post the section of the code because some people have issues with downloading staff.

  12. #12

    Thread Starter
    Member
    Join Date
    Dec 2006
    Location
    Iran
    Posts
    48

    Re: [2005] problem in adding item to ListView

    Code:
            If CheckBox1.Checked = True Then
                Select Case SeaType
                    Case "allname"
                        TSQL = "SELECT * FROM hd WHERE fname LIKE '" & LLike & TextBox3.Text & RLike & "' AND lname LIKE '" & LLike & TextBox2.Text & RLike & "'"
                    Case "status"
                        If RadioButton5.Checked = True Then TSQL = "SELECT * FROM hd WHERE status ='inact'"
                        If RadioButton6.Checked = True Then TSQL = "SELECT * FROM hd WHERE status ='act'"
                    Case "cost"
                        TSQL = "SELECT * FROM hd WHERE cost " & Opera & " '" & TextBox5.Text & "'"
                    Case "allcost"
                        TSQL = "SELECT * FROM hd WHERE allcost " & Opera & " '" & TextBox5.Text & "'"
                    Case "fname"
                        TSQL = "SELECT * FROM hd WHERE fname LIKE '" & LLike & TextBox1.Text & RLike & "'"
                    Case "lname"
                        TSQL = "SELECT * FROM hd WHERE lname LIKE '" & LLike & TextBox1.Text & RLike & "'"
                    Case "uname"
                        TSQL = "SELECT * FROM hd WHERE uname LIKE '" & LLike & TextBox1.Text & RLike & "'"
                    Case "coname"
                        TSQL = "SELECT * FROM hd WHERE coname LIKE '" & LLike & TextBox1.Text & RLike & "'"
                    Case "tel"
                        TSQL = "SELECT * FROM hd WHERE tel LIKE '" & LLike & TextBox1.Text & RLike & "'"
                    Case "mob"
                        TSQL = "SELECT * FROM hd WHERE mob LIKE '" & LLike & TextBox1.Text & RLike & "'"
                    Case "address"
                        TSQL = "SELECT * FROM hd WHERE address LIKE '" & LLike & TextBox4.Text & RLike & "'"
                    Case "email"
                        TSQL = "SELECT * FROM hd WHERE email LIKE '" & LLike & TextBox1.Text & RLike & "'"
                    Case "domain"
                        TSQL = "SELECT * FROM hd WHERE [domain] LIKE '" & LLike & TextBox1.Text & RLike & "'"
                    Case "host"
                        TSQL = "SELECT * FROM hd WHERE host LIKE '" & LLike & TextBox1.Text & RLike & "'"
                    Case "panel"
                        TSQL = "SELECT * FROM hd WHERE panel LIKE '" & LLike & TextBox1.Text & RLike & "'"
                    Case "info"
                        TSQL = "SELECT * FROM hd WHERE info LIKE '" & LLike & TextBox4.Text & RLike & "'"
                    Case "khost"
                        TSQL = "SELECT * FROM hd WHERE khost LIKE '" & LLike & ComboBox2.Text & RLike & "'"
                    Case "time"
                        TSQL = "SELECT * FROM hd WHERE time LIKE '" & LLike & Numeric.Value & RLike & "' AND KDuration='" & Duration & "'"
                    Case "bdate"
                        TSQL = "SELECT * FROM hd WHERE bdate LIKE '" & LLike & MTextBox1.Text & RLike & "'"
                    Case "edate"
                        TSQL = "SELECT * FROM hd WHERE edate LIKE '" & LLike & MTextBox1.Text & RLike & "'"
                End Select
                con.Open()
                cmd.CommandText = TSQL
                cmd.Connection = con
                data = cmd.ExecuteReader
                Do While data.Read
                    If data("Panel") <> "nothing" Then
                        Pnl = data.Item("Panel")
                    Else
                        Pnl = "-----"
                    End If
                    If data("status") = "act" Then
                        Sta = "فعال"
                    ElseIf data("status") = "inact" Then
                        Sta = "غيرفعال"
                    End If
                    Dim MyItem As New ListViewItem
                    Name = data("Fname") & " " & data("Lname")
                    MyItem = ListView1.Items.Add(Row)
                    With MyItem
                        .SubItems.Add("هاست و دامين")
                        .SubItems.Add(data("uname"))
                        .SubItems.Add(Name)
                        .SubItems.Add(data("Coname"))
                        .SubItems.Add(data("tel"))
                        .SubItems.Add(data("Mob"))
                        .SubItems.Add(data("Email"))
                        .SubItems.Add(data("address"))
                        .SubItems.Add(data("domain"))
                        .SubItems.Add(data("host"))
                        .SubItems.Add(data("BandW"))
                        .SubItems.Add(data("khost"))
                        .SubItems.Add(Pnl)
                        .SubItems.Add(data("cost"))
                        .SubItems.Add(data("allcost"))
                        .SubItems.Add(data("bdate"))
                        .SubItems.Add(data("time") & " " & ComboBox4.Text)
                        .SubItems.Add(data("edate"))
                        .SubItems.Add(Sta)
                        .SubItems.Add(data("info"))
                    End With
                    Row += 1
                Loop
                con.Close()
    
            End If
    is piece of my code
    some of string is persian
    Sorry For Bad My English

  13. #13
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] problem in adding item to ListView

    Shamsoft, I thought we went through this but it seems you didn’t understand it. What is the “Row”? It is an integer! Does “ListView1.Items.Add” method except integer type parameter? No!
    So why and how come you have it there? You said that you have the “Option Strict” set to On so how come you don’t see that it is underlined?
    Attached Images Attached Images  

  14. #14
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] problem in adding item to ListView

    you need to convert the "Row" integer to string
    vb Code:
    1. MyItem = ListView1.Items.Add(CStr(Row))

  15. #15

    Thread Starter
    Member
    Join Date
    Dec 2006
    Location
    Iran
    Posts
    48

    Re: [2005] problem in adding item to ListView

    What is the “Row”? It is an integer! Does “ListView1.Items.Add” method except integer type parameter? No!
    yeah. is integer. it mean i cannot using integer value in this parameter?

    You said that you have the “Option Strict” set to On so how come you don’t see that it is underlined?
    no. I don't use Option Explicit On & Option Strict On. because i don't use this Option for my codes but they have not any error message. for example i don't use Option Explicit On & Option Strict On in AllD.vb and also using ListView. but it have not any error
    Sorry For Bad My English

  16. #16
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] problem in adding item to ListView

    Quote Originally Posted by shamsoft
    yeah. is integer. it mean i cannot using integer value in this parameter?
    No you cant. You need to take a look what types the method excepts as a parameter.


    no. I don't use Option Explicit On & Option Strict On. because i don't use this Option for my codes but they have not any error message. for example i don't use Option Explicit On & Option Strict On in AllD.vb and also using ListView. but it have not any error
    I don't know why you don't use Option Explicit On & Option Strict On in your projects but it is a good way of notifing about the errors. My guess is that you don't use it becuse you have non latin letters in your code. But you still can use it and ignore some errors while you are coding. And when you want to run the project than just comment them out.

  17. #17

    Thread Starter
    Member
    Join Date
    Dec 2006
    Location
    Iran
    Posts
    48

    Re: [2005] problem in adding item to ListView

    No you cant. You need to take a look what types the method excepts as a parameter.
    ok. thanks a lot. My problem Resolved

    I don't know why you don't use Option Explicit On & Option Strict On in your projects but it is a good way of notifing about the errors. My guess is that you don't use it becuse you have non latin letters in your code. But you still can use it and ignore some errors while you are coding. And when you want to run the project than just comment them out.
    yeah. i must be use the Option Explicit On & Option Strict On in my project. because i programming by vb6 in last (and now) and i beginner in vb.net, i don't habit to use Option Explicit On & Option Strict On in my project. but i try to using this.
    thanks to your helps again
    Sorry For Bad My English

  18. #18
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] problem in adding item to ListView

    Just a tip, you can set these options for your Visual Studion so that you don't need to have these lines on top of each class. To set it in vs you need to do:
    Click on Tools menu button and select "Options" menu item. Expend the "Projects and Solution" node and select "VB Defaults" and set the options to On at the right.

  19. #19

    Thread Starter
    Member
    Join Date
    Dec 2006
    Location
    Iran
    Posts
    48

    Re: [2005] problem in adding item to ListView

    thanks again
    Sorry For Bad My English

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