msgbox dilemma with translator app - vb.net

I am building language translator for my tribe. I have about 90% it completed. the only problem i am having is that i want to have msgbox pop up and say "translation not found.", when someone enters a phrase or a word that can't be found in the text file which is read " cat | nish stu yah." I can have it pop up when the translation isn't found, but here is the problem when i type in a word that is found the msgbox pop up as well. I dont know if it has to do with that it is running a do while (true) loop or i am just not coding the translator button correctly. the code is:
Do While (True)
Dim line As String = reader.ReadLine
If line Is Nothing Then
Exit Do
End If
Dim words As String() = line.Split("|")
Dim word As String
For Each word In words
If word = TextBox1.Text Then
TextBox2.Text = words(+1)
Else
MessageBox.Show("Translation is not available")
End If
If "" = TextBox1.Text Then
MessageBox.Show("No entry was made.")
End If
Next
Loop

Assuming your file format is:
cat | nish stu yah
with the spaces around the | character, your issue is probably that String.Split will not remove the spaces, so in this case you will end up with:
words(0) = "cat "
words(1) = " nish stu yah"
So, if TextBox1.Text is "cat", it will not match. You could change the comparison to something like this, to make it more forgiving:
If word.Trim().ToLower() = TextBox1.Text.Trim().ToLower() Then
However, you don't really need to loop through words, since you only want to compare the user input against the untranslated version. You are also showing the message box for each line in the translation file. I think you probably want something like this (completely untested):
If "" = TextBox1.Text Then
MessageBox.Show("No entry was made.")
Exit Sub
End If
Dim found As Boolean = False
Do While (True)
Dim line As String = reader.ReadLine()
If line Is Nothing Then
Exit Do
End If
Dim words As String() = line.Split("|"c)
If words(0).Trim().ToLower() = TextBox1.Text.Trim().ToLower() Then
TextBox2.Text = words(1)
found = True
Exit Do
End If
Loop
If Not found Then
MessageBox.Show("Translation is not available")
End If
Still not the most elegant code, but hopefully points you in the right direction.

Related

Multiple index searches

