[ListView] Item hinzufügen

  • VB.NET

Es gibt 13 Antworten in diesem Thema. Der letzte Beitrag () ist von AliveDevil.

    [ListView] Item hinzufügen

    Hallo,

    ich möchte einer ListView per TextBox und Button ein Item hinzufügen.

    Ich habe das hier versucht:

    VB.NET-Quellcode

    1. Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    2. Dim x As Integer
    3. x = ListView1.Items.Count + 1
    4. ListView1.Items.Add(x)
    5. ListView1.Items(x).SubItems.Add(TextBox3.Text)
    6. End Sub


    Fehler:

    VB.NET-Quellcode

    1. Eine Ausnahme (erste Chance) des Typs "System.ArgumentOutOfRangeException" ist in System.Windows.Forms.dll aufgetreten.



    Hab den Fehler^^
    Hi
    Der index-Parameter, der von der ListView.Items-Collection verwendet wird, ist null-basiert. Das heißt, dass das erste Item mit 0 angesprochen wird. Folglich ist das letzte Item das Item mit dem Index ListView.Items.Count - 1.
    Jetzt zum Code selber: Es wäre schöner, wenn du das so machen würdest:

    VB.NET-Quellcode

    1. With ListView1.Items.Add(New ListViewItem(New String() {"text", "subitem1text", "subitem2text", "..."}))
    2. End With

    Das With hab' ich nur so dazugenommen, damit du siehst, dass die Add-Funktion die hinzugefügte Instanz der ListViewItem-Klasse zurückgibt.

    Leider zu spät, aber ich denke, du kannst es trotzdem brauchen.

    Gruß
    ~blaze~
    Ich möchte ab einer bestimmten Stelle die Einträge speichern.

    VB.NET-Quellcode

    1. Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
    2. Dim i As Integer
    3. Dim y As Integer
    4. Dim sps(y) As String
    5. Dim l As Integer
    6. Dim b As Integer
    7. i = 10
    8. y = ListView1.Items.Count
    9. l = 0
    10. b = 0
    11. For i = 10 To y + 1
    12. sps(l) = ListView1.Items(b).SubItems(1).Text
    13. l = l + 1
    14. sps(l) = ListView1.Items(b).SubItems(2).Text
    15. l = l + 1
    16. b = b + 1
    17. File.WriteAllLines(spstxt, sps)
    18. Next


    Aber es kommt immer ein Fehler

    VB.NET-Quellcode

    1. System.ArgumentOutOfRangeException wurde nicht behandelt.
    2. Message=InvalidArgument=Value mit dem Wert 2 ist für index ungültig.
    3. Parametername: index
    4. ParamName=index
    5. Source=System.Windows.Forms
    6. StackTrace:
    7. bei System.Windows.Forms.ListViewItem.ListViewSubItemCollection.get_Item(Int32 index)
    8. bei WindowsApplication1.Form1.Button6_Click(Object sender, EventArgs e) in C:\Users\Nentiker\documents\visual studio 2010\Projects\Command Manager\Command Manager\Form1.vb:Zeile 111.
    9. bei System.Windows.Forms.Control.OnClick(EventArgs e)
    10. bei System.Windows.Forms.Button.OnClick(EventArgs e)
    11. bei System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
    12. bei System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
    13. bei System.Windows.Forms.Control.WndProc(Message& m)
    14. bei System.Windows.Forms.ButtonBase.WndProc(Message& m)
    15. bei System.Windows.Forms.Button.WndProc(Message& m)
    16. bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    17. bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    18. bei System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    19. bei System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
    20. bei System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
    21. bei System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
    22. bei System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
    23. bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
    24. bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
    25. bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
    26. bei WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:Zeile 81.
    27. bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
    28. bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    29. bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    30. bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    31. bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
    32. bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    33. bei System.Threading.ThreadHelper.ThreadStart()
    34. InnerException:


    Oder kennt jemand eine bessere Methode(bzw eine die funktioniert^^)?
    Ich hab jetzt

    VB.NET-Quellcode

    1. For i = 10 To y - 1
    2. sps(l) = ListView1.Items(b).SubItems(0).Text
    3. l = l + 1
    4. sps(l) = ListView1.Items(b).SubItems(1).Text
    5. l = l + 1
    6. b = b + 1
    7. File.WriteAllLines(spstxt, sps)
    8. Next


    Diesmal ein anderer Fehler

    VB.NET-Quellcode

    1. System.IndexOutOfRangeException wurde nicht behandelt.
    2. Message=Der Index war außerhalb des Arraybereichs.
    3. Source=Command Manager
    4. StackTrace:
    5. bei WindowsApplication1.Form1.Button6_Click(Object sender, EventArgs e) in C:\Users\Nentiker\documents\visual studio 2010\Projects\Command Manager\Command Manager\Form1.vb:Zeile 111.
    6. bei System.Windows.Forms.Control.OnClick(EventArgs e)
    7. bei System.Windows.Forms.Button.OnClick(EventArgs e)
    8. bei System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
    9. bei System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
    10. bei System.Windows.Forms.Control.WndProc(Message& m)
    11. bei System.Windows.Forms.ButtonBase.WndProc(Message& m)
    12. bei System.Windows.Forms.Button.WndProc(Message& m)
    13. bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    14. bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    15. bei System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    16. bei System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
    17. bei System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
    18. bei System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
    19. bei System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
    20. bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
    21. bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
    22. bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
    23. bei WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:Zeile 81.
    24. bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
    25. bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    26. bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    27. bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    28. bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
    29. bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    30. bei System.Threading.ThreadHelper.ThreadStart()
    31. InnerException: