-
Aug 2nd, 2021, 04:26 PM
#1
Thread Starter
Junior Member
[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.
-
Aug 2nd, 2021, 05:14 PM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 2nd, 2021, 05:20 PM
#3
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 2nd, 2021, 05:22 PM
#4
Re: VB.Net - Paste value into 2 TextBox
Why would you need GoSub???
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 2nd, 2021, 05:30 PM
#5
Thread Starter
Junior Member
Re: VB.Net - Paste value into 2 TextBox
Originally Posted by .paul.
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.
-
Aug 2nd, 2021, 06:28 PM
#6
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 2nd, 2021, 06:31 PM
#7
Re: [RESOLVED] VB.Net - Paste value into 2 TextBox
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 2nd, 2021, 07:00 PM
#8
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|