none
how to put the cursor at the begining of the TextBox content vb.net? RRS feed

  • Question

  • Hi....

    I am using this code in my clipboard manager app by predusing new textbox each time user copies a text

      Public Function AddNewTextBox() As System.Windows.Forms.TextBox
            Dim RFont As New Font("Ariel", 10, FontStyle.Bold)
            Dim txt As New System.Windows.Forms.TextBox()
            For Each tb As TextBox In Panel1.Controls
                tb.Top += 100
            Next
            Me.Panel1.Controls.Add(txt)
            txt.Top = 0
            txt.Left = 20
            txt.BackColor = Color.PeachPuff
            txt.Multiline = True
            txt.TabStop = False
            txt.Font = RFont
            txt.BorderStyle = BorderStyle.Fixed3D
            txt.Size = New Point(450, 100)
            cLeft += 1
            Return txt
        End Function
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Static previousText As String
            Dim currentText As String
            If Clipboard.ContainsText Then
                currentText = Clipboard.GetText
                If currentText <> previousText Then
                    previousText = currentText
                    AddNewTextBox().SelectedText = currentText
                End If
            End If
        End Sub

    but I want to know how to put the cursor at the beginning of the TextBox content  each time user copies a text?

    If anyone Knows the solution will be that appreciated


    • Edited by Max45-1 Saturday, August 4, 2018 1:31 AM
    Saturday, August 4, 2018 1:30 AM

Answers

  • Have you tried some TextBox.Select(0,0) ?



    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Where exactly should I put it?

    Hi

    Here is your code, amended to remove a serious error and, rearranged a little. This version passes the new String to the Function and is set as the new TextBox Text (no selected text shown)

    Is this anything like your requested information?

    ' Form1 with Panel1
    ' and Timer1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Clipboard.Clear()
        With Timer1
          .Interval = 500
          .Enabled = True
        End With
      End Sub
      Public Function AddNewTextBox(t As String) As TextBox
        Dim RFont As New Font("Ariel", 10, FontStyle.Bold)
        Dim txt As New TextBox
        For Each tb As TextBox In Panel1.Controls
          tb.Top += 100
        Next
        Panel1.Controls.Add(txt)
        txt.Top = 0
        txt.Left = 20
        txt.BackColor = Color.PeachPuff
        txt.Multiline = True
        txt.TabStop = False
        txt.Font = RFont
        txt.BorderStyle = BorderStyle.Fixed3D
    
        ' Exception thrown here'
        ' txt.Size = New Point(450, 100)
    
        txt.Size = New Size(450, 100)
        txt.Text = t
        ' don't know what this is
        ' cLeft += 1 
    
        ' added this
        txt.ScrollBars = ScrollBars.Vertical
        Return txt
      End Function
      Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As EventArgs) Handles Timer1.Tick
        Static previousText As String
        Dim currentText As String
        If Clipboard.ContainsText Then
          currentText = Clipboard.GetText
          If currentText <> previousText Then
            previousText = currentText
    
            ' seems you do nothing with
            ' the returned TextBox here.
            Dim ntb As TextBox = AddNewTextBox(currentText)
            ' NOTE: passed 'currentText'
            ' to function.
          End If
        End If
      End Sub
    End Class
    



    Regards Les, Livingston, Scotland


    • Edited by leshay Saturday, August 4, 2018 12:45 PM aDDED Vert Scroll Bars
    • Marked as answer by Max45-1 Sunday, August 5, 2018 12:58 AM
    • Unmarked as answer by Max45-1 Sunday, August 5, 2018 12:58 AM
    • Marked as answer by Max45-1 Monday, August 6, 2018 12:42 AM
    Saturday, August 4, 2018 12:38 PM

