ListBox Drag and Drop Werte mehrfach vorhanden?

  • VB.NET

Es gibt 6 Antworten in diesem Thema. Der letzte Beitrag () ist von RodFromGermany.

    ListBox Drag and Drop Werte mehrfach vorhanden?

    Hallo,
    ich bitte um Hilfe...
    leider komme ich nicht mehr weiter...

    Es geht um die Function doppelt().
    Ich würde gerne beim Laden der Form und nach dem Hinzufügen der Items per Drag'n Drop zur Listbox, prüfen, ob in der Listbox Werte mehrfach vorkommen.
    Also anz > 1...

    Vielen Dank für die Hilfe...

    Hier mein fehlerhafter Versuch:

    VB.NET-Quellcode

    1. Public Class Form1
    2. Private Sub ListBox_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox.DragEnter
    3. If e.Data.GetDataPresent(DataFormats.FileDrop) Then
    4. e.Effect = DragDropEffects.All
    5. End If
    6. End Sub
    7. Private Sub ListBox_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox.DragDrop
    8. Dim element As Integer = 0
    9. Dim j As Int32 = 0, z As Integer = 0
    10. Dim DroppedFiles As String() = e.Data.GetData(DataFormats.FileDrop)
    11. Dim DroppedFilesPath As String = DroppedFiles(j)
    12. If e.Data.GetDataPresent(DataFormats.FileDrop) Then
    13. If RadioButtonHinzufügen.Checked Then
    14. For Each s As String In DroppedFiles
    15. ListBox.Items.Add(s)
    16. Next s
    17. Else
    18. If ListBox.Items.Contains(DroppedFilesPath) Then
    19. MessageBox.Show(DroppedFilesPath & " ist mehrfach vorhanden.")
    20. End If
    21. End If
    22. End If
    23. doppelt()
    24. End Sub
    25. Private Sub ButtonLoeschen_Click(sender As Object, e As EventArgs) Handles ButtonLoeschen.Click
    26. End Sub
    27. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    28. RadioButtonHinzufügen.Checked = True
    29. doppelt()
    30. End Sub
    31. Function doppelt()
    32. Dim f As String = "", anz As Integer = 0, j As Integer = 0
    33. For j = 0 To ListBox.Items.Count - 1
    34. f = ListBox.Items.Item(j)
    35. For Each element In ListBox.Items
    36. If element.ToString.Contains(f) Then
    37. anz += 1
    38. If anz > 1 Then
    39. MessageBox.Show("Element mehrfach vorhanden: " & j.ToString)
    40. Exit For
    41. End If
    42. End If
    43. Next
    44. Next
    45. End Function
    46. End Class
    @kwon Pack Deine Daten in eine List(Of String), die Du an die ListBox bindest.
    Kommt ein neues Item hinzu, kannst Du das so testen:

    VB.NET-Quellcode

    1. If MyList.Contains(NEW_ITEM) Then
    2. ' ...
    3. End If
    Feddich.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Bevor Du weitermachst, bitte die empfohlenen VS-Einstellungen verwenden. So fehlt z.B. bei Function doppelt() der Rückgabetyp. btw: doppelt ist ein stark ausbaufähiger Methodenname.
    Dein bisherige Implementierung scheitert allein schon daran, dass Du anz (brrr, wieder eine komische Benennung) nicht zurücksetzt, wenn Du das nächste Item prüfst.
    Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „VaporiZed“, mal wieder aus Grammatikgründen.

    Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.
    Hallo RodFromGermany, Hallo VaporiZed,

    Vielen Dank für die Hilfe!!

    Entschuldigung für meine Benennung...

    Habe es mit RodFromGermanys Hilfe hinbekommen:

    (Bitte den Else-Zweig mit dem Ersetzen (Zeile 27) ignorieren, ist noch nicht fertig)

    VB.NET-Quellcode

    1. Option Strict Off
    2. Public Class Form1
    3. Dim liste As New List(Of String)
    4. Private Sub ListBox_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox.DragEnter
    5. If e.Data.GetDataPresent(DataFormats.FileDrop) Then
    6. e.Effect = DragDropEffects.All
    7. End If
    8. End Sub
    9. Private Sub ListBox_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox.DragDrop
    10. Dim element As Integer = 0
    11. Dim anzahl_element_vorhanden As Integer = 0
    12. Dim j As Int32 = 0, z As Integer = 0
    13. Dim DroppedFiles As String() = e.Data.GetData(DataFormats.FileDrop)
    14. Dim DroppedFilesPath As String = DroppedFiles(j)
    15. ListBox_einlesen()
    16. If e.Data.GetDataPresent(DataFormats.FileDrop) Then
    17. If RadioButtonHinzufügen.Checked Then
    18. For Each s As String In DroppedFiles
    19. If liste.Contains(s) Then
    20. MessageBox.Show("Das Element: " & s & " ist bereits vorhanden." & vbCrLf & "Das Programm wird beendet.")
    21. Exit Sub
    22. Else
    23. ListBox.Items.Add(s)
    24. End If
    25. Next s
    26. Else
    27. 'Elemente ersetzen
    28. 'If ListBox.Items.Contains(DroppedFilesPath) Then
    29. ' MessageBox.Show(DroppedFilesPath & " ist mehrfach vorhanden.")
    30. 'End If
    31. End If
    32. End If
    33. End Sub
    34. Private Sub ButtonLoeschen_Click(sender As Object, e As EventArgs) Handles ButtonLoeschen.Click
    35. End Sub
    36. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    37. RadioButtonHinzufügen.Checked = True
    38. ListBox_einlesen()
    39. End Sub
    40. Function ListBox_einlesen()
    41. For Each element In ListBox.Items
    42. liste.Add(element.ToString)
    43. Next
    44. End Function
    45. End Class


    Vollzitat entfernt. ~Thunderbolt

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Thunderbolt“ ()

    8| Zeile#1 geht ja gut los: Option Strict Off X/
    Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „VaporiZed“, mal wieder aus Grammatikgründen.

    Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.
    Dankeschön...

    VB.NET-Quellcode

    1. Option Strict On
    2. Public Class Form1
    3. Dim liste As New List(Of String)
    4. Private Sub ListBox_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox.DragEnter
    5. If e.Data.GetDataPresent(DataFormats.FileDrop) Then
    6. e.Effect = DragDropEffects.All
    7. End If
    8. End Sub
    9. Private Sub ListBox_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox.DragDrop
    10. Dim element As Integer = 0
    11. Dim anzahl_element_vorhanden As Integer = 0
    12. Dim j As Int32 = 0, z As Integer = 0
    13. Dim DroppedFiles As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
    14. Dim DroppedFilesPath As String = DroppedFiles(j)
    15. ListBox_einlesen()
    16. If e.Data.GetDataPresent(DataFormats.FileDrop) Then
    17. If RadioButtonHinzufügen.Checked Then
    18. For Each s As String In DroppedFiles
    19. If liste.Contains(s) Then
    20. MessageBox.Show("Das Element: " & s & " ist bereits vorhanden." & vbCrLf & "Das Programm wird beendet.")
    21. Exit Sub
    22. Else
    23. ListBox.Items.Add(s)
    24. End If
    25. Next s
    26. Else
    27. 'Elemente ersetzen
    28. 'If ListBox.Items.Contains(DroppedFilesPath) Then
    29. ' MessageBox.Show(DroppedFilesPath & " ist mehrfach vorhanden.")
    30. 'End If
    31. End If
    32. End If
    33. End Sub
    34. Private Sub ButtonLoeschen_Click(sender As Object, e As EventArgs) Handles ButtonLoeschen.Click
    35. End Sub
    36. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    37. RadioButtonHinzufügen.Checked = True
    38. ListBox_einlesen()
    39. End Sub
    40. Function ListBox_einlesen() As List(Of String)
    41. For Each element In ListBox.Items
    42. liste.Add(element.ToString)
    43. Next
    44. End Function
    45. End Class

    kwon schrieb:

    VB.NET-Quellcode

    1. For Each element In ListBox.Items
    2. Next
    Da hast Du wohl was nicht verstanden.
    Du sollst die Primärdaten in der Liste halten und diese als DataSource an die ListBox binden.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!