Listbox Einträge hervorheben + Listbox in ein Array übergeben

  • VB.NET

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

    Listbox Einträge hervorheben + Listbox in ein Array übergeben

    Hallo,

    ich schreibe gerade ein Programm, welches einen definierten Pfad(+ Unterverzeichnisse) nach .doc Dateien durchsucht.

    Diese werden dann Automatisch in eine Listbox eingepflegt und nach Alphabet sortiert (Ohne Array sondern mit einem Simplen befehl)

    Nun möchte ich wissen wie ich den ersten Buchstaben des Alphabet Hervorheben kann. ?(

    z.B

    Aaa
    aaa
    aaa
    Bbb
    bbb
    Ccc
    Ddd


    Danach möchte ich alle Listbox1 Einträge in ein Array schreiben, damit ich es nachher über eine Suchfunktion auslesen kann.
    (Das habe ich bereits mal Programmiert. Mir fehlt jedoch nur der übergang von "Listbox1 to Array"


    Quellcode

    1. Form2.ListBox1.Sorted = True
    2. 'Scannt nach .Doc Dateien und trägt es in die Listbox ein
    3. Dim sFile As String
    4. Dim sPath As String
    5. ' Startverzeichnis
    6. 'sPath = Application.StartupPath
    7. sPath = "Z:\Formulare HL"
    8. ' ggf. abschließenden Backslash hinzufügen
    9. If Not sPath.EndsWith("\") Then sPath += "\"
    10. ' alle TXT-Dateien im Startverzeichnis einschl. Unterordner
    11. ' in einer ListBox anzeigen
    12. For Each sFile In My.Computer.FileSystem.GetFiles(sPath, FileIO.SearchOption.SearchAllSubDirectories, "*.doc")
    13. ' Dateiname mit relativer Pfadangabe zum Startverzeichnis ausgeben
    14. Form2.ListBox1.Items.Add(sFile.Substring(sPath.Length))
    15. Next
    16. 'Zählt meine Einträge von der Listbox1
    17. Form2.Label5.Text = Form2.ListBox1.Items.Count


    __

    PS; Nein, ich bin kein VB-Neulin; Ja, ich habe Google zu diesem Thema schon seit 2 wochen in Bedienung. 8-)
    Um Hilfe wäre ich sowas von dankbar :)
    Von ListBox nach Array:

    VB.NET-Quellcode

    1. Dim array As New List(Of String)
    2. For Each sFile In My.Computer.FileSystem.GetFiles(sPath, FileIO.SearchOption.SearchAllSubDirectories, "*.doc")
    3. array.Add(sFile)
    4. Form2.ListBox1.Items.Add(sfile.Replace(sPath, ""))
    5. 'statt Form2.ListBox1.Items.Add(sFile.Substring(sPath.Length))
    6. Next

    Fertig^^
    Oder

    VB.NET-Quellcode

    1. For Each item In ListBox1.Items
    2. array.Add(sPath & "\" & item)
    3. Next
    Hey vielen Dank für alle euren tollen Hilfestellungen!
    Ich bin euch sehr sehr dankbar !
    Werde es Morgen mal ausprobieren, liege gerade krank im Bett -.- grr..

    Listbox to Array hat sehr gut nun funktioniert!


    Mit dem hervorheben was Du mir dort aber gesendet hast ist mir ein wenig zu komplex, da es Java und noch etwas anderes von M$ behandelt :D sag bitte nicht Du verstehst es :) ;) :D

    Quellcode

    1. MsgBox ("Danke sehr")
    ;) :D

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

    AliveDevil schrieb:

    Von ListBox nach Array:

    VB.NET-Quellcode

    1. Dim array As New List(Of String)
    2. For Each sFile In My.Computer.FileSystem.GetFiles(sPath, FileIO.SearchOption.SearchAllSubDirectories, "*.doc")
    3. array.Add(sFile)
    4. Form2.ListBox1.Items.Add(sfile.Replace(sPath, ""))
    5. 'statt Form2.ListBox1.Items.Add(sFile.Substring(sPath.Length))
    6. Next

    Fertig^^
    Oder

    VB.NET-Quellcode

    1. For Each item In ListBox1.Items
    2. array.Add(sPath & "\" & item)
    3. Next

    Ok Version Nr.2 hat funktionierT !!! :)
    Aber mit dem Text hervorheben in der Listbox klappt bei mir leider nicht so.
    also...da hervorheben:

    1. setze den DrawMode der ListBox auf "Ownerdraw"
    2. füge den Code ein

    VB.NET-Quellcode

    1. Private Sub listBox2_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles listBox2.DrawItem
    2. e.DrawBackground()
    3. Dim textFont As Font
    4. If e.Index Mod 2 = 0 Then
    5. textFont = New Font(e.Font.FontFamily, e.Font.Size * 2)
    6. Else
    7. textFont = e.Font
    8. End If
    9. e.Graphics.DrawString(listBox2.Items(e.Index).ToString(), textFont, New SolidBrush(e.ForeColor), RectangleF.op_Implicit(e.Bounds))
    10. e.DrawFocusRectangle()
    11. End Sub
    12. Private Sub listBox2_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles listBox2.MeasureItem
    13. If e.Index Mod 2 = 0 Then
    14. e.ItemHeight = e.ItemHeight * 2
    15. End If
    16. End Sub
    achtung! nicht getestet!
    Dann sollte das eig. funktionieren, aber wie gesagt, ich habs nicht getestet
    hmm...quick and dirty:

    VB.NET-Quellcode

    1. Dim tabelle As New List(Of Alphabet)
    2. Dim alph As String = "abcdefghijklmnopqrstuvwxyz"
    3. 'DrawItem:
    4. If Not tabelle(alph(item.Substring(0,1))).Got
    5. 'zeichne fett
    6. tabelle(alph(item.Substring(0,1))).Got = True
    7. else
    8. ' zeichne den rest
    9. 'End DrawItem
    10. Class Alphabet
    11. Private Item_ As String = ""
    12. Private Got_ As Boolean = False
    13. Sub New()
    14. End Sub
    15. Public Property Item As String
    16. Get
    17. Return Item_
    18. End Get
    19. Set(ByVal value As String)
    20. Item_ = value
    21. End Set
    22. End Get
    23. Property Got As Boolean
    24. Get
    25. return Got_
    26. End Get
    27. Set(ByVal value As Boolean)
    28. Got_ = value
    29. End Set
    30. End Property
    31. End Class

    And again not tested!