copy sets of numbers into a listbox from a textbox - vba

I have a textbox that the user puts in sets of numbers(e.g. 32 45 98 56 52 1 23) and I need to copy these numbers into a listbox so that each number is its own item. So far I have this
For Each ch As Char In TextBox20.Text
If Char.IsDigit(ch) Then
ListBox1.Items.Add(ch)
End If
Next
but the problem is that it will copy each digit as an item so we will end up with
3
2
4
5
9
8
5
6
I need it to copy them like this
32
45
98
56

here is a sample of how to do that
Sub addToListBox()
Dim sample As String
Dim v As Variant
Dim i As Integer
sample = "32 45 98 56 52 1 23"
v = Split(sample, " ")
For i = 0 To UBound(v)
If IsNumeric(v(i)) Then
ListBox1.Items.Add(v(i))
End If
Next i
End Sub

Related

Reverse Number in VB.Net

I want Reverse Number: example:
Textbox1.Text = 2 14 21 22 34 44
a deployment algorithm to do this. make
Expected Output: All Combination possible Reverse.
2 41 21 22 34 44
2 14 12 22 34 44
2 14 21 22 43 44
2 14 21 22 34 44
2 41 12 22 34 44
2 41 12 22 43 44
and so on...
2 14 21 22 34 44
What I try: it works, but it does not carry all the possible combinations, as in the above model.
Dim r As Integer
Public Function Reverse(rn As Integer)
Dim value As Integer
Dim values As New List(Of String)
For Each strValue As String In TextBox1.Text.Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
If Integer.TryParse(strValue.Trim, value) Then
values.Add(value)
End If
Next
Dim numbers = Val(TextBox1.Text)
Dim result As Integer
While numbers > 0
rn = numbers Mod 10
result = result * 10 + rn
numbers = numbers \ 10
End While
Reverse = result
End Function
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox2.Text = Reverse(r & " ")
End Sub

Eliminate Duplicate from a Textbox

I have Textbox1.Lines (separated by a space)
5 7 10 12 12
7 10 12 13 14
8 10 14 15 19
12 13 14 15 19
21 23 27 29 50
51 53 55 78 80
1 1 7 14 19
2 4 7 8 10
How should I remove duplicates from each line and display something like this:
7 10 12 13 14
8 10 14 15 19
12 13 14 15 19
21 23 27 29 50
51 53 55 78 80
2 4 7 8 10
that is, to remove from the entire line that contains a duplicate number on the same line.
Private Sub BttGamblerInput_Click(sender As Object, e As EventArgs) Handles BttGamblerInput.Click
On Error Resume Next
Dim value As Integer
Dim values As New List(Of String)
For Each strValue As String In TxtGamblerImput.Text.Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
If Integer.TryParse(strValue.Trim, value) Then
values.Add(value)
End If
Not worked.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each line In TxtGamblerOutput.Text
Dim originalArray = TxtGamblerOutput.Text.Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
Dim checkArray = originalArray.Distinct().ToArray() ' I haven't used this, so check syntax etc when you code it.
If originalArray.Length = checkArray.Length Then
TxtGamblerOutput.Text = checkArray
Else ' <-- perhaps a bonus here?
' Do something with the invalid line
End If
Next line
End Sub
Code 3:
Dim numbers() As Integer = {1, 3, 5, 7, 2, 1, 5, 4, 4, 8}
' get the duplicates
Dim dups As New Dictionary(Of Integer, Integer) ' number, count of number
For i = 0 To numbers.Length - 1
If dups.ContainsKey(numbers(i)) Then
dups(numbers(i)) += 1
Else
dups.Add(numbers(i), 1)
End If
Next
' display them
For Each dup In dups
If dup.Value > 1 Then
Console.WriteLine(dup.Key)
End If
Next
Console.ReadLine()
I haven't coded or tested anything, but you can use the Distinct method from List(of T) and compare the length of the output to the current List. Another approach is to use an ArrayList (c.f. https://stackoverflow.com/a/13428210/9101981 for the bones of getting unique elements).
Pseudo-ish Code using an Array:
for each line in the text input
originalArray = line.Text.Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
checkArray = originalArray.Distinct().ToArray() ' I haven't used this, so check syntax etc when you code it.
If originalArray.Length = checkArray.Length then
' Your code here to display the valid line
Else ' <-- perhaps a bonus here?
' Do something with the invalid line
End If
Next line
Some additional reading:
https://learn.microsoft.com/en-us/dotnet/api/system.collections.arraylist?view=netframework-4.8
and, in particular, note the recommendation to use List.
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.distinct?view=netframework-4.8
noting that this is the Linq Distinct documentation

