Check if each line from a text file contains a certain string, and add the ones that do to a listbox? - vb.net

The following code works, to simply get each line as a line in the listbox.
Reader = IO.File.OpenText(textlocation)
Dim bookmarks() As String = Reader.ReadToEnd.Split(vbNewLine)
Dim i As Integer = 0
Do Until i = bookmarks.Length
lstFavorites.Items.Add(bookmarks(i))
i += 1
Loop
But I don't want every line to go into the text box. I only want the lines that contain the text "Bookmark" to go into the listbox. What can I do to achieve this? I've tried everything I can think of.
Heres some code I tried, I can't see the problem in it, but it seems to just crash my program.
Do Until i = bookmarks.Length
If bookmarks(i).Contains("at") Then
If radBookmarks.Checked Then
If bookmarks(i).Contains("Bookmark") Then
Original = bookmarks(i)
BeginningOfDemoName = Original.Substring(Original.LastIndexOf("(") + 2)
TickWithParenthesis = BeginningOfDemoName.Substring(BeginningOfDemoName.IndexOf(Chr(34)) + 4)
Tick = TickWithParenthesis.Split(" ")(1).Split(")")(0)
DemoName = BeginningOfDemoName.Split(Chr(34))(0)
ToList = DemoName + " at " + Tick
lstFavorites.Items.Add(ToList)
i += 1
Else
i += 1
End If
ElseIf radEverything.Checked Then
Original = bookmarks(i)
BeginningOfDemoName = Original.Substring(Original.LastIndexOf("(") + 2)
TickWithParenthesis = BeginningOfDemoName.Substring(BeginningOfDemoName.IndexOf(Chr(34)) + 4)
Tick = TickWithParenthesis.Split(" ")(1).Split(")")(0)
DemoName = BeginningOfDemoName.Split(Chr(34))(0)
ToList = DemoName + " at " + Tick
lstFavorites.Items.Add(ToList)
i += 1
End If
End If
Loop

Try to change this line
If bookmarks(i).Contains("Bookmark") Then
to
If bookmarks(i).IndexOf("Bookmark",
StringComparison.CurrentCultureIgnoreCase) >= 0 Then
Contains do a case sensitive comparison and your input string contains a lower case 'bookmark'

Related

Access: Hiding the header on the first page of a section works initially...and then fails. Any thoughts?

I am using code based on widely used methods described here and elsewhere to suppress a page header on the first page of a report section in Access 2016. The code works fine for the first three sections but then removes the header on the second page rather than the first and gets the section page count wrong, including the first page of the following section in the total. If I eliminate the .visible=False statement, the section page numbering is fine, so it feels like removing the header section is messing up the pagination in a way that Access is not adjusting for? Has anyone else had this problem and if so, is there a solution?
Many thanks in advance
Simon
Private Sub PageHeaderSection_Format(Cancel As Integer, FormatCount As Integer)
Dim i As Integer
If Me.Pages = 0 Then
ReDim Preserve GrpArrayPage(Me.Page + 1)
ReDim Preserve GrpArrayPages(Me.Page + 1)
GrpNameCurrent = Me!txtCompany_name2
If GrpNameCurrent = GrpNamePrevious Then
GrpArrayPage(Me.Page) = GrpArrayPage(Me.Page - 1) + 1
GrpPages = GrpArrayPage(Me.Page)
For i = Me.Page - ((GrpPages) - 1) To Me.Page
GrpArrayPages(i) = GrpPages
Next i
Else
GrpPage = 1
GrpArrayPage(Me.Page) = GrpPage
GrpArrayPages(Me.Page) = GrpPage
End If
Else
Me!txtCheck = "Group Page " & GrpArrayPage(Me.Page) & " of " & GrpArrayPages(Me.Page)
If GrpArrayPage(Me.Page) = 1 Then
Me.PageHeaderSection.Visible = False
'Me.PageFooterSection.Visible = True
Else
Me.PageHeaderSection.Visible = True
'Me.PageFooterSection.Visible = False
End If
End If
GrpNamePrevious = GrpNameCurrent
End Sub

Loop through Excel Ranges Filling Combobox VB.Net

