Basically i want one textbox to do this:
TextBox5.Text = TextBox4.Text / TextBox2.Text
It works but if i don't try to input something to the textbox it doesn't update. How do i ?
You can handle both TextBox_TextChanged events, and try to parse the strings as doubles. If both succeed, then do the math and update the TextBox.
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
doMath()
End Sub
Private Sub TextBox4_TextChanged(sender As Object, e As EventArgs) Handles TextBox4.TextChanged
doMath()
End Sub
Private Sub doMath()
Dim tb2 As Double, tb4 As Double
If Double.TryParse(TextBox2.Text, tb2) AndAlso Double.TryParse(TextBox4.Text, tb4) Then
TextBox5.Text = (tb4 / tb2).ToString()
Else
TextBox5.Text = ""
End If
End Sub
The TextBox will be updated immediately with the quotient when both other TextBoxes are valid doubles.
Remember not to try to do math using strings. You may want to put Option Strict On at the top of your code file so the compiler tells you you can't do math with strings like this TextBox4.Text / TextBox2.Text
Related
I have a prank antivirus program I'm making for a friend. Part of the software requires "activation" to remove the "virus". I have 4 TextBoxes when I click the button I want all 4 TexBoxes to be checked for the text "0000". When I have one TextBox it works great, but I need all 4 boxes to get checked before the message box appears. I hope this makes sense. See image here
[Edit] I'm going to mention i'm a total noob at programming.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Text = "0000" Then
MsgBox("Registered")
Me.Hide()
End If
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
End Class
There are many ways to do what you want. Here's a very simple one which you can build on:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' check all the TextBoxes in the array. Return if one isn't valid
For Each textbox As TextBox In {TextBox1, TextBox2, TextBox3, TextBox4}
If textbox.Text <> "0000" Then
Return
End If
Next
' If all TextBox contains the valid string, this will appear
MsgBox("Registered")
Me.Hide()
End Sub
Have fun!
EDIT:
To have 4 different strings: just chain 4 checks. Like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' If all TextBox contains the valid string, this will appear
If TextBox1.Text = "0000" AndAlso TextBox2.Text = "1111" AndAlso TextBox3.Text = "2222" AndAlso TextBox4.Text = "3333" Then
MsgBox("Registered")
Me.Hide()
End If
End Sub
I've searched everywhere, I can't find a way.
I want to find a way to add text after every textbox line but I can't find a way to do it.
I have a textbox1 with:
example1
example2
example3
And so on...
and another textbox2 with #gmail.com
I want the textbox2 to be added to the end of every line in textbox1 like:
example1#gmail.com
example2#gmail.com
example3#gmail.com
And so on...
Any way to do it? Thanks in advance.
This solution is concise, and removes empty lines.
Private Function appendTextToOtherTextLines(textToAppend As String, otherText As String) As String
Return String.Join(Environment.NewLine, otherText.
Split(Environment.NewLine.ToArray(), StringSplitOptions.RemoveEmptyEntries).
Select(Function(s) s & textToAppend))
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox3.Text = appendTextToOtherTextLines(TextBox2.Text, TextBox1.Text)
End Sub
Here's your example working
And if you had an empty line, it is removed in the resulting string
Of course, you could overwrite the original textbox instead, but careful not to click the button twice!
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = appendTextToOtherTextLines(TextBox2.Text, TextBox1.Text)
End Sub
Other option is an event handler which will make this happen automatically when pressing enter at the end of a new line. This is only useful if you are actively entering the lines manually.
Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
If e.KeyCode = Keys.Enter Then
TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.Text.Length - 2) & TextBox2.Text & Environment.NewLine
TextBox1.SelectionStart = TextBox1.Text.Length
End If
End Sub
(this option requires some discipline when pressing enter)
This is full working code.Enjoy it!!!!
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Me.TextBox1.Lines.Length = 0 Then
MsgBox("Please enter some data in textbox1")
Exit Sub
End If
'---------------------------
Dim b(Me.TextBox1.Lines.Length - 1) As String
Dim i As Integer = 0
For Each a As String In Me.TextBox1.Lines
b(i) = a + Me.TextBox2.Text
i = i + 1
Next
'-----------------
Me.TextBox1.Clear()
Me.TextBox1.Lines = b
End Sub
i want to convert total minutes into days hours and minutes. i am new to vb and still trying to learn...
this is my code...
Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
TextBox1.Text = TextBox3.Text / 1440
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
TextBox3.Text = TextBox1.Text * 1440
TextBox7.Text = TextBox1.Text / 24
End Sub
Private Sub TextBox7_TextChanged(sender As Object, e As EventArgs) Handles TextBox7.TextChanged
TextBox1.Text = TextBox7.Text * 24
End Sub
When i run it i am having answers in decimal.. can anyone suggest me a proper way to do this? i do not want decimals.
It doesn't look like you understand the concept of variables, you don't need to have three different handlers to make this work, you can do it all in one handler.
You could do what you want arithmetically but in my opinion, an easier way is to use TimeSpan. It would look something like this:
'TextBox1 is where the user inputs the number of minutes
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim d = TimeSpan.FromMinutes(TextBox1.Text) 'creates a TimeSpan based on user input, this is also a variable creation
TextBox5.Text = d.ToString("dd\:hh\:mm") 'you can format it with a custom format
'Or you can access the elements of the TimeSpan like so
TextBox2.Text = d.Days
TextBox3.Text = d.Hours
TextBox4.Text = d.Minutes
End Sub
I have textbox1.text as the main textbox. Any text that is input into textbox1.text when the submit button is selected is supposed to input the text into the next available textbox which is textbox2.text, textbox3.text and textbox4.text. How can I tell vb.net that if textbox2.text is full then input the text into textbox3.text and if textbox3.text is full to input the text into textbox4.text. Here is where I got stuck. I'm still in the process of learning more about vb.net.
Private Sub SubBttn_Click(sender As Object, e As EventArgs) Handles SubBttn.Click
If TextBox1.Text.Trim.Length > 0 Then
TextBox2.Text = TextBox1.Text
If TextBox2.Text.Trim.Length > 0 Then
TextBox3.Text = TextBox1.Text
End If
End If
End Sub
More compact code to do the job
Private Sub btnsubmit_Click(sender As Object, e As EventArgs) Handles btnsubmit.Click
Dim LstTextBox As New List(Of TextBox) 'A list to contain text boxes excluding the main text box
LstTextBox.Add(TextBox2)
LstTextBox.Add(TextBox3)
LstTextBox.Add(TextBox4)
For Each Tbox As TextBox In LstTextBox
If String.IsNullOrWhiteSpace(Tbox.Text) = True Then 'If text box is empty or contains white spaces
Tbox.Text = TextBox1.Text 'Copies text from main text box
TextBox1.Clear() 'Clears main text box
Exit Sub
End If
Next
End Sub
You can compare the textbox's textlength with the textbox's max length, and if they match up, the next
Private Sub TextBox1_TextChanged(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If TextBox1.TextLength = TextBox1.MaxLength - 1 Then
TextBox2.Focus()
End If
End Sub
This will switch focus to textbox2 as soon as the last digit is typed.
If you want it in a button event, then do the same as above, but without the - 1
If you want to keep the text the same throughout, then do this:
Private Sub SubBttn_Click(sender As Object, e As EventArgs) Handles SubBttn.Click
If TextBox1.TextLength = TextBox1.MaxLength Then
TextBox2.Text = TextBox1.Text
TextBox2.Focus()
End If
End Sub
You would repeat the same action for each textbox, for example:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.TextLength = TextBox1.MaxLength Then
TextBox2.Text = TextBox1.Text
TextBox2.Focus()
End If
If TextBox2.TextLength = TextBox2.MaxLength Then
TextBox3.Text = TextBox2.Text
TextBox3.Focus()
End If
If TextBox3.TextLength = TextBox3.MaxLength Then
TextBox4.Text = TextBox3.Text
TextBox4.Focus()
End If
End Sub
Hope this helps!
Here's what I have:
1 Richtextbox where all the text goes
1 Textbox where a user should input a number
1 radiobutton which the user should check
Here's what I want to happen:
The user enters a number in the textbox (textbox1.text) and checks the checkbox (checkbox1.checked).
The program automatically multiplies "<ol>Item</ol>" times the number entered in the textbox (textbox1.text) and shows it in the richtextbox (richtextbox1.text).
Here's my code:
Private Sub Button2_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If RadioButton1.Checked = True Then
RichTextBox1.Text = "<ol>Item</ol>" * TextBox1.Text
End If
End Sub
Here's what i'm getting:
Conversion from string "<ol>Item</ol>" to type 'Double' is not valid.
What should I do to get rid of this error?
You need to explicitly convert string to Double:
Dim d As Double = Convert.ToDouble(TextBox1.Text)
Now you can use d in any calculation before showing it in RichTextBox1
EDIT:
By reading your question again, now I get a feeling that you may be asking something else. Until you clarify the question, here is the other solution where I feel you want to display a string the number of times entered in the textbox. Here is the code for it:
Dim sb As StringBuilder = New StringBuilder()
If RadioButton1.Checked = True Then
For index = 1 To Convert.ToInt32(TextBox1.Text)
sb.AppendLine("Your string here")
Next
RichTextBox1.Text = sb.ToString()
End If
You'll need to use a loop. I assume you're trying to just repeat that string. But don't forget to add verification that TextBox1.Text is actually a number. You could use IsNumeric(TextBox1.Text) for that.
Private Sub Button2_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If RadioButton1.Checked = True Then
Dim sb As New StringBuilder()
For i As Integer = 0 to CInt(TextBox1.Text) - 1
sb.Append("<ol>Item</ol>")
Next
RichTextBox1.Text = sb.ToString()
End If
End Sub
OR
Private Sub Button2_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If RadioButton1.Checked = True Then
RichTextBox1.Text = String.Join("", Enumerable.Repeat("<ol>Item</ol>", CInt(TextBox1.Text)))
End If
End Sub