Convert big string to decimal in vb.net

Hello i have big string value which is md5 of something now i need to convert it into the decimal value
for example
Dim md5_s As String = "6F05AF42533432A5513610FE839ACC86"
now i need output same like the online converters to this
"54 70 48 53 65 70 52 50 53 51 51 52 51 50 65 53 53 49 51 54 49 48 70
69 56 51 57 65 67 67 56 54 "
is it possible i don't want spaces in the above converted decimal ?
vb.net help please
Okay here i tried and got it n is my approach works fine will this fine n work always right
Dim t As String
Dim a As String = "6F05AF42533432A5513610FE839ACC86"
For Each c As Char In a
t &= Convert.ToInt32(c)
Next
TextBox1.Text = t
will this one is right ?
result is same what i am looking for as
5470485365705250535151525150655353495154494870695651576567675654
so i assume this is right huh ?
I'm not quite sure this is what you are really looking for but this is what you asked for
For count = 0 To md5_s.Length - 1
Dim tempChar As String = md5_s.Substring(count, 1)
Console.Write(Asc(tempChar))
Next
What is more likely what you want is something like this
Private Function HexToByteArray(ByVal hex As [String]) As Byte()
Dim NumberChars As Integer = hex.Length
Dim bytes As Byte() = New Byte(NumberChars / 2 - 1) {}
For i As Integer = 0 To NumberChars - 1 Step 2
bytes(i / 2) = Convert.ToByte(hex.Substring(i, 2), 16)
Next
Return bytes
End Function
either way ... hope this helps

Generate random string in text field

We have that old software (made by one of the first employees many years ago) in company that uses Microsoft Access to run. Boss asked me to add a random string generation in the specific text box on click but i have no idea how to do that. I dont have any Microsoft Access programming experience, thats why i am askin you to help.
I managed to create button and text field so far. Thats where it stops. I also managed to access the code for the button action:
Private Sub command133_Click()
End Sub
This is one way, will work in Access VBA (which is an older basic than vb.net). It will generate a string with letters and numbers.
Sub test()
Dim s As String * 8 'fixed length string with 8 characters
Dim n As Integer
Dim ch As Integer 'the character
For n = 1 To Len(s) 'don't hardcode the length twice
Do
ch = Rnd() * 127 'This could be more efficient.
'48 is '0', 57 is '9', 65 is 'A', 90 is 'Z', 97 is 'a', 122 is 'z'.
Loop While ch < 48 Or ch > 57 And ch < 65 Or ch > 90 And ch < 97 Or ch > 122
Mid(s, n, 1) = Chr(ch) 'bit more efficient than concatenation
Next
Debug.Print s
End Sub
Try this function:
Public Function GetRandomString(ByVal iLength As Integer) As String
Dim sResult As String = ""
Dim rdm As New Random()
For i As Integer = 1 To iLength
sResult &= ChrW(rdm.Next(32, 126))
Next
Return sResult
End Function
Workin on #Bathsheba code, I did this. It will generate a random string with the number of characters you'd like.
Code :
Public Function GenerateUniqueSequence(numberOfCharacters As Integer) As String
Dim random As String ' * 8 'fixed length string with 8 characters
Dim j As Integer
Dim ch As Integer ' each character
random = ""
For j = 1 To numberOfCharacters
random = random & GenerateRandomAlphaNumericCharacter
Next
GenerateUniqueSequence = random
End Function
Public Function GenerateRandomAlphaNumericCharacter() As String
'Numbers : 48 is '0', 57 is '9'
'LETTERS : 65 is 'A', 90 is 'Z'
'letters : 97 is 'a', 122 is 'z'
GenerateRandomAlphaNumericCharacter = ""
Dim i As Integer
Randomize
i = (Rnd() * 2) + 1 'One chance out of 3 to choose one of 3 catégories
Randomize
Select Case i
Case 1 'Numbers
GenerateRandomAlphaNumericCharacter = Chr(Rnd() * 9 + 48)
Case 2 'LETTERS
GenerateRandomAlphaNumericCharacter = Chr(Rnd() * 25 + 65)
Case 3 'letters
GenerateRandomAlphaNumericCharacter = Chr(Rnd() * 25 + 97)
End Select
End Function
I use it with random number of characters, like this :
'Generates random Session ID between 15 and 30 alphanumeric characters
SessionID = GenerateUniqueSequence(Rnd * 15 + 15)
Result :
s8a8qWOmoDvC4jKRjPr5hOY12u 26
TB24qZ4cNfr6EdyY0J 18
6LZRQ9P5WHLNd71LIdqJ 20
KPN0RmlhhJKnVzPTkW 18
R2pNOKWJMKl9KpSoIV2egUNTEb1QC2 30
X8jHuupP6SvEI8Dt2wJi 20
NOTE: This is still not completely random. It will give a higher count of numbers than normal as approx 1/3 of all chars generated will be numbers.
Normally distribution will look like:
10 numbers plus 26 lowercase plus 26 uppercase = 62 possible chars.
Numbers will normally be 10/62 parts of the string or 1/6.2
With the code
i = (Rnd() * 2) + 1 'One chance out of 3 to choose one of 3 catégories
the count of numbers is pushed up to 1/3 (on average)
Probably not too much of a worry - unless you are trying to beat the NSA and then you have decreased your range significantly.

