Schleife über gesamte Sub

  • VB.NET
  • .NET (FX) 4.0

Es gibt 49 Antworten in diesem Thema. Der letzte Beitrag () ist von markoh2603.

    Packe deinen Code bitte in die VB-Tags, er ist schon so chaotisch genug.

    markoh2603 schrieb:

    VB.NET-Quellcode

    1. For i = 0 To host_arraylist.Count

    Du musst bis host_arraylist.Count-1 gehen, weil du mit 0 anfängst.

    markoh2603 schrieb:

    Dim ping_result(host_arraylist.Count)

    Und auch alle deine Arrays müssen bis host_arraylist.Count-1 gehen, weil im VB der höchste Index angegeben wird.
    Danke Rod und white,

    der Fehler ist jetzt weg und ich habs auch verstanden, warum der Fehler kam. Noch ne kurze Frage, bin ich schon wieder aufm Holzweg oder beweg ich mich den richtigen Weg entlang. Ich weiß, viele Wege führen nach Rom, nur der Holzweg eben nicht ;)

    P.S. Das mit den VB-Tags werd ich das nächste mal machen.
    Die Änderung von Rod habe ich ebenfalls schon eingebaut. Die Labels müssten im Private Sub statusbox_Load erzeugt werden. Den Namen vom Label wollte ich nur ändern, falls ich diesen mal brauche für irgendwas, da sollte ich ihn doch einfacher Ansprechen können, oder nicht?
    Stimmt, brauch ich wirklich nicht.

    So habe jetzt die Labels und dachte, fertig, juhu 1. Schritt erledigt. ABER. Scheinbar bin ich doch wieder aufm Holzweg gelandet.

    VB.NET-Quellcode

    1. Option Explicit On
    2. Option Strict On
    3. Imports System.Runtime.InteropServices
    4. Imports Microsoft.Win32
    5. Imports System.IO
    6. Imports System.Diagnostics
    7. Imports System.Threading
    8. Imports System.Configuration
    9. Imports System.Collections.Specialized


    VB.NET-Quellcode

    1. Public Class planpresenter
    2. Private Sub statusbox_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3. Timer1.Enabled = True
    4. Timer1.Interval = 10000
    5. Dim host_arraylist = read_hosts()
    6. Dim host_label(host_arraylist.Count - 1) As Label
    7. For i = 0 To host_arraylist.Count - 1
    8. host_label(i) = New Label
    9. host_label(i).ForeColor = Color.Black
    10. host_label(i).Text = CStr((host_arraylist(i)))
    11. host_label(i).Location = New Point(8, 30 * i + 20)
    12. host_label(i).Size = CType(New System.Drawing.Point(100, 20), Drawing.Size)
    13. statusbox.Controls.Add(CType(host_label(i), Control))
    14. Next
    15. End Sub
    16. Private Sub Timer1_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    17. Dim host_arraylist = read_hosts()
    18. Dim ping_result(host_arraylist.Count - 1) As Boolean
    19. Dim status_label(host_arraylist.Count - 1) As Label
    20. For i = 0 To host_arraylist.Count - 1
    21. status_label(i) = New Label
    22. ping_result(i) = CBool(ping(CStr(host_arraylist(i))))
    23. If ping_result(i) = True Then
    24. status_label(i).Text = "Online"
    25. Else
    26. status_label(i).Text = "Offline"
    27. End If
    28. status_label(i).ForeColor = farbe_ändern(ping_result(i))
    29. status_label(i).Location = New Point(110, 30 * i + 20)
    30. status_label(i).Size = CType(New System.Drawing.Point(60, 20), Drawing.Size)
    31. statusbox.Controls.Remove(CType(status_label(i), Control))
    32. statusbox.Controls.Add(CType(status_label(i), Control))
    33. Next
    34. End Sub
    35. Function ping(ByVal host As String) As Boolean
    36. Dim Result As Boolean
    37. Try
    38. Result = (My.Computer.Network.Ping(host))
    39. Return Result
    40. Catch ex As Exception
    41. Result = CBool(False)
    42. Return Result
    43. End Try
    44. End Function
    45. Function farbe_ändern(ByVal result As Boolean) As System.Drawing.Color
    46. Dim Farbe As System.Drawing.Color
    47. If result = True Then
    48. Farbe = (Color.Green)
    49. Else
    50. Farbe = (Color.Red)
    51. End If
    52. Return (Farbe)
    53. End Function
    54. Function read_hosts() As ArrayList
    55. Dim host As String = CStr(My.Settings.Item("presenter"))
    56. Dim liste As New ArrayList(host.Split(CChar(";")))
    57. Return liste
    58. End Function
    59. End Class


    Die host-labels erzeuge ich in der Load, funktioniert und ändert sich ja sowieso nicht. Da der Ping im Timer aufgerufen wird und anhand des Return-Codes die farbe_ändern() Function aufgerufen wird, war ich der Meinung, dass die status-Labels auch im Timer erzeugt werden müssen. Erzeugt werden sie (auch korrekt) nur bei einer Status-Änderung ändern sie sich nicht.

    markoh2603 schrieb:

    VB.NET-Quellcode

    1. host_label(i).Size = CType(New System.Drawing.Point(100, 20), Drawing.Size)


    Wieso CType überall?

    VB.NET-Quellcode

    1. host_label(i).Size= New Drawing.Size(100,20)


    markoh2603 schrieb:

    VB.NET-Quellcode

    1. statusbox.Controls.Add(CType(host_label(i), Control))


    Wieso CType?

    VB.NET-Quellcode

    1. statusbox.Controls.Add(host_label(i))


    markoh2603 schrieb:

    VB.NET-Quellcode

    1. status_label(i) = New Label
    2. .....
    3. statusbox.Controls.Remove(CType(status_label(i), Control))
    4. statusbox.Controls.Add(CType(status_label(i), Control))


    Du entfernst nicht das alte Label, sondern das neu erzeugte (das noch gar nicht da ist). Du musst am Anfang der Schleife entfernen und DANN erst das neue Label zuweisen.

    Die Frage ist, warum willst du überhaupt Labels entfernen?
    Von wollen kann keine Rede sein, ich hatte es zuerst ohne den Remove, da sich aber da das Label ebenfalls nicht änderte, dachte ich, ich muss es entfernen, damit es dann neu erzeugt wird. Den Ctype hatte mir VB so vorgeschlagen, evtl. aber vor einer Änderung der Dateitypen. Ich schau mir das nochmal an.
    Die Frage ist, wozu willst du sie neu erzeugen, wenn du nur ihren Text verändern willst? Du sagtest doch, die Anzahl würde beim Programmstart feststehen. Das frage ich dich schon die ganze Zeit...

    Die Frage ist natürlich, wenn ein Host wegfällt, dann stimmt deine Indizierung überhaupt nicht mehr. Wenn Host 5 wegfällt, ist dann ehem. Host 6 der Host 5. Und schon hast du falsches Label verfärbt. Irgendwie steige ich bei deinen ganzen Labels nicht durch ;(
    Es fällt kein Host weg. Wenn in den AppSettings 5 hosts stehen, dann geht es um 5 hosts. Bei diesen 5 hosts soll der Erreichbarkeitsstatus immer wieder überprüft werden. Ändert sich der Erreichbarkeitsstatus bei einem der 5 hosts, soll sich der Text von Online auf Offline oder umgekehrt ändern, sowie entsprechend des Status die Farbe von rot auf grün oder umgekehrt. Also kurzum, die Indizierung bleibt während der Programmlaufzeit immer gleich, nur Label.Text und Label.ForeColor können sich ändern. Ich hoffe so war es verständlich.
    Das hatte ich auch schonmal gemacht, das ging nur leider gar nicht.

    VB.NET-Quellcode

    1. Private Sub statusbox_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2. Timer1.Enabled = True
    3. Timer1.Interval = 10000
    4. Dim host_arraylist = read_hosts()
    5. Dim host_label(host_arraylist.Count - 1) As Label
    6. Dim status_label(host_arraylist.Count - 1) As Label
    7. For i = 0 To host_arraylist.Count - 1
    8. host_label(i) = New Label
    9. status_label(i) = New Label
    10. host_label(i).ForeColor = Color.Black
    11. host_label(i).Text = CStr((host_arraylist(i)))
    12. host_label(i).Location = New Point(8, 30 * i + 20)
    13. host_label(i).Size = New System.Drawing.Size(100, 20)
    14. status_label(i).Location = New Point(110, 30 * i + 20)
    15. status_label(i).Size = New System.Drawing.Size(60, 20)
    16. statusbox.Controls.Add(host_label(i))
    17. statusbox.Controls.Add(status_label(i))
    18. Next
    19. End Sub
    20. Private Sub Timer1_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    21. Dim host_arraylist = read_hosts()
    22. Dim ping_result(host_arraylist.Count - 1) As Boolean
    23. Dim status_label(host_arraylist.Count - 1) As Label
    24. For i = 0 To host_arraylist.Count - 1
    25. ping_result(i) = CBool(ping(CStr(host_arraylist(i))))
    26. If ping_result(i) = True Then
    27. status_label(i).Text = "Online"
    28. Else
    29. status_label(i).Text = "Offline"
    30. End If
    31. status_label(i).ForeColor = farbe_ändern(ping_result(i))
    32. Next
    33. End Sub


    status_label.Text und status_label.ForeColor muss ich doch im Timer definieren, da ich nur dort wegen der funktionen ping() und farbe_ändern() den korrekten Wert habe!?
    Falls dem nicht so ist, wie bekomme dann aus dem Timer den Return-Code von ping()?
    Was haltet ihr davon ?
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Private prog As New presenter.presenter
    2. Private hosts As String() = My.Settings.Item("presenter").ToString().Split(";"c)
    3. Private MyLabels1 As New List(Of Label)
    4. Private MyLabels2 As New List(Of Label)
    5. Private Sub statusbox_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6. 'Klassenbibliothek presenter aufrufen
    7. For i = 0 To hosts.Count - 1
    8. Dim lb As New Label
    9. With lb
    10. .Name = hosts(i)
    11. .ForeColor = Color.Black
    12. .Text = .Name
    13. .Location = New Point(8, 30 * i + 20)
    14. .Size = New Size(100, 20)
    15. End With
    16. Dim lb2 As New Label
    17. With lb2
    18. .Name = hosts(i)
    19. .Location = New Point(110, 30 * i + 20)
    20. .Size = New Size(60, 20)
    21. End With
    22. PingPongPing(hosts(i), lb2)
    23. MyLabels1.Add(lb)
    24. MyLabels2.Add(lb2)
    25. statusbox.Controls.Add(lb)
    26. statusbox.Controls.Add(lb2)
    27. Next
    28. mytimer.Start()
    29. End Sub
    30. Private Sub PingPongPing(host As String, lb As Label)
    31. Try
    32. Dim result As Boolean = CBool(prog.ping(host))
    33. If result Then
    34. lb.ForeColor = Color.Green
    35. lb.Text = "Online"
    36. End If
    37. Catch ex As Exception
    38. lb.ForeColor = Color.Red
    39. lb.Text = "Offline"
    40. End Try
    41. End Sub
    42. Private mytimer As New System.Timers.Timer(30000)
    43. Private Sub tickticktick(sender As Object, e As ThisTimerEventArgs)
    44. Dim i As Integer = 0
    45. For Each lb As Label in MyLabels2
    46. PingPongPing(hosts(i), lb)
    47. i += 1
    48. End For
    49. End Sub

    »There's no need to "teach" atheism. It's the natural result of education without indoctrination.« — Ricky Gervais
    Eben drum hab ich es gemacht, ja es ist C&P fertig, aber aufgeräumt (kein CStr() und kein, warum es auch da war CType(New Point(x, y), Size)) und es sollte funktionieren, hoffe ich.
    »There's no need to "teach" atheism. It's the natural result of education without indoctrination.« — Ricky Gervais
    Ich muss nochmal nachfragen:

    VB.NET-Quellcode

    1. Private Sub tickticktick(sender As Object, e As ThisTimerEventArgs)
    2. Dim i As Integer = 0
    3. For Each lb As Label in MyLabels2
    4. PingPongPing(hosts(i), lb)
    5. i += 1
    6. End For


    Statt End For müßte doch Next stehen oder? und ThisTimerEventArgs kennt er gar nicht. Wo wird die Sub TickTickTick überhaupt aufgerufen, wird die durch MyTimer automatisch gerufen?

    Da ich nicht C&P machen wollte, habe ich meinen Code umgearbeitet und bekomme die gleichen Fehler wie bei C&P.
    Spoiler anzeigen


    VB.NET-Quellcode

    1. Option Explicit On
    2. Option Strict On
    3. Imports System.Runtime.InteropServices
    4. Imports Microsoft.Win32
    5. Imports System.IO
    6. Imports System.Diagnostics
    7. Imports System.Threading
    8. Imports System.Configuration
    9. Imports System.Collections.Specialized
    10. Public Class planpresenter
    11. Private host_arraylist As String() = My.Settings.Item("presenter").ToString().Split(";")
    12. Private hostlabels As New List(Of Label)
    13. Private statuslabels As New List(Of Label)
    14. Private Sub statusbox_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    15. For i = 0 To host_arraylist.Count - 1
    16. Dim host_label As New Label
    17. With host_label
    18. .Text = host_arraylist(i)
    19. .ForeColor = Color.Black
    20. .Location = New Point(8, 30 * i + 20)
    21. .Size = New Size(100, 20)
    22. End With
    23. Dim status_label As New Label
    24. With status_label
    25. .Location = New Point(8, 30 * i + 20)
    26. .Size = New Size(60, 20)
    27. End With
    28. ping(host_arraylist(i), status_label)
    29. hostlabels.Add(host_label)
    30. statuslabels.Add(status_label)
    31. statusbox.Controls.Add(host_label)
    32. statusbox.Controls.Add(status_label)
    33. Next
    34. Timer1.Enabled = True
    35. Timer1.Interval = 10000
    36. End Sub
    37. Private Sub Timer1_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    38. Dim i As Integer = 0
    39. For Each status_label As Label In statuslabels
    40. ping(host_arraylist(i), status_label)
    41. i += 1
    42. Next
    43. End Sub
    44. Function ping(ByVal host As String, ByVal status_label As Label) As Boolean
    45. Try
    46. Dim Result As Boolean = My.Computer.Network.Ping(host)
    47. If Result = True Then
    48. status_label.ForeColor = Color.Green
    49. status_label.Text = "Online"
    50. End If
    51. Catch ex As Exception
    52. status_label.ForeColor = Color.Red
    53. status_label.Text = "Offline"
    54. End Try
    55. End Function




    Ich habe meinen Code jetzt entsprechend angepasst, dank der vielen guten Ratschläge. Habe jetzt auch keine Convertierungen mehr drin, die gar nicht nötig gewesen wären, wenn man es gleich richtig gemacht hätte. Ich spare mir jetzt 2 Funktionen (farbe_ändern() sowie read_hosts() ). Durch die Globale Deklaration des Arrays mit den hostnamen, brauch ich nicht dauernd die Funktion read_hosts aufrufen, das macht das ganze wirklich deutlich übersichtlicher und das ganze noch in einem feinen 1-Zeiler.
    Allerdings habe ich jetzt das Problem, dass die host_labels alle schön erstellt werden, die status-labels aber fehlen und ich mir gerade nen Wolf suche, warum das so ist.
    Mach ich in die Function ping eine

    VB.NET-Quellcode

    1. MsgBox(host & "online")
    bzw.

    VB.NET-Quellcode

    1. MsgBox(host & "offline")
    . Werden die Nachrichten ordentlich angezeigt.