none
How to loop this code to continue working? RRS feed

  • Question

  • Hi all

    I've created an app it is automatic & seamless clipboard history app, that automatically saves texts user copy and list them in it. So all copied text is collected and saved for later use.Then no copying over anything important anymore

    My app contains five Labels ,each time user copy a text the copied text appear in one label.Starting from label1 to Label5.After user has copy five texts ,my app start replacing texts in the labels with the new copied texts. 

    And here the code that I use it 

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            On Error Resume Next
    
            If Me.Label1.Text = "" Then
                Me.Label1.Text = My.Computer.Clipboard.GetText
                My.Computer.Clipboard.Clear()
    
            ElseIf Me.Label1.Text <> "" And Me.Label2.Text = "" Then
                Label2.Text = My.Computer.Clipboard.GetText
                My.Computer.Clipboard.Clear()
    
            ElseIf Me.Label1.Text <> "" And Label2.Text <> "" And Me.Label3.Text = "" Then
                Me.Label3.Text = My.Computer.Clipboard.GetText
                My.Computer.Clipboard.Clear()
    
            ElseIf Me.Label1.Text <> "" And Label2.Text <> "" And Me.Label3.Text <> "" And Me.Label4.Text = "" Then
                Me.Label4.Text = My.Computer.Clipboard.GetText
                My.Computer.Clipboard.Clear()
    
            ElseIf Me.Label1.Text <> "" And Label2.Text <> "" And Me.Label3.Text <> "" And Me.Label4.Text <> "" And Me.Label5.Text = "" Then
                Me.Label5.Text = My.Computer.Clipboard.GetText
                My.Computer.Clipboard.Clear()
    
           
            End If
        End Sub

    So far this code can do the mission for the first five copy operation form Label1 to Label5. But then it fail to loop.

    The question now is how to make this a piece of code to loop and continue my app mission?

    Here's an android app from google play that make the same idea of my app

    https://play.google.com/store/apps/details?id=org.rojekti.clipper

    Thanks advance

    Saturday, June 16, 2018 1:23 AM

Answers

  • Maybe organise a stack of strings, where Label1 always contains the most recent text. For example:

       If My.Computer.Clipboard.GetText <> "" AndAlso My.Computer.Clipboard.GetText <> Label1.Text Then

          Label5.Text = Label4.Text

          Label4.Text = Label3.Text

          Label3.Text = Label2.Text

          Label2.Text = Label1.Text

          Label1.Text = My.Computer.Clipboard.GetText

       End If



    • Edited by Viorel_MVP Saturday, June 16, 2018 7:23 AM
    • Proposed as answer by Kasun Abeseela Saturday, June 16, 2018 4:02 PM
    • Marked as answer by Max45-1 Sunday, June 17, 2018 12:02 AM
    Saturday, June 16, 2018 7:23 AM