All replies

  • Have you tried someTextBox.Select(0,0) ?

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, August 4, 2018 1:41 AM
    Moderator
  • Have you tried some TextBox.Select(0,0) ?


    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Where exactly should I put it?

    Saturday, August 4, 2018 11:55 AM
  • Have you tried some TextBox.Select(0,0) ?



    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Where exactly should I put it?

    Hi

    Here is your code, amended to remove a serious error and, rearranged a little. This version passes the new String to the Function and is set as the new TextBox Text (no selected text shown)

    Is this anything like your requested information?

    ' Form1 with Panel1
    ' and Timer1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Clipboard.Clear()
        With Timer1
          .Interval = 500
          .Enabled = True
        End With
      End Sub
      Public Function AddNewTextBox(t As String) As TextBox
        Dim RFont As New Font("Ariel", 10, FontStyle.Bold)
        Dim txt As New TextBox
        For Each tb As TextBox In Panel1.Controls
          tb.Top += 100
        Next
        Panel1.Controls.Add(txt)
        txt.Top = 0
        txt.Left = 20
        txt.BackColor = Color.PeachPuff
        txt.Multiline = True
        txt.TabStop = False
        txt.Font = RFont
        txt.BorderStyle = BorderStyle.Fixed3D
    
        ' Exception thrown here'
        ' txt.Size = New Point(450, 100)
    
        txt.Size = New Size(450, 100)
        txt.Text = t
        ' don't know what this is
        ' cLeft += 1 
    
        ' added this
        txt.ScrollBars = ScrollBars.Vertical
        Return txt
      End Function
      Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As EventArgs) Handles Timer1.Tick
        Static previousText As String
        Dim currentText As String
        If Clipboard.ContainsText Then
          currentText = Clipboard.GetText
          If currentText <> previousText Then
            previousText = currentText
    
            ' seems you do nothing with
            ' the returned TextBox here.
            Dim ntb As TextBox = AddNewTextBox(currentText)
            ' NOTE: passed 'currentText'
            ' to function.
          End If
        End If
      End Sub
    End Class
    



    Regards Les, Livingston, Scotland


    • Edited by leshay Saturday, August 4, 2018 12:45 PM aDDED Vert Scroll Bars
    • Marked as answer by Max45-1 Sunday, August 5, 2018 12:58 AM
    • Unmarked as answer by Max45-1 Sunday, August 5, 2018 12:58 AM
    • Marked as answer by Max45-1 Monday, August 6, 2018 12:42 AM
    Saturday, August 4, 2018 12:38 PM
  • Great, it works. But should I remove this or it is necessary to keep the code working?.

     Clipboard.Clear()
    Because I don't want to mess with user's clipboard


    • Edited by Max45-1 Sunday, August 5, 2018 1:01 AM
    Sunday, August 5, 2018 1:01 AM
  • Great, it works. But should I remove this or it is necessary to keep the code working?.

     Clipboard.Clear()
    Because I don't want to mess with user's clipboard


    Hi

    That is entirely up to you. If you do not clear it, when your application finds some text in the Clipboard, it will activate a new TextBox.


    Regards Les, Livingston, Scotland

    Sunday, August 5, 2018 1:42 PM
  • Great, it works. But should I remove this or it is necessary to keep the code working?.

     Clipboard.Clear()
    Because I don't want to mess with user's clipboard


    Hi

    That is entirely up to you. If you do not clear it, when your application finds some text in the Clipboard, it will activate a new TextBox.


    Regards Les, Livingston, Scotland

    Hi

    No need to that because the code in Timer1 prevent it from happening without need to clear clipboard.

    Anyway thanks a lot for help that is appreciated. But last question on this: how to use mouse wheel to scroll up and down inside textboxes and on the panel?

    Thanks a lot again

    Monday, August 6, 2018 12:42 AM
  • Great, it works. But should I remove this or it is necessary to keep the code working?.

     Clipboard.Clear()
    Because I don't want to mess with user's clipboard


    Hi

    That is entirely up to you. If you do not clear it, when your application finds some text in the Clipboard, it will activate a new TextBox.


    Regards Les, Livingston, Scotland

    Hi

    No need to that because the code in Timer1 prevent it from happening without need to clear clipboard.

    Anyway thanks a lot for help that is appreciated. But last question on this: how to use mouse wheel to scroll up and down inside textboxes and on the panel?

    Thanks a lot again

    Hi

    Without the ClipBoard.Clear, iff you have copied some text before you run the code,then when you do run it, a new TextBox will be created and displayed.

    As far as scrolling the Panel, then try setting the AutoScroll Property to True.


    Regards Les, Livingston, Scotland

    • Marked as answer by Max45-1 Monday, August 6, 2018 1:10 AM
    • Unmarked as answer by Max45-1 Monday, August 6, 2018 1:10 AM
    Monday, August 6, 2018 1:05 AM
  • Great, it works. But should I remove this or it is necessary to keep the code working?.

     Clipboard.Clear()
    Because I don't want to mess with user's clipboard


    Hi

    That is entirely up to you. If you do not clear it, when your application finds some text in the Clipboard, it will activate a new TextBox.


    Regards Les, Livingston, Scotland

    Hi

    No need to that because the code in Timer1 prevent it from happening without need to clear clipboard.

    Anyway thanks a lot for help that is appreciated. But last question on this: how to use mouse wheel to scroll up and down inside textboxes and on the panel?

    Thanks a lot again

    Hi

    Without the ClipBoard.Clear, iff you have copied some text before you run the code,then when you do run it, a new TextBox will be created and displayed.

    As far as scrolling the Panel, then try setting the AutoScroll Property to True.


    Regards Les, Livingston, Scotland

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As EventArgs) Handles Timer1.Tick
            Static previousText As String
            Dim currentText As String
            If Clipboard.ContainsText Then
                currentText = Clipboard.GetText
                If currentText <> previousText Then
                    previousText = currentText
                    Dim ntb As TextBox = AddNewTextBox(currentText)
                End If
            End If
        End Sub

    I had used Clipboard.clear and I met some issues. Then I replaced it with the above code and it works.

    regard to scrolling panel1 I have already set Auto scroll property to true but it does not work....

    Monday, August 6, 2018 1:15 AM
  • Hi

    Try the code I posted as a stand alone test project, set the Panel AutoScroll Property to True and try it - both with and without the Clipboard.Clear (make sure you have copied some text prior to each run though).

    Here, the code I posted works as per your requirements, TextBox scrolling, Panel Scrolling and needs the ClipBoard.Clear as well.


    Regards Les, Livingston, Scotland

    Monday, August 6, 2018 1:22 AM
  • Hi

    Try the code I posted as a stand alone test project, set the Panel AutoScroll Property to True and try it - both with and without the Clipboard.Clear (make sure you have copied some text prior to each run though).

    Here, the code I posted works as per your requirements, TextBox scrolling, Panel Scrolling and needs the ClipBoard.Clear as well.


    Regards Les, Livingston, Scotland

    I found it

     txt.AutoScrollOffset.ToString()


    • Edited by Max45-1 Monday, August 6, 2018 2:03 AM
    Monday, August 6, 2018 2:02 AM