Created a new embed field automatically once one is full? [VB] - vb.net

Private Async Function cmdList() As Task
Dim m = Context.Message
Dim u = Context.User
Dim g = Context.Guild
Dim c = Context.Client
Dim words As String = ""
Dim embed As New EmbedBuilder With {
.Title = $"Wallpaper keyword list",
.ImageUrl = "https://i.imgur.com/vc241Ku.jpeg",
.Description = "The full list of keywords in our random wallpaper list",
.Color = New Color(masterClass.randomEmbedColor),
.ThumbnailUrl = g.IconUrl,
.Timestamp = Context.Message.Timestamp,
.Footer = New EmbedFooterBuilder With {
.Text = "Keyword Data",
.IconUrl = g.IconUrl
}
}
For Each keyword As String In wall.keywords
words = words + keyword + " **|** "
Next
embed.AddField("Full list", words)
Await m.Channel.SendMessageAsync("", False, embed.Build())
End Function
This is my command to get every word from an array and put it on a field. What I want to know is how do I make it so once the field gets full it'll automatically add a new one and continue with the list. This might be a little far-fetched but just don't know how to go about this. Sorry if I can't understand any of the answers. I'm still a little new to coding on Discord.net and well vb in general.

This is a modification of you hastebin code
Dim row As Integer = 0
Dim words As String = String.Empty
For Each keyword As String In wall.keywords
'If appending the keyword to the list of words exceeds 256
'don't append, but instead add the existing words to a field.
If words.Length + keyword.length + 7 > 256 Then
row += 1
embed.AddField($"List #{row}", words) 'Add words to field
'reset words
words = String.Empty
End If
words = words + keyword + " **|** "
Next
'The add condition within the for loop is only entered when we are
'about to exceed to field length. Anything string under the max
'length would exit the loop without being added. Add it here
embed.AddField($"List #{row + 1}", words)
Await m.Channel.SendMessageAsync("", False, embed.Build())
While it does not change any of the logic, you could consider using a StringBuilder

Related

Create Array of Labels

I'm really hoping to find some way to either create an Array of Label() or to create a Label variable with a constructed name using a string. Unfortunately after searching I'm afraid it won't be possible so I'm looking for alternatives as well.
I have a List of Strings, and I would like to create a Label for each one, inside of a Form using System.Windows.Forms. I am hoping to avoid having one large label holding all of the text, or needing to create a large number of predefined labels, as the number of Strings my list contains will vary from 3 or 4 to 30 or higher.
After some trial and error I have managed to get it not crashing, and the information I am setting to the Label is setting correctly, but becomes nothing outside of the For Loop.
Sub CheckResult(ByRef strList As List(Of String))
Dim LineCount As Integer = strList.Count
Dim Line As String
With CheckForm
.size = New System.Drawing.Size(340, (LineCount * 20) + 40 )
.StartPosition = FormStartPosition.CenterScreen
.MaximizeBox = False
.MinimizeBox = False
.Text = CheckType
End With
Dim CheckTextLabel(0 To LineCount - 1) As Label
Dim i As Integer = 0
For Each Line In strList
Line = strList(i)
CheckTextLabel(i) = New Label()
With CheckTextLabel(i)
.Text = Line
.Size = New System.Drawing.Size(320, 20)
.Location = New System.Drawing.Point(10, 10 + (LineCount * 20))
'.Font = MidFont
End With
CheckForm.Controls.Add(CheckTextLabel(i))
'Location 1
i += 1
Next
'Location 2
...
When I check the value of CheckTextLabel(i).Text at Location 1, I get the expected value.
If I check the value of CheckTextLabel(1) or any other value for i at Location 2, it returns a blank result.
Please let me know if there is a way to do this, and if as I fear there is not, I'll accept alternatives

Substring Method in VB.Net

I have Textboxes Lines:
{ LstScan = 1,100, DrwR2 = 0000000043 }
{ LstScan = 2,200, DrwR2 = 0000000041 }
{ LstScan = 3,300, DrwR2 = 0000000037 }
I should display:
1,100
2,200
3,300
this is a code that I can't bring to a working stage.
Dim data As String = TextBox1.Lines(0)
' Part 1: get the index of a separator with IndexOf.
Dim separator As String = "{ LstScan ="
Dim separatorIndex = data.IndexOf(separator)
' Part 2: see if separator exists.
' ... Get the following part with Substring.
If separatorIndex >= 0 Then
Dim value As String = data.Substring(separatorIndex + separator.Length)
TextBox2.AppendText(vbCrLf & value)
End If
Display as follows:
1,100, DrwR2 = 0000000043 }
This should work:
Function ParseLine(input As String) As String
Const startKey As String = "LstScan = "
Const stopKey As String = ", "
Dim startIndex As String = input.IndexOf(startKey)
Dim length As String = input.IndexOf(stopKey) - startIndex
Return input.SubString(startIndex, length)
End Function
TextBox2.Text = String.Join(vbCrLf, TextBox1.Lines.Select(AddressOf ParseLine))
If I wanted, I could turn that entire thing into a single (messy) line... but this is more readable. If I'm not confident every line in the textbox will match that format, I can also insert a Where() just before the Select().
Your problem is you're using the version of substring that takes from the start index to the end of the string:
"hello world".Substring(3) 'take from 4th character to end of string
lo world
Use the version of substring that takes another number for the length to cut:
"hello world".Substring(3, 5) 'take 5 chars starting from 4th char
lo wo
If your string will vary in length that needs extracting you'll have to run another search (for example, searching for the first occurrence of , after the start character, and subtracting the start index from the newly found index)
Actually, I'd probably use Split for this, because it's clean and easy to read:
Dim data As String = TextBox1.Lines(0)
Dim arr = data.Split()
Dim thing = arr(3)
thing now contains 1,100, and you can use TrimEnd(","c) to remove the final comma
thing = thing.TrimEnd(","c)
You can reduce it to a one-liner:
TextBox1.Lines(0).Split()(3).TrimEnd(","c)

