tOGGLE cASE For Textbox? - vb.net

How can I add tOGGLE cASE to textboxes, for example, I click a button and it changes the text in a textbox to tOGGLE cASE (hello -> hELLO), basically it takes first letter and lower cases it and the rest upper cases it.

Here is a method using .NET Culture functions to first convert to Title Case and then invert the case to your "tOGGLE cASE"
Private Sub btn_ConvertTotOGGLEcASE_Click(sender As Object, e As EventArgs) Handles btn_ConvertTotOGGLEcASE.Click
'Get the current value of the textbox
Dim MyText As String = MyTextBox.Text
'Convert it to Title Case using built in .NET tools
Dim MyTextInfo As System.Globalization.TextInfo = New System.Globalization.CultureInfo("en-US", False).TextInfo
MyText = MyTextInfo.ToTitleCase(MyText)
'Then invert the case of all the characters
Dim InvertedText As Char() = MyText.Select(Function(c) If(Char.IsLetter(c), If(Char.IsUpper(c), Char.ToLower(c), Char.ToUpper(c)), c)).ToArray()
'Finally convert it back to a string
MyTextBox.Text = New String(InvertedText)
End Sub

You can split you string in an array, iterate in the array with lcase(mid(string,1,1) & ucase(mid(string,2, len(string)-1)), and recompose your array in a string
Public function ToogleText(myStr as string) as string
dim str() as string
str = split(myStr," ")
dim toogleStr as string
toogleStr = ""
for each substr as string in str
toogleStr = toogleStr & lcase(mid(substr,1,1)) & ucase(mid(substr, 2,len(substr)-1)) & " "
next substr
if len(toogleStr) > 0 then
ToogleText = mid(toogleStr,1,len(toogleStr)-1)
else
ToogleText =""
end if
end function

Related

Find all instances of a word in a string and display them in textbox (vb.net)

I have a string filled with the contents of a textbox (pretty large).
I want to search through it and display all occurances of this word. In addition I need the searchresult to display some charachters in the string before and after the actual searchterm to get the context for the word.
The code below is part of a code that takes keywords from a listbox one by one using For Each. The code displays the first occurance of a word together with the characters in front and after the word - and stop there. It will also display "no Match for: searched word" if not found.
As stated in the subject of this question - I need it to search the whole string and display all matches for a particular word together with the surrounding characters.
Where = InStr(txtScrape.Text, Search)
If Where <> 0 Then
txtScrape.Focus()
txtScrape.SelectionStart = Where - 10
txtScrape.SelectionLength = Where + 50
Result = txtScrape.SelectedText
AllResults = AllResults + Result
Else
AllResults = AllResults + "No Match for: " & item
End If
I recommend that you can split the string into long sentences by special symbols, such as , : ? .
Split(Char[])
You can refer to the following code.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
RichTextBox1.Text = ""
Dim Index As Integer
Dim longStr() As String
Dim str = TextBox3.Text
longStr = TextBox1.Text.Split(New Char() {CChar(":"), CChar(","), CChar("."), CChar("?"), CChar("!")})
Index = 0
For Each TheStr In longStr
If TheStr.Contains(str) Then
RichTextBox1.AppendText(longStr(Index) & vbCrLf)
End If
Index = Index + 1
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "....."
End Sub
End Class
Result:
Try like this:
Dim ArrStr() As String
Dim Index As Integer
Dim TheStr As String
Dim MatchFound As Boolean
MatchFound = False
ArrStr = Split(txtScrape.text," ")
Index = 1
For Each TheStr In ArrStr
If TheStr = Search Then
Console.WriteLine(Index)
MatchFound = True
End If
Index = Index + 1
Next
Console.WriteLine(MatchFound)
Inside the If statement you will get the index there. And MatchFound is the Boolean value if match found.

English sentence as input and translates it into Textese

working on a project where I input a text file and write a sentence in an "English" text box and with a click of a button the program translates it into "textese" type of language. It will only change words listed in the text file and ignore everything else. Below is the code I have, and wondering what is going wrong, it fails to run and highlights my If Then statement, so I think my issue is there but not sure what. A few examples of what is in the text file and how they are ordered / separated by a comma.
anyone,ne1
are,r
ate,8
band,b&
be,b
before,b4
busy,bz
computer,puter
Public Class frmTextese
Private Sub btnTranslate_Click(sender As Object, e As EventArgs) Handles btnTranslate.Click
Dim inputData() As String = IO.File.ReadAllLines("Textese.txt")
Dim english As Integer = 0
Dim englishSentence As String = txtEnglish.Text
Dim result() As String = englishSentence.Split(" "c)
Do While english < (result.Length - 1)
Dim line As String
Dim data() As String
Dim englishArray As String
Dim texteseArray As String
For i As Integer = 0 To (inputData.Length - 1)
line = inputData(i)
data = line.Split(","c)
englishArray = line.Split(","c)(0)
texteseArray = line.Split(","c)(1)
If result(i).StartsWith(englishArray(i)) Then
If englishArray(i).Equals(texteseArray(i)) Then
result(i) = texteseArray(i)
End If
End If
txtTextese.Text = result(i)
Next
Loop
End Sub
End Class
You only need to compare result(i) against englishArray. Also, your while loop was an endless loop. When searching the textese array, once you find a match, you can quit searching and go on to the next english word. Finally, you should take care to use variable names that describe their purpose.
Look at this code (untested). I made the string comparison case insensitive, but that is not required.
Private Sub btnTranslate_Click(sender As Object, e As EventArgs) Handles btnTranslate.Click
'Get all the textese definitions
Dim inputData() As String = IO.File.ReadAllLines("Textese.txt")
Dim english As Integer = 0
Dim englishSentence As String = txtEnglish.Text
Dim result() As String = englishSentence.Split(" "c)
Do While english < (result.Length - 1)
Dim line As String
Dim data() As String
Dim englishArray As String
Dim texteseArray As String
For i As Integer = 0 To (inputData.Length - 1)
'Split the textese entry into two parts
line = inputData(i)
data = line.Split(","c)
englishArray = data(0)
texteseArray = data(1)
'Compare the word in the english sentence against the word in the textese array
'using a case insensitive comparison (not required)
If result(i).Equals(englishArray, StringComparison.CurrentCultureIgnoreCase) Then
'Replace the word in the sentence with its textese version
result(i) = texteseArray
'If we found the word, there is no need to continue searching, so
'skip to the next word in the english sentence
Exit For
End If
Next
'Increment the loop counter to avoid an endless loop
english = english + 1
Loop
'Take the elements of the result array and join them together, separated by spaces
txtTextese.Text = String.Join(" ", result)
End Sub

Creating a text translator with text file

I'm trying to have this program take a normal English sentence in one text box and with the click of a button convert it into a textese sentence.
Any word that can be shortened will be replaced with the words in a text file.
An example of a line in the text file is
anyone,ne1
There are 52 lines (replacement words).
What would be the best way to approach this problem? Is a nested loop possibly a good route to take?
I don't have much experience and trying to learn the language more so open to trying all methods.
Below is what I have so far before I begin the coding process as I'm not really sure where to head from here. Only the words that are found within the text file will be replaced, so I think I'd use an If/Else statement that would ignore and leave any words not found alone.
Public Class frmTextese
Dim inputData() As String = IO.File.ReadAllLines("Textese.txt")
Private Sub btnTranslate_Click(sender As Object, e As EventArgs) Handles btnTranslate.Click
Dim english As Integer = 0
Dim englishSentence As String = txtEnglish.Text
Dim result() As String
result = englishSentence.Split(englishSentence)
Dim line As String
Dim data() As String
For i As Integer = 0 To (inputData.Length - 1)
line = inputData(i)
data = line.Split(" "c)
Next
'txtTextese.Text =
End Sub
End Class
An image of what I am trying to achieve:
Try this and have a look at the comments to get an idea of what is happening. I should add that strictly speaking, this site isn't a code writing service, but I can't help it sometimes. This also takes into account capitalization of words and common punctuation after the words. It's a bit quick and dirty, but for the basics, it seems to work ok.
Public Class frmTextese
'create a new empty dictionary where we'll add the pairs of english and
'textese words
Dim englishToTextese As New Dictionary(Of String, String)
Private Sub frmTextese_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'load textese dictionary to an array
Dim filedata() As String = IO.File.ReadAllLines("k:\textese.txt")
'Split each line into its elements and add to the dictionary
For Each item As String In filedata
Dim splitstring() As String = item.Split(","c)
englishToTextese.Add(splitstring(0), splitstring(1))
Next
End Sub
Private Function IsCapitalized(word As String) As Boolean
'If the first character in the word is upper case then return true
'else return false
If Mid(word, 1, 1) = Mid(word, 1, 1).ToUpper Then
Return True
Else
Return False
End If
End Function
Private Function GetPunctuation(word As String) As Tuple(Of String, String)
'If the last character in the word is a punctuation mark, return a pair of values which are :
'the word without puctuation, and the punctuation. Else
'return the word and an empty string
Dim result As Tuple(Of String, String)
If "!':;?/.,".Contains(word.Last()) Then
result = New Tuple(Of String, String)(Mid(word, 1, word.Length - 1), word.Last)
Else
result = New Tuple(Of String, String)(word, "")
End If
Return result
End Function
Private Sub btnTranslate_Click(sender As Object, e As EventArgs) Handles btnTranslate.Click
Dim textese As String = ""
'split the text in the txtEnglish textbox into its component words including any punctuation
Dim words() As String = txtEnglish.Text.Split(" "c)
For Each englishWord As String In words
'get the word and any punctuation following it as a pair of items
'and store in punctResult
Dim punctResult As Tuple(Of String, String) = GetPunctuation(englishWord)
Dim texteseWord As String
'store the first item (the word) in englishWord
englishWord = punctResult.Item1
'store the secont item (the punctuation or a blank string) in punctuation
Dim punctuation As String = punctResult.Item2
'If the english word is in the dictionary
If englishToTextese.ContainsKey(englishWord.ToLower) Then
'get the textesevertion
texteseWord = englishToTextese(englishWord.ToLower)
'if the original english word was capiutalized, capitalize the textese word
If IsCapitalized(englishWord) Then
texteseWord = texteseWord.ToUpperInvariant
End If
'add the word to the textese sentence
textese = textese & texteseWord & punctuation & " "
Else
'if the word isn't in the dictionary, add the original english word and its original state
'of capitalization and punctuation to the textese sentence
textese = textese & englishWord & punctuation & " "
End If
Next
'store the new texteze sentence in the textbox
txtTextese.Text = textese
End Sub
End Class

Check if null VB.NET

having trouble working out why this won't check the textbox too as well as the selected color. If I do not put a color it flags up the "please enter fields" message, however if I do select a color but do not put anything in the name textbox then it carries on and just outputs a blank string in the msgbox.
Code is:
Dim newColor As Color
Dim userName As String
Dim notEnoughArguments As String = "Please fill out the fields"
'Click event for button
Private Sub enterBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enterBtn.Click
If (userName Is "") Then
MsgBox(notEnoughArguments)
ElseIf (userName Is "" And colorLtb.SelectedItem Is Nothing) Then
MsgBox(notEnoughArguments)
ElseIf (colorLtb.SelectedItem Is Nothing) Then
MsgBox(notEnoughArguments)
Else
userName = txt1.Text
Dim selectedColor As String = colorLtb.SelectedItem.ToString
newColor = Color.FromName(selectedColor)
Dim msgBoxText As String = "Hello " + txt1.Text + "." & vbCrLf + "Changing your color to " + selectedColor + "."
MsgBox(msgBoxText)
Me.BackColor = newColor
End If
End Sub
For strings (like your textbox contents) use String.IsNullOrWhitespace as a test. Also you want both arguments, right? So a single statement should do:
If String.IsNullOrEmpty(userName) OrElse colorLtb.SelectedItem Is Nothing Then
MessageBox.Show(notEnoughArguments)
Return
End If
The problem is that Dim userName As String means the variable has nothing at all and that is not the same as an empty string. I always declare strings and immediately set them to String.Empty to avoid null reference exceptions, but using String.IsNullOrEmpty is a clean and robust way to test the contents of a string variable.
Normally to test for equality in VB, you use a single = rather than Is
If (userName = "") Then
When testing for Nothing, you have to use Is
If (userName Is Nothing) Then
IsNullOrEmpty combines both tests. As the accepted answer suggests:
If (String.IsNullOrEmpty(userName)) Then

Flesch Readability Index in Visual Basic

I'm working on a program that is supposed to perform the calculations for the Flesch Readability Index. The program is supposed to read in a text file "Project7.txt", it's then supposed to display the text in a multi-line text box and perform the following calculations:
Count the number of words in the file.
Count the number of syllables in the file.
Count the number of sentences in the file (a sentence can be ended by a ".", "?", "!", or ":"
The program is then supposed to plug the values into the following formula and display the result in a label (label1).
206.835-85.6*(Number of syllables/Number of words) - 1.015*(Number of words/Number of sentences)
Here is the code I have written so far.
Option Strict On
Imports System.IO
Public Class Form1
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
End Sub
Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
Dim open As New OpenFileDialog
open.Filter = "text files |project7.txt|All file |*.*"
open.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
If open.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim selectedFileName As String = System.IO.Path.GetFileName(open.FileName)
If selectedFileName.ToLower = "project7.txt" Then
Dim doc As String = ""
Dim line As String
Using reader As New StreamReader(open.OpenFile)
While Not reader.EndOfStream
doc += reader.ReadLine
Console.WriteLine(line)
End While
Dim text = File.ReadAllText("Project7.txt")
Dim words = text.Split(" "c)
Dim wordCount = words.Length
Dim separators As Char() = {"."c, "!"c, "?"c, ":"c}
Dim sentences = text.Split(separators, StringSplitOptions.RemoveEmptyEntries)
Dim sentenceCount = sentences.Length
End Using
Else
MessageBox.Show("You cannot use that file!")
End If
End If
End Sub
Function CountSyllables(word As String) As Integer
word = word.ToLower()
Dim dipthongs = {"oo", "ou", "ie", "oi", "ea", "ee", _
"eu", "ai", "ua", "ue", "au", "io"}
For Each dipthong In dipthongs
word = word.Replace(dipthong, dipthong(0))
Next
Dim vowels = "aeiou"
Dim vowelCount = 0
For Each c In word
If vowels.IndexOf(c) >= 0 Then vowelCount += 1
Next
Return vowelCount
End Function
End Class
Any suggestions are appreciated. Thanks in advance for the help.
Is the code always reporting one more sentence than there actually is?
If so take a look at this from the String.Split method MSDN docs:
When the Split function encounters two delimiters in a row, or a
delimiter at the beginning or end of the string, it interprets them as
surrounding an empty string ("")...
I'm sure your last sentence ends with your sentence delimiter so what's happening is your assignment to sentences is getting an extra, empty array element. See for yourself by breakpointing the line after your assignment and hovering your mouse over sentences. Examine the contents of the array.
The fix is to call Split with the option to remove empty array values. To do that though you'll need to call the Split overload that takes an array of Char for the delimiters:
Replace this line:
Dim sentences = text.Split("."c, "!"c, "?"c, ":"c)
With this:
Dim separators As Char() = {"."c, "!"c, "?"c, ":"c}
Dim sentences = text.Split(separators, StringSplitOptions.RemoveEmptyEntries)
And you should be good.