Random Letter Choosing for Textbox - vb.net

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?

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

Related

How can I get ten numbers and displays the biggest and lowest one?

sorry I'm a newbie and I'm trying to write a program to get ten integers from user with a an Inputbox or Textbox and displays the biggest and lowest one in a label with visual basic. I'll be appreciated if you help me out with this.
Thank you. this is my solution. I don't know how to compare these ten numbers with each other.
Private Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click
Dim i, Container, Max, Numbers
Max = 0
i = 1
While (i <= 10)
Numbers = InputBox("please enter a number", "Enter a number")
Max = Numbers
Container = Container & " " & Numbers
i = i + 1
End While
lblresult.Text = Container
End Sub
conceptually speaking you should use a List(Of Integer) or List(Of Double), perform the loop 10 times adding the value into the list.
Suppose this is our list
Dim container As New List(Of Integer)
To get input
Dim userInput = ""
Dim input As Integer
userInput = InputBox("please enter a number", "Enter a number")
If Integer.TryParse(userInput, input) Then
container.Add(input)
End If
After the loop
Console.WriteLine($"Min: {container.Min()} Max: {container.Max()}")
Does this make sense to you ?
Edit, based on asking for Windows Forms example.
You could do the following instead of a InputBox, requires a label, a button and a TextBox.
Public Class MainForm
Private container As New List(Of Integer)
Private Sub CurrentInputTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) _
Handles CurrentInputTextBox.KeyPress
If Asc(e.KeyChar) <> 8 Then
If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
e.Handled = True
End If
End If
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
CurrentLabel.Text = "Enter number 1"
End Sub
Private Sub ContinueButton_Click(sender As Object, e As EventArgs) _
Handles ContinueButton.Click
If Not String.IsNullOrWhiteSpace(CurrentInputTextBox.Text) Then
container.Add(CInt(CurrentInputTextBox.Text))
CurrentLabel.Text = $"Enter number {container.Count + 1}"
If container.Count = 10 Then
ContinueButton.Enabled = False
CurrentLabel.Text =
$"Count: {container.Count} " &
$"Max: {container.Max()} " &
$"Min: {container.Min()}"
Else
ActiveControl = CurrentInputTextBox
CurrentInputTextBox.Text = ""
End If
End If
End Sub
End Class
I really didn't want to do your homework for you but I was afraid you might be hopelesly confused.
First let's go over your code. See comments
Private Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click
Dim i, Container, Max, Numbers 'Don't declare variables without an As clause
Max = 0 'Max is an object
i = 1 'i is and object
While i <= 10 'the parenthesis are unnecessary. You can't use <= 2 with an object
Numbers = InputBox("please enter a number", "Enter a number")
Max = Numbers
Container = Container & " " & Numbers 'Container is an object; you can't use & with an object
i = i + 1 'Again with the object i can't use +
End While
lblresult.Text = Container
End Sub
Now my approach.
I created a List(Of T) at the Form level so it can be seen from different procedures. The T stands for type. I could be a built in type or type you create by creating a Class.
The first click event fills the list with the inputted numbers. I used .TryParse to test if the input is a correct value. The first parameter is a string; the input from the user. The second parameter is a variable to hold the converted string. .TryParse is very clever. It returns True or False base on whether the input string can be converted to the correct type and it fills the second parameter with the converted value.
The second click event loops through the list building a string to display in Label1. Then we use methods available to List(Of T) to get the numbers you desire.
Private NumbersList As New List(Of Integer)
Private Sub FillNumberList_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As Integer
While i < 10
Dim input = InputBox("Please enter a whole number")
Dim inputInt As Integer
If Integer.TryParse(input, inputInt) Then
NumbersList.Add(inputInt)
i += 1 'We only increment i if the parse is succesful
End If
End While
MessageBox.Show("Finished Input")
End Sub
Private Sub DisplayResults_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label1.Text = "You input these numbers "
For Each num In NumbersList
Label1.Text &= $"{num}, "
Next
Label2.Text = $"The largest number is {NumbersList.Max}"
Label3.Text = $"The smallest number is {NumbersList.Min}"
End Sub

scan multiple barcode in one textbox vb.net

