Read text file and name buttons VB.NET - vb.net

What I wan't to do is to read a text file with 1, 2, 3, 4 or 5 lines.
If the text file is completely full (5 lines) then the code works.
But the code needs to work when there are only 3 lines in the text file.
So eventually you get 3 buttons with names and 2 without.
The code:
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Lines() As String = IO.File.ReadAllLines(file)
Dim Ordernummer1 As String
Dim Ordernummer2 As String
Dim Ordernummer3 As String
Dim Ordernummer4 As String
Dim Ordernummer5 As String
Dim Regels As New List(Of String)
Select Case Lines.Count
Case 1
Lines(1) = ""
Lines(2) = ""
Lines(3) = ""
Lines(4) = ""
Case 2
Lines(2) = ""
Lines(3) = ""
Lines(4) = ""
Case 3
Lines(3) = ""
Lines(4) = ""
Case 4
Lines.add(4) = ""
Case 5
'nothing
End Select
MsgBox(Lines(0))
last = Lines(0).Substring(Lines(0).LastIndexOf("\") + 1)
MsgBox(last)
Ordernummer1 = last.Substring(0, last.IndexOf(" "))
MsgBox(Ordernummer1)
Button12.Text = Ordernummer1
MsgBox(Lines(1))
last = Lines(1).Substring(Lines(1).LastIndexOf("\") + 1)
MsgBox(last)
Ordernummer2 = last.Substring(0, last.IndexOf(" "))
MsgBox(Ordernummer2)
Button13.Text = Ordernummer2
MsgBox(Lines(2))
last = Lines(2).Substring(Lines(2).LastIndexOf("\") + 1)
MsgBox(last)
Ordernummer3 = last.Substring(0, last.IndexOf(" "))
MsgBox(Ordernummer3)
Button14.Text = Ordernummer3
MsgBox(Lines(3))
last = Lines(3).Substring(Lines(3).LastIndexOf("\") + 1)
MsgBox(last)
Ordernummer4 = last.Substring(0, last.IndexOf(" "))
MsgBox(Ordernummer4)
Button15.Text = Ordernummer4
MsgBox(Lines(4))
last = Lines(4).Substring(Lines(4).LastIndexOf("\") + 1)
MsgBox(last)
Ordernummer5 = last.Substring(0, last.IndexOf(" "))
MsgBox(Ordernummer5)
Button16.Text = Ordernummer5

One option:
Dim lines = File.ReadAllLines(filePath)
Dim buttons = {Button12, Button13, Button14, Button15, Button16}
'Process lines and corresponding Buttons.
For index = 0 To lines.GetUpperBound(0)
Dim line = lines(index)
Dim btn = buttons(index)
'Use line and btn here.
Next
'Process remaining Buttons if desired.
For index = lines.Length To buttons.GetUpperBound(0) Step 1
'E.g.
buttons(index).Hide()
Next

Related

Use String Join in VB.Net

I have TxtGamblerImput.Text = 2 4 6 8 10 how should that work that code? how can use that code in that textbox?
TextBox4.Text = String.Join(" ", myFirstComb)
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
'This your First Combination, maximal widht = element
Dim Content As String = TxtGamblerImput.Text
'This Your Element
Dim Element As Integer = 5
Dim myFirstComb = (From mVal In Content.ToString.Split() Select Convert.ToInt32(mVal)).ToArray
'This Your Result List
Dim MyList As New List(Of String)
Dim myM As Integer = myFirstComb.Length
Dim myPost = myM
For myCnt = myM To 1 Step -1
Dim LastStr = ""
For mycnt3 = 1 To Element
For myCnt2 = myCnt To myM
'If myCnt2 = myM Then MsgBox("T")
Dim myVal = myFirstComb(myCnt2 - 1)
If myVal > Element Then myVal = 1
If Element - myM + myCnt2 = myVal Then
myFirstComb(myCnt2 - 1) = myVal
Else
If (myVal + 1) > Element Then myVal = 0
myFirstComb(myCnt2 - 1) = myVal + 1
End If
Next
Dim Hsl = String.Join(" ", myFirstComb)
If LastStr <> Hsl Then
MyList.Add(Hsl)
End If
LastStr = Hsl
Next
Next
myFirstComb = (From mVal In Content.ToString.Split() Select Convert.ToInt32(mVal))
TextBox4.Text = String.Join(" ", myFirstComb)
End Sub
In that Output: Show 2 4 6 8 10
I dind't work? How can use that code for worked?

Split string and put it in a textbox

How to split the string "V237P023F50.5" into different text boxes.
I am trying this code:
Dim input As String = "V237P023F50.5"
TextBox1.Text = input
Dim parts As String() = input.Split(New String() {"V"}, StringSplitOptions.RemoveEmptyEntries)
Dim parts1 As String() = input.Split(New String() {"V", "P"}, StringSplitOptions.RemoveEmptyEntries)
Dim parts2 As String() = input.Split(New String() {"V", "P", "F"}, StringSplitOptions.RemoveEmptyEntries)
For i As Integer = 0 To parts.Length - 1
If i > 0 OrElse input.StartsWith("V") = True Then
parts(i) = "" & parts(i)
TextBox2.Text = parts(i)
End If
Next
For i As Integer = 0 To parts1.Length - 1
If i > 0 OrElse input.StartsWith("P") = True Then
parts1(i) = "" & parts1(i)
TextBox4.Text = parts1(i)
End If
Next
For i As Integer = 0 To parts2.Length - 1
If i > 0 OrElse input.StartsWith("F") = True Then
parts2(i) = "" & parts2(i)
TextBox5.Text = parts2(i)
End If
Next
Expected output
V237
P023
F50.5
Please help me.
If this is fixed length then use Substring().
Dim oldstring As String = "V237P023F50.5"
TextBox2.Text = oldstring.Substring(0, 4)
TextBox4.Text = oldstring.Substring(4, 4)
TextBox5.Text = oldstring.Substring(8)
If the alphacharacters are the same then you can use IndexOf() with Substring.
Dim oldstring As String = "V237P023F50.5"
TextBox2.Text = oldstring.Substring(oldstring.IndexOf("V" ), (oldstring.IndexOf("P") - oldstring.IndexOf("V")))
TextBox4.Text = oldstring.Substring(oldstring.IndexOf("P" ), (oldstring.IndexOf("F") - oldstring.IndexOf("P")))
TextBox5.Text = oldstring.Substring(oldstring.IndexOf("F" ))

Using MailMessage with semi cologn seperation

If I manually put my address in for EmailMessage.To.Add(GetDelimitedField(x, strEmailRep, ";")) It sends me the message just fine. However If I use the code as is below which is using a list that looks like ;email1#mail.com;email2.mail.com
Then it gives an error that email address cannot be blank
Somewhere in GetDelimitedField is erasing addresses. I'm not sure where the problem is actually occurring. Here is all the code involved with this.
strmsg = "LOW STOCK ALERT: Component (" & rsMPCS("MTI_PART_NO") & ") has reached or fallen below it's minimum quantity(" & rsMPCS("MIN_QTY") & ")."
Dim EmailMessage As MailMessage = New MailMessage
EmailMessage.From = New MailAddress("noreply#mail.com")
For x = 1 To GetCommaCount(strEmailRep) + 1
EmailMessage.To.Add(GetDelimitedField(x, strEmailRep, ";"))
Next
EmailMessage.Subject = ("LOW STOCK ALERT!")
EmailMessage.Body = strmsg
EmailMessage.Priority = MailPriority.High
EmailMessage.IsBodyHtml = True
Dim smtp As New SmtpClient("smtp.mycompany.com")
smtp.UseDefaultCredentials = True
smtp.Send(EmailMessage)
Public Function GetCommaCount(ByVal sText As String)
Dim X As Integer
Dim Count As Integer
Dim Look As String
For X = 1 To Len(sText)
Look = Microsoft.VisualBasic.Left(sText, X)
If InStr(X, Look, ";", 1) > 0 Then
Count = Count + 1
End If
Next
GetCommaCount = Count
End Function
Public Function GetDelimitedField(ByRef FieldNum As Short, ByRef DelimitedString As String, ByRef Delimiter As String) As String
Dim NewPos As Short
Dim FieldCounter As Short
Dim FieldData As String
Dim RightLength As Short
Dim NextDelimiter As Short
If (DelimitedString = "") Or (Delimiter = "") Or (FieldNum = 0) Then
GetDelimitedField = ""
Exit Function
End If
NewPos = 1
FieldCounter = 1
While (FieldCounter < FieldNum) And (NewPos <> 0)
NewPos = InStr(NewPos, DelimitedString, Delimiter, CompareMethod.Text)
If NewPos <> 0 Then
FieldCounter = FieldCounter + 1
NewPos = NewPos + 1
End If
End While
RightLength = Len(DelimitedString) - NewPos + 1
FieldData = Microsoft.VisualBasic.Right(DelimitedString, RightLength)
NextDelimiter = InStr(1, FieldData, Delimiter, CompareMethod.Text)
If NextDelimiter <> 0 Then
FieldData = Microsoft.VisualBasic.Left(FieldData, NextDelimiter - 1)
End If
GetDelimitedField = FieldData
End Function
You can split the list easier using string.Split:
Dim strEmails = "a#test.com;b#test.com;c#test.com;"
Dim lstEmails = strEmails.Split(";").ToList()
'In case the last one had a semicolon:
If (lstEmails(lstEmails.Count - 1).Trim() = String.Empty) Then
lstEmails.RemoveAt(lstEmails.Count - 1)
End If
If (lstEmails.Count > 0) Then
lstEmails.AddRange(lstEmails)
End If

Hangman vb.net only first letter showing

Basically I generate a random word for example
"Tree" and when I press the T button it changes the label into a T but then when I choose R it doesnt show, can someone else see what i've done wrong?
here is my code
Sub GuessLetter(ByVal LetterGuess As String)
Dim strGuessedSoFar As String = Lbltempword.Text
Dim LengthOfSecretWord As Integer
LengthOfSecretWord = secret.Length - 1
tempWord = ""
Dim letterPosition As Integer
For letterPosition = 0 To LengthOfSecretWord
If secret.Substring(letterPosition, 1) = LetterGuess Then
tempWord = tempWord & LetterGuess
Else
tempWord = tempWord & Lbltempword.Text.Substring(letterPosition, 1)
End If
Next
Lbltempword.Text = tempWord
If Lbltempword.Text = secret Then 'YOU WIN
DisableButtons()
BtnStart.Enabled = True
MsgBox("YOU WIN")
End If
If Lbltempword.Text = strGuessedSoFar Then
NumWrong = NumWrong + 1
End If
DisplayHangman(NumWrong)
End Sub
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
randomword()
MsgBox(secret)
EnableButtons()
BtnStart.Enabled = False
'Load up the temp word label with dashes
Secret_Word = secret
LoadLabelDisplay()
NumWrong = 0
DisplayHangman(NumWrong)
End Sub
Sub LoadLabelDisplay()
Lbltempword.Text = ""
Dim LengthOfSecretWord As Integer
LengthOfSecretWord = secret.Length - 1
Dim LetterPosition As Integer
For LetterPosition = 0 To LengthOfSecretWord
Lbltempword.Text = Lbltempword.Text & "-"
Next
End Sub
I also generate the random words by doing this.
Sub randomword()
Dim RAND(16)
Dim rng As New System.Random()
For i = 0 To 16
RAND(0) = "Tree"
RAND(1) = "Star"
RAND(2) = "Jesus"
RAND(3) = "Present"
RAND(4) = "advent"
RAND(5) = "Calender"
RAND(6) = "Jinglebell"
RAND(7) = "skint"
RAND(8) = "lapland"
RAND(9) = "Santa"
RAND(10) = "raindeer"
RAND(11) = "Cookies"
RAND(12) = "Milk"
RAND(13) = "nothing"
RAND(14) = "play"
RAND(15) = "sack"
Next
secret = RAND(rng.Next(RAND.Count()))
End Sub

adding color, depending on item value in list view

I have a list view like this:
It shows item description and how much quantity delivered.
If all quantity delivered then, I need to show line in green.
If any quantity delivered(partial) then, I need to show in orange.
If no quantity delivered then, I need to show yellow.
I have given code like this,and it is not working.
ListView1.Font = New System.Drawing.Font("Tahoma", 8.0!, System.Drawing.FontStyle.Bold)
ListView1.Items.Clear()
If ds.Tables(0).Rows.Count > 0 Then
Dim dt As DataTable = ds.Tables(0)
Dim str(ds.Tables(0).Columns.Count) As String
Dim lvi As ListViewItem
Dim rr As DataRow
Dim highlight As String = ""
Dim temp1 As String = ""
Dim temp2 As String = ""
Dim temp1Sum, temp2Sum As Integer
Dim diffCount As Integer = 0
Dim newColumn As Integer = 0
Dim strTemp As String
For Each rr In dt.Rows
For col As Integer = 0 To ds.Tables(0).Columns.Count - 1
str(col) = rr(col).ToString()
If col > 4 And col > newColumn Then
Dim qtyVal As String
qtyVal = rr(col).ToString
strTemp = qtyVal
If temp1 = "" Then
temp1 = IIf(qtyVal = "", "0.00", qtyVal)
Else
temp2 = temp1
temp1 = IIf(qtyVal = "", "0.00", qtyVal)
newColumn = col + 1
End If
If temp1 <> "" And temp2 <> "" Then
If temp1 <> temp2 Then
diffCount = diffCount + 1
End If
temp1 = ""
temp2 = ""
End If
End If
Next
lvi = New ListViewItem(str)
ListView1.Items.Add(lvi)
If diffCount = 0 Then
lvi.BackColor = Color.Green
noofdeliver = noofdeliver + 1
txtdelivercount.Text = noofdeliver
ElseIf diffCount > 0 Then
If temp2Sum = 0 Then
lvi.BackColor = Color.Yellow
Else
lvi.BackColor = Color.Orange
End If
End If
temp2Sum = 0
diffCount = 0
newColumn = 0
Next
End If
Put this line ListView1.Items.Add(lvi) after the color changing
TEST CODE
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim lvi = New ListViewItem("foo " & ListView1.Items.Count + 1)
lvi.BackColor = Color.Red
ListView1.Items.Add(lvi)
End Sub
here the actual problem is...
you are using str variable as array but you are assigning to ListViewItem as lvi = New ListViewItem(str) without specifying the index for str array. As you specified in code it always takes the first value of the array only..
solution: when you are assigning array str value to ListViewItemlvi specify the corresponding index value.