All replies

  • I've created an app it is automatic & seamless clipboard history app, that automatically saves texts user copy and list them in it. So all copied text is collected and saved for later use.Then no copying over anything important anymore.

    Which label should get replaced?

    If it's the oldest text, then you code needs to do this:

    - Move Label 2 text to Label 1.
    - Move Label 3 text to Label 2.
    - Move Label 4 text to Label 3.
    - Move Label 5 text to Label 4.
    - Delete label 5 text.
    - Starting from label 1, find the first label that has no text. Insert the clipboard text into that label.

    Saturday, June 16, 2018 1:36 AM
  • I've created an app it is automatic & seamless clipboard history app, that automatically saves texts user copy and list them in it.

    To monitor clipboard, you must use a clipboard format listener (AddClipboardFormatListener) or a clipboard viewer (SetClipboardViewer)

    (many samples on Google...)


    • Edited by Castorix31 Saturday, June 16, 2018 4:36 AM
    Saturday, June 16, 2018 4:36 AM
  • Maybe organise a stack of strings, where Label1 always contains the most recent text. For example:

       If My.Computer.Clipboard.GetText <> "" AndAlso My.Computer.Clipboard.GetText <> Label1.Text Then

          Label5.Text = Label4.Text

          Label4.Text = Label3.Text

          Label3.Text = Label2.Text

          Label2.Text = Label1.Text

          Label1.Text = My.Computer.Clipboard.GetText

       End If



    • Edited by Viorel_MVP Saturday, June 16, 2018 7:23 AM
    • Proposed as answer by Kasun Abeseela Saturday, June 16, 2018 4:02 PM
    • Marked as answer by Max45-1 Sunday, June 17, 2018 12:02 AM
    Saturday, June 16, 2018 7:23 AM
  • Maybe organise a stack of strings, where Label1 always contains the most recent text. For example:

       If My.Computer.Clipboard.GetText <> "" AndAlso My.Computer.Clipboard.GetText <> Label1.Text Then

          Label5.Text = Label4.Text

          Label4.Text = Label3.Text

          Label3.Text = Label2.Text

          Label2.Text = Label1.Text

          Label1.Text = My.Computer.Clipboard.GetText

       End If



    Thank you very much. Finally it works....

    Here one last question...

    I am using Timer1 to make my code achieve its mission as following:-

     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.Timer1.Enabled = True
        End Sub
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            On Error Resume Next
    
            If Me.Label1.Text = "" Then
                Me.Label1.Text = My.Computer.Clipboard.GetText
                My.Computer.Clipboard.Clear()
    
            ElseIf Me.Label1.Text <> "" And Me.Label2.Text = "" Then
                Label2.Text = My.Computer.Clipboard.GetText
                My.Computer.Clipboard.Clear()
    
            ElseIf Me.Label1.Text <> "" And Label2.Text <> "" And Me.Label3.Text = "" Then
                Me.Label3.Text = My.Computer.Clipboard.GetText
                My.Computer.Clipboard.Clear()
    
            ElseIf Me.Label1.Text <> "" And Label2.Text <> "" And Me.Label3.Text <> "" And Me.Label4.Text = "" Then
                Me.Label4.Text = My.Computer.Clipboard.GetText
                My.Computer.Clipboard.Clear()
    
            ElseIf Me.Label1.Text <> "" And Label2.Text <> "" And Me.Label3.Text <> "" And Me.Label4.Text <> "" And Me.Label5.Text = "" Then
                Me.Label5.Text = My.Computer.Clipboard.GetText
                My.Computer.Clipboard.Clear()
                   End If
            If My.Computer.Clipboard.GetText <> "" AndAlso My.Computer.Clipboard.GetText <> Label1.Text Then
    
                Label5.Text = Label4.Text
    
                Label4.Text = Label3.Text
    
                Label3.Text = Label2.Text
    
                Label2.Text = Label1.Text
    
                Label1.Text = My.Computer.Clipboard.GetText
            End If
    
        End Sub
    
         Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            On Error Resume Next
            Me.Timer1.Stop()
            Clipboard.SetText(Me.Label1.Text)
            SendKeys.Send("^v") ' cntrl-v to paste
        End Sub
    
        Private Sub Label1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs)
            My.Computer.Clipboard.Clear()
            Me.Timer1.Start()
    
        End Sub

    So do you advice me to keep using Timer1 or do you see that there is a butter way instead of Timer1?

    Because sometimes the app's form got stuck.

    Thanks advance anyway 

    Sunday, June 17, 2018 12:27 AM
  • I am using Timer1 to make my code achieve its mission as following:- 

    It might be working, but it's still not right.

    You need to get rid of the On Error - if there's an error (and I can't see what error you are expecting) then you want to know about it so you can fix it.

    Your complex If statements are not required.   If you move the text items before you try to add the new one, then you only need to add the new one at Label1 - no testing is needed.

    If you wish to keep the order you originally had (the most recent at the bottom) then you need to move the items up before you add the new one.  Adding the new one can be simplified if you exit when you find an empty slot. 

    If Me.Label1.Text = "" Then Me.Label1.Text = My.Computer.Clipboard.GetText My.Computer.Clipboard.Clear()
    Exit Sub
    End If
            If Me.Label2.Text = "" Then
    ...

    I would not recommend clearing the clipboard - users generally prefer that you don't mess with what they have pasted.

    You can avoid using the timer if you use the clipboard watcher:
    https://blogs.msdn.microsoft.com/codefx/2012/03/07/sample-of-mar-7th-monitor-windows-clipboard-changes-in-wpf/

    That code is for WPF but you only need a tiny bit of it, and it can be converted manually.

    If the app occasionally hangs with the timer then it is likely that the timer tick interval is too short.

    Sunday, June 17, 2018 1:01 AM
  • So do you advice me to keep using Timer1 or do you see that there is a butter way instead of Timer1?

    as it has been said, you must use AddClipboardFormatListener
    Sunday, June 17, 2018 7:32 AM