For example i got two barcode, so i scan the two barcode at the textbox, it appear AVM12323AVM44454..
But I want it to be displayed in textbox for example like AVM12323, AVM44454. where there is a "," comma between the two code.
Previously i'm only tested for scan one barcode only in the textbox. So now i'm trying to scan more than one barcode in one textbox.
i have been looking the few example but not success.
Private Sub TextBoxMulti_TextChanged(sender As Object, e As EventArgs) Handles TextBoxMulti.TextChanged
Dim selectedMultiArrayScan As String()
Dim selectedMultiScan As String = ""
selectedMultiScan = TextBoxMulti.Text & ","
selectedMultiArrayScan = selectedMultiScan.Split(",")
End Sub
Private Sub MultiScan_Click(sender As Object, e As EventArgs) Handles MultiScan.Click
For Each stateName As String In selectedMultiArrayScan
//some query for each scanned item
Next stateName
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Enabled = False ' Fire only once
If TextBox1.Text.Trim <> "" Then TextBox1.Text += ", "
End Sub
Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
Timer1.Enabled = True
End Sub
Read the textbox with the event TextChanged like this:
If TextBox1.Text.Length >3 Then
Dim txt As String = Label1.Text + ", " + TextBox1.Text
If txt.Length = 6 Then txt = txt.Remove(0, 2)
Label1.Text = txt
'if you want to erase the text in TextBox1
TextBox1.SelectionStart = 0
TextBox1.SelectionLength = Label1.Text.Length
Try
End If
I use this code and it show the Barcodes in a label, you can replace the label to show in the textBox. Also, my Barcodes lenght always have 4 Alphanumeric characters. For you, Replace the If TextBox1.Text.Length >3 Then (3 to 8) to detect where a Barcode is readed.

messagebox appeared second time

In my code, when TextBox3 does not have any value, it must show a notice in a MsgBox to enter a value in TextBox1
But when I run it the MsgBoxnotice appears twice in the screen when it should show only once.
Here is my code:
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If TextBox3.Text = Nothing Then
TextBox1.Clear()
MsgBox("Enter Number to Textbox1")
Else
Dim digit As Integer = CInt(TextBox3.Text)
If TextBox1.TextLength = digit Then
Dim fields() As String = ListBox1.Text.Split(";")
Dim idx As Integer = ListBox1.FindString(TextBox1.Text)
If idx <> -1 Then
ListBox1.SelectedIndex = idx
ListBox1.SelectedIndex.ToString(fields(0))
ListBox2.Items.Add(Now() + Space(1) + ListBox1.Text.Substring(0, 13))
PrintDocument1.Print()
Else
TextBox1.Clear()
End If
End If
End If
End Sub
The Issue here is that the event handler gets triggered another time because clearing the textbox1 equals the textbox1_changed event handler to go of. You could as well just disable the textbox till the textbox3 is not nothing anymore.
or a quick solution would be aswell
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If not TextBox1.Text = Nothing AndAlso TextBox3.text = Nothing Then
TextBox1.Clear()
MsgBox("Enter Number to Textbox1")
.............
You are using the wrong event. Textchanged triggers when you clear the textbox as well resulting in two messageboxes.
Use LostFocus instead
Here is the solution,
Public Class Form1
Dim message as boolean = true
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If TextBox3.Text = Nothing Then
If message Then 'show the message as true
message = False 'set the message false for textbox_changed not appear again
Textbox1.Clear()
message = True 'set the message true for next time textbox change appear again
MsgBox("Enter Number to Textbox3")
End If
Else
Dim digit As Integer = CInt(TextBox3.Text)
If TextBox1.TextLength = digit Then
Dim fields() As String = ListBox1.Text.Split(";")
Dim idx As Integer = ListBox1.FindString(TextBox1.Text)
If idx <> -1 Then
ListBox1.SelectedIndex = idx
ListBox1.SelectedIndex.ToString(fields(0))
ListBox2.Items.Add(Now() + Space(1) + ListBox1.Text.Substring(0, 13))
PrintDocument1.Print()
Else
TextBox1.Clear()
End If
End If
End If
End Sub

reading bits with for loop, I get 7 bits instead of 8, what's the wrong with this code