I have a program that needs to iterate through a very large excel range, to combine two ranges into one combo box value. I have the following code to do so, but all it does is iterate the first value five times. If I remove the first FOR loop, then it simply only returns the first value and never completes. Any suggestions on what I can do to polish this code up and get it working?
Dim i As Integer
If TenantBox.SelectedItem = "CNS" Then
WFMBook.Workbooks.Open("C:\Schedule.xlsx")
For i = 0 To 5 Step +1
For Each CNSCell In WFMBook.Range("A3:A1441").Cells
f = CNSCell.Value.ToString
Next
For Each tst In WFMBook.Range("B3:B1441").Cells
l = tst.Value.ToString
Next
ComboBox1.Items.Add(f + " " + l)
If (i = 5) Then
Exit For
End If
Console.WriteLine(i)
Next
End If
Try this, just change the x to 1 if VBA is 1 based.
If TenantBox.SelectedItem = "CNS" Then
WFMBook.Workbooks.Open("C:\XHSchedule.xlsx")
Dim colCount = WFMBook.Range("A3:A8").Cells.Count
For x = 1 To colCount Step +1
For Each CNSCell In WFMBook.Range("A3:A8").Cells
f = WFMBook.Range("A3:A8").Cells(x).Value.ToString
l = WFMBook.Range("B3:B8").Cells(x).Value.ToString
Next
ComboBox1.Items.Add(f + " " + l)
Next
End If

Adding content of a cell into a string variable in VBA. Also, adding cells below it using offset

I have a simple function that is going to pull from a specific Cell (N4). If there are cells below it then it will loop and collect all the content of the cell and separate with comma.
I'm getting #VALUE! error in excel right now and I kind of know where my issue lies but can not figure how to fix it, since I am not that proficient with excel-vba code. I think the issue is with ActiveSheet.Range("N4").Value and the offset part.
I am not sure how to offset then select the value in the text then add it to my string Value.
Any thought on how I can select the content of cell and add to string variable and doing the same when you offset?
Here is my code:
Function pullshit() As String
Dim output As String
Dim counter As Integer
counter = 1
output = ActiveSheet.Range("N4").Value
If Application.offset(N4, counter, 0).Value = "" Then
pullshit = output
Else
While counter <> 0
output = output + ", " + Application.offset(N4, counter, 0).Value
counter = counter + 1
If Application.offset(N4, counter, 0) = "" Then
counter = 0
End If
Wend
pullshit = output
End If
End Function
Offset use is wrong. Try this.
Function pullshit() As String
Dim output As String
Dim counter As Integer
counter = 1
output = ActiveSheet.Range("N4").Value
If Range("N4").Offset(counter, 0).Value = "" Then
pullshit = output
Else
While counter <> 0
output = output + ", " + Range("N4").Offset(counter, 0).Value
counter = counter + 1
If Range("N4").Offset(counter, 0) = "" Then
counter = 0
End If
Wend
pullshit = output
End If
End Function
Use of the function in a screenshot

Making text go to a new line when in a while loop

I have a richTextbox, and a while loop, x = 0, and every time this loop runs, x += 1 till it reaches a certain value.
Heres what I want to happen:
while x <> 10 then
it will say item 0 +1 on a new line, and then item 1 + 1 on the line under it, etc, so you will see all 10 values after.
What happens is, it will change that line into the new value.
My question is: How do I make it put the words on a new line instead?
Here is my code:
While readListItem <> PaymentList.Items.Count
If readListItem > PaymentList.Items.Count Then
readListItem = 0
Exit While
End If
readListItem += 1
MessageBox.Show(readListItem)
resultBox.Text = vbNewLine + PaymentList.Items.Item(readListItem) + " costs " + enteredCost + "." + vbNewLine
End While
readListItem is "x", and that is being iterated by 1 every time the loop runs
PaymentList is my listbox containing an unknown value (the user sets the number of items in this list)
The If if there because, after x = lets say 10, it would add another to x (so now it = 11) and try to print out the 11th item, as it doesnt exist, it crashes. This is to attempt to prevent that. I didnt want to go with a try catch
Try adding the new value instead of replacing the entire value:
resultBox.Text &= Environment.NewLine() & PaymentList.Items.Item(readListItem) + " costs " + enteredCost + "."
The expression &= is equivalent, in this case, to resultBox.Text = resultBox.Text & "...".
And once advice, you can simplify your loop by using this condition:
While readListItem < PaymentList.Items.Count
'...
End While

Cant figure out how to merge variables vb.net

I am creating a for each loop to take the words from a string and place them each into a text box. The program allows for up to "9" variables What I am trying to attempt is.
Foreach word in Words
i = i +1
Varible & i = word
txtCritical1.Text = variable & i
any ideas on a way to make this work?
Have a look through the MSDN article on For Each. It includes a sample using strings.
https://msdn.microsoft.com/en-us/library/5ebk1751.aspx
So i went with a simple if statement this does the trick. Each text box is filled in.
Dim details As String = EditEvent.EditDbTable.Rows(0).Item(13).ToString()
Dim words As String() = details.Split(New Char() {"«"})
Dim word As String
For Each word In words
i = i + 1
v = word
If i = 1 Then
txtCritical1.Text = v
ElseIf i = 2 Then
txtCritical2.Text = v
ElseIf ....
ElseIf i = 9 then
txtCritical2.text = v
Else
....
End If
Next