I have written a code to search for text using the index to not save to the listbox.
I need to add more characters to search.
I need to find the text starting with - and at the end -> this text is not written to the listbox.
They also need to find text beginning with / ** and at the end of * / but this code no longer works.
Dim openfile = New OpenFileDialog() With {.Filter = "Text (*.php)|*.php"}
If (openfile.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
For Each line As String In File.ReadAllLines(openfile.FileName, Encoding.Default)
'OPEN DIALOG
Next
End If
Dim komentar As Boolean
komentar = False
For Each line As String In File.ReadAllLines(openfile.FileName, Encoding.Default)
'FIND Charakter <!- and stop write to listbox
If Not line.IndexOf("<!-") = -1 Then
komentar = True
End If
If komentar Then --- this wrong
'FIND Charakter --> and afrer write to listbox
If Not line.IndexOf("-->") = -1 Then
komentar = False
End If
' I don't know how to specify another condition to search for other sentences
' that don't write when it finds characters
If Not line.IndexOf("/**") = -1 Then
komentar = True
End If
If komentar Then
If Not line.IndexOf("*/") = -1 Then
komentar = False
End If
Else
ListBox1.Items.Add(line)
End If
This code has the task of opening a file through a dialog box. Before it is loaded into the list box, it searches for text that begins with / ** and ends with * / and this text is not written to the listbox. I still need the listbox not to include sentences starting with so I used if not line.indexof and comment True or False.
I misunderstood your request at first but Andrew Morton's comment made me realise what you were actually asking for. Here's how I might implement it:
Dim terminatorsByInitiator As New Dictionary(Of String, String) From {{"<!--", "-->"},
{"/**", "*/"}}
Dim isComment = False
Dim terminator As String
For Each line In File.ReadAllLines(openfile.FileName, Encoding.Default)
If isComment Then
'Check whether we are at the end of the comment.
If line.EndsWith(terminator) Then
isComment = False
End If
Else
'Get the comment initiator at the start of the line, if there is one.
Dim initiator = terminatorsByInitiator.Keys.FirstOrDefault(Function(s) line.StartsWith(s))
If initiator Is Nothing Then
'This line does not initiate a comment so add it to the list.
ListBox1.Items.Add(line)
Else
'This line does initiate a comment.
isComment = True
'Remember which comment terminator to look for.
terminator = terminatorsByInitiator(initiator)
End If
End If
Next
The use of the Dictionary ensures that you always look for the right comment terminator based on the initiator that was found.

Indent RTF Text in RichTextBox without losing the RTF style

I'd want to indent RTF text in a RichTextBox without losing the RTF style.
Dim Alinea As String = " "
Private Sub Indent_Click(sender As Object, e As EventArgs) Handles Indent.Click
Try
Dim Output As String = Nothing
Dim Split() As String = RichTextBox1.Lines
For i = 0 To Split.Length - 1
Output = String.Concat(Output, Split(i).Insert(0, Alinea), If(Not i = Split.Length - 1, vbNewLine, Nothing))
Next
RichTextBox1.Text = Output
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
The previous code works, but it returns the text without any style.
I'd just like to add Alinea on all beginnings of line of the RichTextBox text.
I've tried to use the RichTextBox1.Rtf property, but it shows a MsgBox saying "File format not valid".
Instead of using RichTextBox1.Lines, use RichTextBox1.Rtf.
RichTextBox1.Rtf = RichTextBox1.Rtf.Replace(vbCrLf, vbCrLf & vbTab)
This works, but you may want to key on something like \par or \par & vbcrlf to adhere more to the rtf standard.
RichTextBox1.Rtf = RichTextBox1.Rtf.Replace("\par" & vbCrLf, "\par" & vbCrLf & vbTab)
"It is left as an exercise to the reader" to make it work on the first line and for any whitespace character following "\par". (I always hated that phrase.)

Using a swear word filter with InStr in Visual Basic 2012

I want to compare the IF argument to a string array. The user will try to put in a teamname into a textbox, if the user uses a swear word anywhere within that textbox, it will display an error message and clear the textbox. If the user has not sworn, it will register the teamname and carry on with the program (As can be seen in the 2nd IF statement). I have tried to get this code to work for a week now and cannot get it to work.
Private Sub SelectionButtonEnter_Click(sender As Object, e As EventArgs) Handles SelectionButtonEnter.Click
Dim HasSworn As Boolean = False
Dim swears() As String = {"Fuck", "fuck", "Shit", "shit", "Shite", "shite", "Dick", "dick", "Pussy", "pussy", "Piss", "piss", "Vagina", "vagina", "Faggot", "faggot"} 'Declare potential swear words the kids can use
For Each swear As String In swears
If InStr(SelectionTextBoxTeamName.Text, swear) > 0 Then
SelectionTextBoxTeamName.Clear() 'Clear the textbox
MessageBox.Show("Remember ... You can be disqualified, raise your hand and Blair will set up the program for you again") 'Let the user know they have entered a swear word and ask them to select another team name
End If
If Not InStr(SelectionTextBoxTeamName.Text, swear) > 0 Then
Timer1.Enabled = True 'Enable timer 1 for the learn box
Timer3ForSelection.Enabled = True 'Enable this timer to show the learn button
TeamName = SelectionTextBoxTeamName.Text() 'Once this button has been pressed, store the content of that textbox in a the TeamName string
SelectionLabelTeamName.Text = "Welcome " & SelectionTextBoxTeamName.Text & " Please click 'Learn' in the box below to begin" 'Display the contents of the string along with other text here
SelectionLabelTeamNameTL.Text() = "Team Name: " & TeamName 'Display the contents of the string along with other text here
SelectionTextBoxTeamName.BackColor = Color.Green 'Have the back color of the box set to green
SelectionTextBoxTeamName.Enabled = False 'Do not allow the user/users enter another team name
End If
Next 'A next must be declared in a for each statement
End Sub
Thanks in advance.
I don't think I'd approach it that way; if the user types f**kyou, your code wouldn't catch it. How about this instead:
In your code:
If ContainsBannedWord(SelectionTextBoxTeamName.Text) Then
Msgbox "Hold out your hand, bad person. SlapSlapSlap"
Else
Msgbox "Good boy!"
End if
Function ContainsBannedWord(sInput As String) As Boolean
Dim aBannedWords(1 To 5) As String
Dim x As Long
' Make all the banned words capitalized
aBannedWords(1) = "BANNED1"
aBannedWords(2) = "BANNED2"
aBannedWords(3) = "BANNED3"
aBannedWords(4) = "BANNED4"
aBannedWords(5) = "BANNED5"
For x = LBound(aBannedWords) To UBound(aBannedWords)
If InStr(UCase(sInput), aBannedWords(x)) > 0 Then
ContainsBannedWord = True
Exit Function
End If
Next
ContainsBannedWord = False
End Function

VBA StringBetween

i have a little problem.
I want to get the Version Number from my Site, which is stored like that:
<version>1</version>
How can i get the Number between that 2 Strings into a variable?
Im completly new to vba and just need it for a simple check.
Thanks
//EDIT:
Tried now:
Data = .responseText
version = Replace(Replace(Data, "<version>", vbNullString), "</version>", vbNullString)
If version > Cells(2, 3) Then
strTitle = "new version"
strPrompt = "new version available"
iRet = MsgBox(strPrompt, vbOKOnly + vbExclamation, strTitle)
End If
In the Cell there is a "1" also on the site a:
<version>1</version>
But everytime i run it the msgbox pop up..
I just testewd it. The version aswell the cell output = 1. DOnt know why it wont work then
in VBA:
Dim Data As String, cleanData As String
Data = "<version>1</version>"
cleanData = Replace(Replace(Data, "<version>", vbNullString), "</version>", vbNullString)
In Excel where B57 is the text "1":
LEFT(RIGHT(B57, LEN(B57) - FIND(">",B57)), 1)
... Given that you know it will be surrounded by those 2 words specifically, you oculd use something like:
MyVersion = Replace(Replace(x, "<version>", ""), "</version>", "")
If this is already stored in a string, then a simple answer might be:
replace(
replace(myString,"<version>","")
,"</version>","")
Basically, just replace the first half of your string with nothing, then replace the second half with nothing, leaving just the number.

vb.net console application only works once

I'm just learning to code in vb.net and am currently messing around with VB.net console applications. I can't for the life of me figure something out. It's probably been asked before on here, but I can't find anything by searching. I just coded a simple "check if Y or N was entered. If y/n was entered, display 'you have entered y/n'" program, and it works fine the first time. However, after the first entry I can't get the process to repeat. All I get back is blank space. For example, if i enter y, I'll get the corresponding message. however, if after that I enter n I get nothing back.
here's the code:
Module Module1
Sub Main()
Console.Title = "Hello"
Console.WriteLine("Y or N")
Dim line As String
line = Console.ReadLine()
Do Until line = "exit"
If line = "y" Then
Console.WriteLine("you have chosen y")
Console.ReadLine()
ElseIf line = "n" Then
Console.WriteLine("you have chosen n")
Console.ReadLine()
End If
line = ""
Loop
End Sub
End Module
I'm assuming the answer's super simple, but I can't seem to figure it out or fin the answer online.
Thanks for the help.
You have to store the value of Console.ReadLine() in the Line string.
Module Module1
Sub Main()
Console.Title = "Hello"
Console.WriteLine("Y or N")
Dim line As String
line = Console.ReadLine()
Do Until line = "exit"
If line = "y" Then
Console.WriteLine("you have chosen y")
ElseIf line = "n" Then
Console.WriteLine("you have chosen n")
End If
line = Console.ReadLine()
Loop
End Sub
End Module
you need to assign Console.Readline() to line in your Do Loop:
Do Until line = "exit"
If line = "y" Then
Console.WriteLine("you have chosen y")
ElseIf line = "n" Then
Console.WriteLine("you have chosen n")
End If
line = Console.ReadLine()
Loop
This line is your problem:
line = ""
You're reading the console but not assigning that to your variable.
Here's how it should be:
Do Until line = "exit"
If line = "y" Then
Console.WriteLine("you have chosen y")
ElseIf line = "n" Then
Console.WriteLine("you have chosen n")
End If
line = Console.ReadLine()
Loop
You need to re-assign line to whatever the next line read from the console is, like so:
Module Module1
Sub Main()
Console.Title = "Hello"
Console.WriteLine("Y or N")
Dim line As String
line = Console.ReadLine()
Do Until line = "exit"
If line = "y" Then
Console.WriteLine("you have chosen y")
ElseIf line = "n" Then
Console.WriteLine("you have chosen n")
End If
line = Console.ReadLine() ''here
Loop
End Sub
End Module
Inside your loop, you're doing Console.ReadLine() without doing anything with the value, and you're then doing line = "". Your loop is going around infinitely with an empty line, and ignoring the user input.
Remove the two lines Console.ReadLine(), and then replace line = "" with line = Console.ReadLine().