Announcement

Collapse
No announcement yet.

Wo liegt der Fehler - Clipboard nach neuem Objekt abfragen

Collapse
This topic is closed.
X
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Wo liegt der Fehler - Clipboard nach neuem Objekt abfragen

    Hi Forum!
    Ich will, das mein Programm je nach Angaben des Timers checkt, ob ein neues Item in die Zwischenablage kommt. Dazu habe ich folgenden Code:
    Code:
    Dim WithEvents t As Timer
        Dim text As String
        Dim pic As Bitmap
        Dim list As ArrayList
        Dim count As Integer  Private Sub Form1_Load(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles MyBase.Load
     
     
            t = New Timer
     
            If My.Computer.Clipboard.ContainsText = True Then
                text = My.Computer.Clipboard.GetText
                count = 1
            ElseIf My.Computer.Clipboard.ContainsImage = True Then
     
                pic = My.Computer.Clipboard.GetImage
                count = 2
            ElseIf My.Computer.Clipboard.ContainsFileDropList = True Then
                list = New ArrayList
                list.AddRange(My.Computer.Clipboard.GetFileDropList)
                For Each item As String In list
                    ImageListBox2.Items.Add(New _
                      Global.Controls.Development.ImageListBoxItem(item, 3))
                Next
                count = 3
            Else
                count = 0
            End If
            t.Start()
     
     
        End Sub  Private Sub t_Tick(ByVal sender As Object, ByVal e As _
          System.EventArgs) Handles t.Tick
            If My.Computer.Clipboard.ContainsText = True Then
                If My.Computer.Clipboard.GetText IsNot text Then
     
                    text = My.Computer.Clipboard.GetText
                    ImageListBox2.Items.Add(New _
                      Global.Controls.Development.ImageListBoxItem(text, 2))
                End If
            ElseIf My.Computer.Clipboard.ContainsImage = True Then
                If My.Computer.Clipboard.GetImage IsNot pic Then
     
                    pic = My.Computer.Clipboard.GetImage
                    ImageListBox2.Items.Add(New _
                      Global.Controls.Development.ImageListBoxItem(pic.Height & _
                      pic.Width, 1))
                End If
            ElseIf My.Computer.Clipboard.ContainsFileDropList = True Then
                If My.Computer.Clipboard.GetFileDropList IsNot list Then
     
                    list.AddRange(My.Computer.Clipboard.GetFileDropList)
                    For Each item As String In list
                        ImageListBox2.Items.Add(New _
                          Global.Controls.Development.ImageListBoxItem(item, 3))
                    Next
                End If
            End If
     
     
        End Sub
    Jetzt Frage ich mich:
    Wo liegt der Fehler, den wenn ihr das Programm ausprobiert (ihr braucht eigentlich bloß einen ImageListBox oder eventuell ListBox) spuckt es jede Sekunde den aktuellen Inhalt aus - auch wenn ich diesen schon da habt.

    Nun frage ich euch: Wo liegt dieser blöde Fehler? Ich finde den einfach nicht!
    Wäre sehr dankbar für eine Antwort.

    Gruß
    Tobi
    Ich programmiere mit Vb 2008!

  • #2
    Hallo,

    keine Ahnung wo der Fehler liegt (ist mir auch zuviel VB.net-Code - bin C#ler . Außerdem finde ich dass der Code zu kompliziert ist. Warum die Prüfung im Load-Ereignis und dann im Timer-Event nochmals? -> Wenn schon dann lagere das in eine Methode aus.

    Ich weiß nur dass das funktioniert:
    [highlight=vbnet]
    Imports System
    Imports System.Windows.Forms

    Namespace WindowsFormsApplication1
    * * Public Partial Class Form1
    * * * * Inherits Form
    * * * *
    * * * * Private _timer As Timer
    * * * * Private _text As String
    * * * *
    * * * * Public Sub New()
    * * * * * * InitializeComponent()
    * * * * * *
    * * * * * * _timer = New Timer()
    * * * * * * AddHandler _timer.Tick, AddressOf _timer_Tick
    * * * * * * _timer.Interval = 500
    * * * * * * _timer.Start()
    * * * * End Sub
    * * * *
    * * * * Private Sub _timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
    * * * * * * If Clipboard.ContainsText() Then
    * * * * * * * * Dim text As String = Clipboard.GetText()
    * * * * * * * * If _text = text Then
    * * * * * * * * * * Exit Sub
    * * * * * * * * End If
    * * * * * * * *
    * * * * * * * * _text = text
    * * * * * * * * listBox1.Items.Add(_text)
    * * * * * * End If
    * * * * End Sub
    * * End Class
    End Namespace
    [/highlight]

    Anmerkung:
    If My.Computer.Clipboard.ContainsText = True Then
    Ist - entschuldige - Blödsinn. ContainsText kann nur True/Flase sein da es ein boolscher Wert ist. Somit ist eine Prüfung auf = True sinnlos denn die If-Bedinung springt je nach dem boolschen Wert des Ausdrucks zum Ziel. Somit reicht es wenn nur ContainsText als Bedingung angegeben wird.


    mfG Gü
    "Any fool can write code that a computer can understand. Good programmers write code that humans can understand". - Martin Fowler

    Comment


    • #3
      Hallo Tobi,

      wie wäre es mit einem anschließenden:
      My.Computer.Clipboard.Clear

      Nur weil Du das Clipboard abfragst, bleibt der Inhalt weiterhin erhalten; man kann ja aus dem Clipboard auch beliebig oft per CTRL + V beliebig raus kopieren lassen.

      Und da es nicht geleert wird, findet Dein Timer jede Sekunde weiterhin einen Inhalt.
      Verwechsel das Clipboard bitte nicht mit einem Stack.

      Vielleicht wäre es für Deinen Anwendungsfall besser, auf einen geänderten (+ nicht leeren) Inhalt zu prüfen.
      Olaf Helper

      <Blog> <Xing>
      * cogito ergo sum * errare humanum est * quote erat demonstrandum *
      Wenn ich denke, ist das ein Fehler und das beweise ich täglich

      Comment


      • #4
        Endlich!

        Vielen Dank, an euch alle, das mit dem Clear-Event ist super!
        Ich hab jetzt schon in über 5 Foren nachgefragt und ihr seid die besten!

        Vielen, vielen Dank!

        PS: Die Lösung, warum bin ich da selber nicht draufgekommen?
        Ich programmiere mit Vb 2008!

        Comment


        • #5
          Aber wenn ich das´mit einer GetFileDropList ausprobiere, stürzt das Programm ab.
          Probier's mal aus.
          Wäre nett, wenn ihr dafür auch eine Lösung hättet.

          Gruß
          Tobi
          Ich programmiere mit Vb 2008!

          Comment


          • #6
            Vielleicht wäre es für Deinen Anwendungsfall besser, auf einen geänderten (+ nicht leeren) Inhalt zu prüfen.
            DU hast recht. Aber wie mach ich das?
            Vielleicht mit WM-CLIPBOARD?
            Ich programmiere mit Vb 2008!

            Comment


            • #7
              Siehe: Wie funktioniert das WM-CLIPBOARD?

              Dieses Thema wird geschlossen -> Fortsetzung im obigen Link.


              mfG Gü
              "Any fool can write code that a computer can understand. Good programmers write code that humans can understand". - Martin Fowler

              Comment

              Working...
              X