Reputation: 55
So this code by Neethu Soman,
Dim input As String = TextBox2.Text '<--- this should be input "Hello I am Greg"
TextBox2.Text = "" '<--- clear the textbox to store output
Dim rnd As New Random '<---- generating new random number
For Each c As Char In input '<--- iterate through each character
If rnd.Next() Mod 2 = 0 Then
TextBox2.Text &= UCase(c) '<--- if true then print particular letter in upperCase
Else
TextBox2.Text &= LCase(c) '<--- if true then print particular letter in LowerCase
End If
Next
basically does what is supposed to, it converts random letter to lower, and upper case with a 50/50 chance. Although, that one problem is that is clears the text, and rewrites it in its new converted form, which is the problem. Is there a way to make this work without having to clear the text?
Upvotes: 0
Views: 273
Reputation:
Thanks for make a try with my answer, you can convert random letter to lower, Or upper case with a 50/50 chance using the following code without using TextBox2.Text = ""
Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
Dim rnd As New Random'<--- Generating random number
If rnd.Next() Mod 2 = 0 Then
e.KeyChar = UCase(e.KeyChar) '<--- if true then change key char to upperCase
Else
e.KeyChar &= LCase(e.KeyChar) '<--- if true then change key char to LowerCase
End If
End Sub
If you want to do it in button click means:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim randomString As String = ""
Dim rnd As New Random
For Each c As Char In TextBox2.Text
If rnd.Next() Mod 2 = 0 Then
randomString &= UCase(c)
Else
randomString &= LCase(c)
End If
Next
TextBox2.Text = randomString
End Sub
Upvotes: 1