Three text box all line join in list of string - vb.net

I want to do is just like string join with three text box all line will be merge here's my sample
Textbox1.Text lines:
Sample1
Sample2
Sample3
Textbox2.Text lines:
: (seperator)
Textbox3 lines:
Pass1
Pass2
Pass3
Here's my code for merging all text box
For Each str As String In Me.right.Lines
For Each str2 As String In Me.sep.Text
For Each str3 As String In Me.left.Lines
If Not str2 = "" Then
'(change into list of string)
sam.Text += str + str2 + str3 & vbNewLine
Else
'(change into list of string)
sam.Text += str + str3 & vbNewLine
End If
Next
Next
Next
and I want to do my merge result will be
Dim listsample = New List(Of String)
listsample.AddRange(Strings.Split(str + st2 + st3 + " ", " ", -1, CompareMethod.Binary))
and I want final result will be
listsample.AddRamge(Strings.Split("Sample1:Pass1 Sample2:Pass2 Sample3:Pass3, " ", -1, CompareMethod.Binary))
for easy to understand what I'm saying is how can I convert this
sam.Text += str + str3 & vbNewLine
into this
listsample.AddRamge(Strings.Split("Sample1:Pass1 Sample2:Pass2 Sample3:Pass3, " ", -1, CompareMethod.Binary))

Private Sub OpCode()
Dim lst As New List(Of String)
Dim sep = TextBox2.Text
For Each line1 As String In TextBox1.Lines
For Each line2 As String In TextBox3.Lines
If Not sep = "" Then
lst.Add(line1 & sep & line2)
Else
lst.Add(line1 & line2 & vbNewLine)
End If
Next
Next
For Each s In lst
Debug.Print(s)
Next
End Sub

Related

VB.NET ComboBox selected item not remains effect than 2 times

I have a VB windows form application that has 02 ComboBox that provide newname input for a renaming file event. The first combobox provide prefix for new name comprise items (aa, bb, cc,... can add more through keydown button click event), the other combobox provide main name comprise items (XX, YY, ZZ,.. can also add more through keydown button click event). When I select "aa" from the first combobox, "XX" from the other then fire the rename event, the new file name should be "aa - XX", if file "aa - XX" has already existed then add "1" to the last as "aa - XX 1" and so on and if no item selected in prefix combobox the newname just be "XX" and increment. I get the old file name through a system openfiledialog. My code for rename as follows:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim var As String, prfix As String
var = ComboBox1.Text
prfix = ComboBox2.Text
If ComboBox2.Text = Nothing Then
If File.Exists(n & "\" & var & extn) = False Then
My.Computer.FileSystem.RenameFile(OpenFD.FileName, var & extn)
Else
Dim i As Integer = 1
Dim newfn As String = var & " " & i & extn
Dim m As String = n & "\" & newfn
While File.Exists(m)
newfn = var & " " & i & extn
m = n & "\" & newfn
i += 1
End While
My.Computer.FileSystem.RenameFile(OpenFD.FileName, newfn)
End If
Else
If File.Exists(n & "\" & prfix & " - " & var & extn) = False Then
My.Computer.FileSystem.RenameFile(OpenFD.FileName, prfix & " - " & var & extn)
Else
Dim j As Integer = 1
Dim newfn1 As String = prfix & " - " & var & " " & j & extn
Dim k As String = n & "\" & newfn1
While File.Exists(k)
newfn1 = var & " " & j & extn
k = n & "\" & newfn1
j += 1
End While
My.Computer.FileSystem.RenameFile(OpenFD.FileName, newfn1)
End If
End If
MessageBox.Show("Select a next file")
End Sub
My code run well 2 times. After I select "aa" and "XX" and leave it to rename, first result is "aa - XX", the second result is "aa - XX 1" but the third result is "XX", the forth is "XX 1" and then incrementing so on while the result should be "aa - XX 2" and next increment. I don't understand why combobox1 still effective but combobox2 as Nothing after no re-selecting the item in both comboboxes (2 times). I'm very new with VB so any advice should be much appreciated. Thanks.
In your lower Else block, you were incorrectly building up the file name.
You build up the first "newfn1" with:
Dim newfn1 As String = prfix & " - " & var & " " & j & extn
But then below, you used:
newfn1 = var & " " & j & extn
Notice the missing prefix and dash parts at the beginning.
Here's the full corrected version:
Dim j As Integer = 1
Dim newfn1 As String = prfix & " - " & var & " " & j & extn
Dim k As String = Path.Combine(n, newfn1)
While File.Exists(k)
j = j + 1
newfn1 = prfix & " - " & var & " " & j & extn
k = Path.Combine(n, newfn1)
End While
My.Computer.FileSystem.RenameFile(OpenFD.FileName, newfn1)
I'm a little confused by your explanation but if I understand correctly this should help,
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
CreateFile()
End Sub
Private BasePath As String = "" 'TODO
Private Ext As String = "txt"
Private Sub CreateFile()
If ComboBox1.SelectedIndex < 0 OrElse
ComboBox2.SelectedIndex < 0 OrElse
ComboBox1.SelectedItem.ToString = "" OrElse
ComboBox2.SelectedItem.ToString = "" Then
'error message
Exit Sub
End If
Dim fileName As String = String.Format("{0}-{1}.{2}",
ComboBox1.SelectedItem.ToString,
ComboBox2.SelectedItem.ToString,
Ext)
fileName = IO.Path.Combine(BasePath, fileName)
Dim ct As Integer = 1
Do While IO.File.Exists(fileName)
fileName = String.Format("{0}-{1}{3}.{2}",
ComboBox1.SelectedItem.ToString,
ComboBox2.SelectedItem.ToString,
Ext,
ct)
fileName = IO.Path.Combine(BasePath, fileName)
ct += 1
Loop
Dim fs As IO.FileStream = IO.File.Create(fileName)
fs.Close()
fs.Dispose()
End Sub

Operation with two string textboxes separated by a comma vb net

I have 2 strings of numbers: like this:
Textbox1.Text = 1,2,3,4,5,6,7,8,9,11
Textbox2.Text = 11,9,8,7,6,5,4,3,2,1
I want to calculate, display in textbox3, their sum, + or - or * or /
all value from textbox1.text + all values from textbox2.text
Textbox3.Text = 12,11,11,11,11,11,11,11,11,12
I would like, though, and a specification where I can change the sign, instead of + being -, or / or * depending on my choice.
this code is a bit cumbersome, I would like to improve it with something simpler.
Dim str1 As String
'Set your string value
str1 = TxtBoxLstDrawsPlus.Text
Dim str2 As String
'Set your string value
str2 = TxtBoxLstDrawsMinus.Text
Dim strWords As String() = str1.Split(",")
Dim strWordsAAA As String() = str2.Split(",")
TextBox6.Text &= Val(strWords(0)) + Val(strWordsAAA(0)) & "," & Val(strWords(1)) + Val(strWordsAAA(1)) & "," & Val(strWords(2)) + Val(strWordsAAA(2)) & "," & Val(strWords(3)) + Val(strWordsAAA(3)) & "," & Val(strWords(4)) + Val(strWordsAAA(4)) & "," & Val(strWords(5)) + Val(strWordsAAA(5)) & "," & Val(strWords(6)) + Val(strWordsAAA(6)) & "," & Val(strWords(7)) + Val(strWordsAAA(7)) & "," & Val(strWords(8)) + Val(strWordsAAA(8)) & "," & Val(strWords(9)) + Val(strWordsAAA(9)) & "," & Val(strWords(10)) + Val(strWordsAAA(10)) & "," & Val(strWords(11)) + Val(strWordsAAA(11)) & "," & Val(strWords(12)) + Val(strWordsAAA(12)) & "," & Val(strWords(13)) + Val(strWordsAAA(13)) & "," & Val(strWords(14)) + Val(strWordsAAA(14)) & "," & Val(strWords(15)) + Val(strWordsAAA(15)) & "," & Val(strWords(16)) + Val(strWordsAAA(16)) & "," & Val(strWords(17)) + Val(strWordsAAA(17)) & "," & Val(strWords(18)) + Val(strWordsAAA(18)) & "," & Val(strWords(19)) + Val(strWordsAAA(19))
You can split the input strings (TextBox1.Text and TextBox2.Text), Zip the string arrays to produce a result array, and finally String.Join it in TextBox3:
Here's in one line:
TextBox3.Text = String.Join(",", TextBox1.Text.Split({","c}, StringSplitOptions.RemoveEmptyEntries).
Zip(TextBox2.Text.Split({","c}, StringSplitOptions.RemoveEmptyEntries),
Function(x, y) CInt(x) + CInt(y)))
You can use the same approach to do different arithmetic operations:
Dim opr As Char = "+"c '<- From your arithmetic operation selector like ComboBox.
TextBox3.Text = String.Join(",", TextBox1.Text.Split({","c}, StringSplitOptions.RemoveEmptyEntries).
Select(Function(x) CInt(x)).
Zip(TextBox2.Text.Split({","c}, StringSplitOptions.RemoveEmptyEntries).
Select(Function(y) CInt(y)),
Function(x, y)
Select Case opr
Case "-"c
Return x - y
Case "*"c
Return x * y
Case "/"c
Return x \ y
Case Else
Return x + y
End Select
End Function))
It'll be a good idea if you'd validate your inputs first:
Dim Arr1() = TextBox1.Text.Split({","c}, StringSplitOptions.RemoveEmptyEntries)
Dim Arr2() = TextBox2.Text.Split({","c}, StringSplitOptions.RemoveEmptyEntries)
Dim opr As Char = OprTextBox.Text.ElementAtOrDefault(0) 'A TextBox this time as another example.
Dim ValidOpr() As Char = {"+"c, "-"c, "*"c, "/"c}
If Arr1.Length <> Arr2.Length OrElse
Arr1.Any(Function(x) Not Integer.TryParse(x, Nothing)) OrElse
Arr2.Any(Function(x) Not Integer.TryParse(x, Nothing)) OrElse
Not ValidOpr.Contains(Opr) Then
MessageBox.Show("Excuse me ... !?!?")
Return
End If
'You have valid inputs. Proceed...
TextBox3.Text = String.Join(",", Arr1.Select(Function(x) CInt(x)).
Zip(Arr2.Select(Function(x) CInt(x)),
Function(x, y)
Select Case opr
Case "-"c
Return x - y
Case "*"c
Return x * y
Case "/"c
Return x \ y
Case Else
Return x + y
End Select
End Function))
Better yet, enum the arithmetic operations:
Public Enum ArithmeticOperations
Add
Subtract
Multiply
Divide
End Enum
.. and create a parameterized Function that returns the joined string.
'You name it...
Public Function GetCalcString(input1 As String,
input2 As String,
opr As ArithmeticOperations) As String
Dim Arr1() = input1.Split({","c}, StringSplitOptions.RemoveEmptyEntries)
Dim Arr2() = input2.Split({","c}, StringSplitOptions.RemoveEmptyEntries)
If Arr1.Length <> Arr2.Length OrElse
Arr1.Any(Function(x) Not Integer.TryParse(x, Nothing)) OrElse
Arr2.Any(Function(x) Not Integer.TryParse(x, Nothing)) Then
Return Nothing
End If
Return String.Join(",", Arr1.Select(Function(x) CInt(x)).
Zip(Arr2.Select(Function(x) CInt(x)),
Function(x, y)
Select Case opr
Case ArithmeticOperation.Subtract
Return x - y
Case ArithmeticOperation.Multiply
Return x * y
Case ArithmeticOperation.Divide
Return x \ y
Case Else
Return x + y
End Select
End Function))
End Function
Assuming that, the arithmetic operations are listed in a ComboBox in the same order:
Private Sub TheCaller()
TextBox3.Text = GetCalcString(TextBox1.Text,
TextBox12.Text,
CType(ComboBox1.SelectedIndex, ArithmeticOperations))
End Sub

VBA, 2nd last "/" using InstrRev

I have code that parses out the last word on a string.
ie. Stack/Over/Flow will give me "Flow".
But I want to get "Over/Flow".
This is what I got, but only able to get "Flow"
arr(counter - 2) = "'" & mid(Text, InStrRev(Text, "/") + 1) & "'"
I would use Split()
Sub lastTwo()
Dim str As String
str = "Stack/Over/Flow"
Dim splt() As String
splt = Split(str, "/")
If UBound(splt) > 0 Then
Debug.Print splt(UBound(splt) - 1) & "/" & splt(UBound(splt))
End If
End Sub
Here is a function that does it:
Function lastParts(str As String, delim As String, x As Long) As String
Dim splt() As String
splt = Split(str, "/")
If UBound(splt) + 1 >= x Then
Dim t As String
t = "=INDEX(INDEX({""" & Join(splt, """;""") & """},N(IF({1},ROW(" & UBound(splt) - x + 2 & ":" & UBound(splt) + 1 & "))),),)"
lastParts = Join(Application.Transpose(Application.Evaluate(t)), delim)
Else
lastParts = str
End If
End Function
It has three parts, the string, the delimiter and the number of returns.
It can be called using your code:
arr(counter-2) = lastParts(Text,"/",2)
or from the worksheet
=lastParts(A1,"/",2)
Initially misread the question. You can nest InStrRev() calls
arr(counter - 2) = "'" & mid(Text, InStrRev(Text, "/",InStrRev(Text, "/")-1)+1) & "'"

How to insert text in between texts in Textbox VB6/VBA

I wanted to insert some texts(new line) in between existing texts in a textbox (multiline = true).
Example: (Textbox1.text's value is written below)
Name: Name of Client
DOB: 11/11/11
>>>THIS IS WHERE I WHAT TO INSERT THE VALUE OF TEXTBOX2.TEXT
Hospitalization: No
Serial Number: 12345678
Private Sub cmdTransfer_Click()
Dim SearchNote As Integer, SearchThis As String, tx2 As String
If cb9.Value = True Then
tx2 = "ADDRESS: " & vbTab & text2.Text & vbCrLf
End If
SearchThis = "Hospitalization"
SearchNote = InStr(Textbox1.Text, SearchThis)
If SearchNote Then
With textbox1
.SetFocus
.SelStart = SearchNote
.Text = .Text & .SelStart & tx2
End with
End If
End Sub
What I'm doing in my code is I'm getting the number of characters before the "Hospitalization" so that I can insert the value of Textbox2 before it. I dont know how to do that tho. Please help.
Thanks!
I believe the code you are looking for is this:
Left(SearchNote, InStr(1, SearchNote, "Hospitalization") - 1) & "new text to insert" & Mid(SearchNote, InStr(1, SearchNote, "Hospitalization"))
Left will take the first few letters up to the starting point of "Hospitalization". Then you insert the new string (possible with a new line before and after with & chr(10) &). Then you add with Mid everything after "Hospitalization".
Since I don't have a sample copy of your spreadsheet, there is a chance that one/some of my variables might be different. If you find problems with any of these, check all of the vars.
Solution #1: Create module and add this function:
Function addText(txtBox As String, addString As String)
Dim endIndex As Long
Dim SearchThis As String
Dim input1, input2, input3 As String
SearchThis = "Hospitalization"
' Get index of Hospitalization
endIndex = InStr(1, txtBox, SearchThis) - 1
If endIndex > 0 Then
input1 = Mid(txtBox, 1, endIndex)
input2 = addString & vbNewLine
input3 = Mid(txtBox, endIndex, Len(txtBox))
' Return with added text
addText = CStr(input1 & input2 & input3)
End If
End Function
then call in your button to update your text box:
Private Sub cmdTransfer_Click()
Dim tx2 As String
If cb9.Value = True Then
tx2 = "ADDRESS: " & vbTab & text2.Text & vbNewLine
Else
' Stop if there is nothing to add
End
End If
If textbox1.Value <> vbNullString Then
textbox1.Value = addText(textbox1.Value, tx2)
End If
End Sub
Solution #2: Call everything from within your button:
Private Sub cmdTransfer_Click()
Dim endIndex As Long
Dim SearchThis As String
Dim input1, input2, input3 As String
Dim txtBox As String, tx2 As String
'set tx2
If cb9.Value = True Then
tx2 = "ADDRESS: " & vbTab & text2.Text & vbNewLine
Else
' Stop if nothing to add
End
End If
If textbox1.Value <> vbNullString Then
' set txtBox variable
txtBox = textbox1.Value
Else
' Avoid Error if text box is null
End
End If
SearchThis = "Hospitalization"
' Get index of Hospitalization
endIndex = InStr(1, txtBox, SearchThis) - 1
If endIndex > 0 Then
input1 = Mid(txtBox, 1, endIndex)
input2 = tx2 & vbNewLine
input3 = Mid(txtBox, endIndex, Len(txtBox))
textbox1.Value = input1 & input2 & input3
End If
End Sub
What i would do is split text1 into an array then just add the text in the middle, mainString is text1, midStr is text2:
Dim mainStr as String, midStr as String, ArreStr() as String
mainStr=text1.text:midStr=text2.text
ArreStr=Split(mainStr,VBNewLine)
text1.text=ArreStr(0) & vbnewline & midStr & vbnewline & ArreStr(1)

String to abbreviation

I'm a graphic artist, new to Excel and VBA but trying to use it to process mountains of data in excel to be used as variable data in Illustrator.
If I want to convert cells with product names for signs like "Budwieser, Bud Light & Bud Black Crown" to an abbreviation following the format "Budweiser_BL_BBC"
I have written a function that I thought would accomplish my task but it returns #VALUE!
Edit
To explain the logic: my idea was to take the string, split it on " & " and then split the first position of the resulting array on ", " then adding what was after the "&" to the end of the second array - this array, sProd, has the products separated into different positions of the array.
Then looping through that array and splitting each product at the spaces creating a jagged array.
Then loop through that array again creating a string taking only the first letter of each word in each product, separating products with an underscore. The exception being that the first word of the first product is spelled out and set in proper case. (Just saw an error in my logic and added the code for the first word exception).
Edit #2
The function should return a string with the first word of the original string set in proper case with all other words abbreviated to their first letter and products separated by underscores. So "Budweiser, Bud Light & Bud Light Lime" returns "Budweiser_BL_BLL", "All Coke & Dr Pepper Products" would return "AllC_DPP" and "Gatorade" returns "Gatorade".
This is my first bout with Excel and VBA.
Function Abbrev(p As String) As String
Dim sAmpersand() As Variant
Dim sProd() As Variant
sAmpersand = Split(p, " & ")
sProd = Split(sAmpersand(0), ", ")
sProd(UBound(sProd)) = sAmpersand(1)
Dim ProductCount As Integer
Dim ProductEnd As Integer
ProductEnd = UBound(sProd) - 1
For ProductCount = 0 To ProductEnd
sProd(ProductCount) = Split(sProd(ProductCount), " ")
ProductCount = ProductCount + 1
Next ProductCount
Dim WordCount As Integer
Dim WordEnd As Integer
WordEnd = UBound(sProd(ProductCount)) - 1
Abbrev = StrConv(sProd(0)(0), vbProperCase)
For ProductCount = 0 To ProductEnd
For WordCount = 0 To WordEnd
If ProductCount = 0 Then
WordCount = 1
End If
Abbrev = Abbrev & Left(sProd(ProductCount)(WordCount), 1)
WordCount = WordCount + 1
Next WordCount
If ProductCount + 1 < ProductEnd Then
Abbrev = Abbrev & "_"
End If
ProductCount = ProductCount + 1
Next ProductCount
End Function
Working code:
Function Abbrev(p As String) As String
Dim res As String, w1, w2
res = Split(Split(p, ",")(0), " ")(0)
If res = Split(p, ",")(0) Then res = res & "_"
For Each w1 In Split(Mid(Replace(p, " &", ","), Len(res) + 1), ",")
For Each w2 In Split(w1, " ")
res = res & Left(w2, 1)
Next w2
res = res & "_"
Next w1
Abbrev = IIf(Right(res, 1) <> "_", res, Left(res, Len(res) - 1))
End Function
Here's a better abbreviate function:
Function Abbreviate(Name As String) As String
Dim I As Integer
Dim sResult As String
Dim sTemp As String
I = InStr(Name, " ")
If I < 1 Then
Abbreviate = Name
Exit Function
End If
sResult = Left$(Name, I)
sTemp = Name
Do While I > 0
sTemp = Right$(sTemp, Len(sTemp) - I)
If Left$(sTemp, 1) = "(" Then
If Mid$(sTemp & "***", 3, 1) = ")" Then
sResult = sResult & " " & Left$(sTemp, 3)
Else
sResult = sResult & " " & Left$(sTemp, 1)
End If
Else
sResult = sResult & " " & Left(sTemp, 1)
End If
I = InStr(sTemp, " ")
Loop
Abbreviate = sResult
End Function
This is from user al_b_cnu on mrexcel.com
Here is a modified version to shorten up the result a bit:
Function Abbreviate(Name As String) As String
Dim I As Integer
Dim sResult As String
Dim sTemp As String
I = InStr(Name, " ")
If I < 1 Then
Abbreviate = Name
Exit Function
End If
sResult = Left$(Name, I)
sTemp = Name
Do While I > 0
sTemp = Right$(sTemp, Len(sTemp) - I)
If Left$(sTemp, 1) = "(" Then
If Mid$(sTemp & "***", 3, 1) = ")" Then
sResult = sResult & Left$(sTemp, 3)
Else
sResult = sResult & Left$(sTemp, 1)
End If
Else
sResult = sResult & Left(sTemp, 1)
End If
I = InStr(sTemp, " ")
Loop
Abbreviate = sResult
End Function