how to tokenize chart data for csv file in classic asp

Hi below is the data sample for chart data which I read from URL
ARRAY ' ' 3 8
Y
25 75 100 125 150 175 200 225
'A' 3 8 6 7 5 3 2 7
'B' 1 9 7 8 4 7 2 5
'C' 8 7 3 6 56 9 111 8
Now in this I want to save this data into a csv file as below
Time A B c
25 3 1 8
75 8 9 7
100 6 7 3
125 7 8 9
150 5 4 56
175 3 7 9
200 2 2 111
225 7 5 8
Actually I have to first read the data into an array in which each element will contain one line of the data file, Now I will have to split the each element by space and store it in a two dimensional array, now for each ith index of each array I have to create a coma separated line then put "\n" at the end of line , and at last save this to a csv file . I don't know much about the syntax of vb-script and classic asp that's why I am facing this problem . Please help me
To get you started:
Sub doReOrder(sFSpecI, sFSpecO, sCol1)
Dim oTS : Set oTS = goFS.OpenTextFile(sFSpecI)
Dim sData : sData = oTS.ReadLine() ' ARRAY ' ' 3 8
Dim aParts : aParts = Split(sData, " ")
Dim nCols : nCols = CByte(aParts(3)) ' Count vs UBound vs Data!
Dim nRows : nRows = CByte(aParts(4))
Dim nRows2 : nRows2 = nRows + 1
oTS.SkipLine ' Y
' get table in one string, prepend col1 name, clean '
sData = "'" & sCol1 & "' " & Replace(oTS.ReadAll(), vbCrLf, " ")
sData = Trim(Replace(sData, "'", """"))
' get table in flat array
aParts = Split(sData, " ")
oTS.Close
Set oTS = goFS.CreateTextFile(sFSpecO, True)
' WScript.Echo Join(aParts, "|")
ReDim aData(nCols) ' hold one (new) row to prep for Join
Dim nRow
For nRow = 0 To nRows
Dim nCol
For nCol = 0 To nCols
' magic column hopping
aData(nCol) = aParts(nRow + nCol * nRows2)
Next
oTS.WriteLine Join(aData, ",")
Next
oTS.Close
End Sub
Test:
Dim sFSpecI : sFSpecI = "..\Data\f1.txt"
Dim sFSpecO : sFSpecO = "..\Data\f1.csv"
Dim sCol1 : sCol1 = "Time" ' dangerous - possibly reserved in SQL
WScript.Echo goFS.OpenTextFile(sFSpecI).ReadAll()
doReOrder sFSpecI, sFSpecO, sCol1
WScript.Echo goFS.OpenTextFile(sFSpecO).ReadAll()
Output:
ARRAY ' ' 3 8
Y
25 75 100 125 150 175 200 225
'A' 3 8 6 7 5 3 2 7
'B' 1 9 7 8 4 7 2 5
'C' 8 7 3 6 56 9 111 8
"Time","A","B","C"
25,3,1,8
75,8,9,7
100,6,7,3
125,7,8,6
150,5,4,56
175,3,7,9
200,2,2,111
225,7,5,8