VB - Sorting Alphabetically From a CSV File

I don't know a lot about the subject of sorting but here goes: I am trying to sort a music library (comma seperated in a csv file. Some examples):
1,Sweet Home Alabame,Lynyrd Skynyrd,4:40,Classic Rock
2,Misirlou,Dick Dale,2:16,Surf Rock
I need to sort them alphabetically (by title of track) but I don't know two things: 1. Why my current technique isn't working:
Dim array() As String = {}
sr = New StreamReader("library.csv")
counter = 1
Do Until sr.EndOfStream
array(counter) = sr.ReadLine()
counter += 1
Loop
System.Array.Sort(Of String)(array)
Dim value As String
For Each value In array
Console.WriteLine(value)
Next
Console.ReadLine()
I don't know if this is the best way of sorting. I then need to display them as well. I can do this without sorting, but can't figure out how to do it with sorting.
Help please (from people who, unlike me, know what they're doing).
Right now you're putting all fields in one long string of text (each row).
In order to sort by a particular field, you'll need to build a matrix of rows and columns. For example, a DataTable.
Here's a class that should do the trick for you:
https://www.codeproject.com/Articles/11698/A-Portable-and-Efficient-Generic-Parser-for-Flat-F
Here's the sample usage code from the article, translated to VB:
Public Class CsvImporter
Public Sub Import()
Dim dsResult As DataSet
' Using an XML Config file.
Using parser As New GenericParserAdapter("MyData.txt")
parser.Load("MyData.xml")
dsResult = parser.GetDataSet()
End Using
' Or... programmatically setting up the parser for TSV.
Dim strID As String, strName As String, strStatus As String
Using parser As New GenericParser()
parser.SetDataSource("MyData.txt")
parser.ColumnDelimiter = vbTab.ToCharArray()
parser.FirstRowHasHeader = True
parser.SkipStartingDataRows = 10
parser.MaxBufferSize = 4096
parser.MaxRows = 500
parser.TextQualifier = """"c
While parser.Read()
strID = parser("ID")
strName = parser("Name")
' Your code here ...
strStatus = parser("Status")
End While
End Using
' Or... programmatically setting up the parser for Fixed-width.
Using parser As New GenericParser()
parser.SetDataSource("MyData.txt")
parser.ColumnWidths = New Integer(3) {10, 10, 10, 10}
parser.SkipStartingDataRows = 10
parser.MaxRows = 500
While parser.Read()
strID = parser("ID")
strName = parser("Name")
' Your code here ...
strStatus = parser("Status")
End While
End Using
End Sub
End Class
There's also this from here, demonstrating DataTable usage:
Dim csv = "Name, Age" & vbCr & vbLf & "Ronnie, 30" & vbCr & vbLf & "Mark, 40" & vbCr & vbLf & "Ace, 50"
Dim reader As TextReader = New StringReader(csv)
Dim table = New DataTable()
Using it = reader.ReadCsvWithHeader().GetEnumerator()
If Not it.MoveNext() Then
Return
End If
For Each k As var In it.Current.Keys
table.Columns.Add(k)
Next
Do
Dim row = table.NewRow()
For Each k As var In it.Current.Keys
row(k) = it.Current(k)
Next
table.Rows.Add(row)
Loop While it.MoveNext()
End Using
And this Q&A illustrates how to sort the DataTable by a given column.

string.split vb.net

I have a text in database which is "computer-hardware". I used that code to split
' string seperated by colons '-'
Dim info As String = strcotegory
Dim arInfo As String() = New String(3) {}
' define which character is seperating fields
Dim splitter As Char() = {"-"c}
arInfo = info.Split(splitter)
For x As Integer = 0 To arInfo.Length - 1
Response.Write(arInfo(x) & "</br> ")
Next
but now I want to get "computer" in textbox1 and "hardware" in textbox2.
please guide me
If I understood you correctly, replace the For loop with the following lines of code:
textbox1.Text = arInfo(0)
textbox2.Text = arInfo(1)
By the way, initializing arInfo (... = New String(3) {}) is not necessary, since you overwrite the value of arInfo anyway (arInfo = ...).

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