Results 1 to 8 of 8

Thread: [RESOLVED] VB.Net - Paste value into 2 TextBox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2021
    Posts
    23

    Resolved [RESOLVED] VB.Net - Paste value into 2 TextBox

    Hi

    I would like to use a button to paste a value into 2 textbox with a specific formatting.

    Ex:
    If i have copied to clipboard the value:
    Code:
    46.57180476969694, -82.05882047395006
    Or
    Code:
    46.57180476969694; -82.05882047395006
    When i would click on the button, it would paste in TextBox1: 46.5718, and in TextBox2: -82.0588.

    So i only need 4 digits after decimal.

    I've tried like below:
    Code:
      Dim strTemp As String
            Dim strParts() As String
            On Error Resume Next
            strTemp = Clipboard.GetText
            'Google Map
            strParts = Split(strTemp, ",")
            TextBox1.Text = Split(strParts(0), ".")(0) & "." & Left$(Split(strParts(0), ".")(1), 4)
            TextBox2.Text = Split(strParts(1), ".")(0) & "." & Left$(Split(strParts(1), ".")(1), 4)
            Return
    But that does not work at all.

    How can i do that? Sometime i could have as a separator "," and other time ";".

    In VB6, i used to use GoSub, but that does not work for VB.Net

    Thank you.
    Last edited by Wilder1234; Aug 2nd, 2021 at 04:37 PM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,458

    Re: VB.Net - Paste value into 2 TextBox

    Try this...

    Code:
    Dim strTemp As String = Clipboard.GetText
    Dim strParts() As String = strTemp.Split(New String() {";", ","}, StringSplitOptions.None)
    If strParts.Length = 2 Then
        TextBox1.Text = CDec(strParts(0)).ToString("f4")
        TextBox2.Text = CDec(strParts(1)).ToString("f4")
    End If

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,458

    Re: VB.Net - Paste value into 2 TextBox

    To ensure no errors, you could use Decimal.TryParse instead of CDec

    Code:
    If strParts.Length = 2 Then
        Dim d1, d2 As Decimal
        If Decimal.TryParse(strParts(0), d1) AndAlso Decimal.TryParse(strParts(1), d2) Then
            TextBox1.Text = d1.ToString("f4")
            TextBox2.Text = d2.ToString("f4")
        End If
    End If

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,458

    Re: VB.Net - Paste value into 2 TextBox

    Why would you need GoSub???

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2021
    Posts
    23

    Re: VB.Net - Paste value into 2 TextBox

    Quote Originally Posted by .paul. View Post
    To ensure no errors, you could use Decimal.TryParse instead of CDec

    Code:
    If strParts.Length = 2 Then
        Dim d1, d2 As Decimal
        If Decimal.TryParse(strParts(0), d1) AndAlso Decimal.TryParse(strParts(1), d2) Then
            TextBox1.Text = d1.ToString("f4")
            TextBox2.Text = d2.ToString("f4")
        End If
    End If
    I like that one. It works great. Thanks a lot.

    I was using GoSub in VB6 cause it was working. Probably that i could have used something better than.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,458

    Re: [RESOLVED] VB.Net - Paste value into 2 TextBox

    I wouldn't recommend using it but, Strings.Left is equal to the VB6 Left$ function.
    I also wouldn't recommend using On Error Resume Next. Defensive programming techniques are far superior to bumbling through your code after an error.
    Last point - the Split method you were using is the VB6 Legacy method. Notice the difference to the Split method i used? The version you were using has some advantages, but not the overload you were using.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,458

    Re: [RESOLVED] VB.Net - Paste value into 2 TextBox

    This is the advantageous overload i mentioned...

    https://docs.microsoft.com/en-us/dot...t?view=net-5.0

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jul 2021
    Posts
    23

    Re: [RESOLVED] VB.Net - Paste value into 2 TextBox

    Thank's for the information. Now i understand the differences.
    I will also remove the On Error Resume Next

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width