OutPuts:
TextBox 2 : FalseTrueTrueTrueTrueTrueFalseFalse
TextBox 3 : 1111100
My problem is why is that the first boolean of "TextBox 2" is "False" and the first integer of "TextBox 3" is 1 ? "TextBox 2" has 8 booleans while "TextBox 3" only has 7 bits. And apparently, in "TextBox 3, the first bit is not there. Where have I done wrong .. ? commentary has provided in the code. Please shed some light here.
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim array() As Byte = File.ReadAllBytes("D:\binfile.bin")
Using memory As MemoryStream = New MemoryStream(array)
Using reader As BinaryReader = New BinaryReader(memory)
ba1 = New BitArray(array)
Dim bit_set As Integer
For i As Integer = 0 To 7
'to view all 8 bits in boolean format
TextBox2.Text = TextBox2.Text & ba1.Get(i)
If ba1.Get(i) = False Then
boolean2bits = 0
'End If
ElseIf ba1.Get(i) = True Then
boolean2bits = 1
End If
'to collect all 8 bits in integer format
bit_set = bit_set & boolean2bits
If (i = 7) Then
Exit For
End If
Next
'to view collected bits in the text box
TextBox3.Text = bit_set
End Using
End Using
End Sub
Simply because you are assigning the value 01111100 to the integer variable bit_set. But of course, as an integer, that leading 0 is not significant, so it gets stripped out automatically, and gets simplified to simply 1111100, because it is the same number after all.
If you don't want to lose the leading zero for display purposes, then you probably don't want bit_set to be of type Integer. Just declare at as a Dim bit_set As String, and the leading zero will not disappear.
Looks like you're making some progress towards your end goal Pretty_Girl.
Here are some snippets to take in and digest:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
ba1 = New BitArray(File.ReadAllBytes("D:\binfile.bin"))
Dim bits As New List(Of String)
Dim bools As New List(Of String)
For i As Integer = 0 To 7
bools.Add(ba1.Get(i).ToString)
bits.Add(If(ba1.Get(i), "1", "0"))
Next
'to view collected bits/bools in the text box
TextBox2.Text = String.Join(",", bools.ToArray)
TextBox3.Text = String.Join("", bits.ToArray)
End Sub
Alternate Version 2:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
ba1 = New BitArray(File.ReadAllBytes("D:\binfile.bin"))
TextBox2.Clear()
TextBox3.Clear()
For i As Integer = 0 To 7
TextBox2.AppendText(ba1.Get(i).ToString & ",")
TextBox3.AppendText(If(ba1.Get(i), "1", "0"))
Next
End Sub
Alternate Version 3:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
ba1 = New BitArray(File.ReadAllBytes("D:\binfile.bin"))
Dim bits As New System.Text.StringBuilder
Dim bools As New System.Text.StringBuilder
For i As Integer = 0 To 7
bools.Append(ba1.Get(i).ToString & ",")
bits.Append(If(ba1.Get(i), "1", "0"))
Next
TextBox2.Text = bools.ToString
TextBox3.Text = bits.ToString
End Sub

Selecting text in vb

I want to select a word to another word in a text box in vb.net with everything between them highlighted.
an example is
I went to the beach, had a pinic with my family and then went home at 6 o clock.
The starting word to be had and the end word being home and everything highlighted in between.
I have already used a little bit of code
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim a As String
Dim b As String
a = TextBox2.Text 'means what ever is in textbox2 string to the location where "a" is
b = InStr(RichTextBox1.Text, a)
If b Then
RichTextBox1.Focus()
RichTextBox1.SelectionStart = b - 1
RichTextBox1.SelectionLength = Len(a)
but its not exactly what i want it to do.
Addition to this was using RegEx Function as shown below
'gets rid of the enter line break eg <enter> command no new lines
Dim content As String = Replace(TextBox1.Text, Global.Microsoft.VisualBasic.ChrW(10), Nothing)
'searches for this tag in the brackets between ".*" will be the contents
Dim Regex As New Regex("<div.*class=""answer_text"".*id=editorText"".*""")
'Show the string
For Each M As Match In Regex.Matches(content)
'This will get the values, there are 3 atm meta.name des and content
Dim Description As String = M.Value.Split("""").GetValue(3)
'displays the content in the label
TextBox3.Text = "" & Description
Next
This will select everything between startWord and endWord excluding them both
Dim startWord As String = "had"
Dim endWord As String = "home"
Dim index As Integer = richTextBox1.Text.IndexOf(startWord)
richTextBox1.[Select](index + startWord.Length, richTextBox1.Text.IndexOf(endWord) - index - startWord.Length)
Here's a solution involving two TextBox controls:
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
Dim a As String
Dim b As String
Dim index_a As Integer
Dim index_b As Integer
a = TextBox1.Text
b = TextBox2.Text
index_a = InStr(RichTextBox1.Text, a)
index_b = InStr(RichTextBox1.Text, b)
If index_a And index_b Then
RichTextBox1.Focus()
RichTextBox1.SelectionStart = index_a - 1
RichTextBox1.SelectionLength = (index_b - index_a) + Len(b)
End If
End Sub
TextBox1 contains the first word, TextBox2 contains the second word. When clicking the button, it will highlight from the first word to the end of the second word.