Square of value VB 2010 - vb.net

How do i write a program that square the value in the textbox and add it in the listbox each time the button is click. from 0 to 100000. thanks
This is what I have so far
Public Class frmSquare
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
Dim x As Double
Dim number As Double = txtNumber.Text
lstSquare.Items.Add(number * number)
x = number
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
Me.Close()
End Sub
End Class

Here's another way to do this, hope this helps:
If IsNumeric(TextBox1.Text) Then
Dim dbl As Double = CDbl(TextBox1.Text)
dbl *= dbl
ListBox2.Items.Add(dbl)
Else
MessageBox.Show("Value entered is not a Number")
End If

Related

If my counter ends at 10 at textbox1(TB1) the random number won't show in my textbox2(TB2)

If my counter ends at 10 at textbox1 (TB1) the random number won't show in my textbox2(TB2)
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TB1.Text = 10 Then
Dim num1 As Integer
Dim randomnumber As New Random
num1 = randomnumber.Next(100, 201)
TB2.Text = num1
End If
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
TB1.Text = TB1.Text + 1
If TB1.Text = 10 Then
Timer1.Enabled = False
End If
End Sub
End Class
Use a numeric variable to keep the count, not the TextBox. The TextBox.Text property is of type String, not Integer!
Putting Option Strict On at the top of your code will enforce using proper types. VB by default allows you to loosely convert between integer and string, which is poor practice.
Option Strict On
Public Class Form1
Private count As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
count = 0
Timer1.Enabled = True
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If count = 10 Then
Dim randomnumber As New Random()
Dim num1 = randomnumber.Next(100, 201)
TB2.Text = num1.ToString()
End If
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
count += 1
Timer1.Enabled = count <> 10
TB1.Text = count.ToString()
End Sub
End Class

Use a textbox to store a numeric counter in VB.Net

I want to count with a TextBox.
Here is my code:
Public Class Form1
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
OrainsProgressBar1.Increment(1)
If OrainsProgressBar1.Value = 100 Then
Timer3.Start()
Timer1.Stop()
End If
End Sub
Private Sub OrainsTheme1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrainsTheme1.Click
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Timer1.Start()
Timer2.Start()
End Sub
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
OrainsProgressBar1.Increment(-1)
If OrainsProgressBar1.Value = 0 Then
Timer1.Start()
Timer3.Stop()
End If
End Sub
Private Sub OrainsButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrainsButton1.Click
OrainsTextBox1.Text += 100
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
OrainsTextBox1.Text += 1
End Sub
End Class
But I have an error with OrainsTextBox1.Text += 1. VB says:
'Conversion from string "" to type 'Double' is not valid.'
What is the problem?
In the .Net world, the data types of things matter a lot. Strings (like the .Text property) are NOT numbers. You need to convert. Even if someone only enters the digits 0-9 into the textbox, that's still a string of numeric characters, rather than a number. And what should happen if someone enters random text into that textbox that won't convert to a number type at all?
For this code, I suggest building a property, like this:
Private _orainsValue As Double
Public Property OrainsValue As Double
Get
Return _orainsValues
End Get
Set
_orainsValue = Value
OrainsTextBox1.Text = _orainsValue.ToString()
End Set
End Property
That will let you write code like this and have the expected result shown to the user:
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
OrainsVale += 1
End Sub
Note that it does mean you will want to mark the TextBox disabled,though, because this doesn't account for user data entry.
Instead of doing like this OrainsTextBox1.Text += 1
do like this OrainsTextBox1.Text = Val(OrainsTextBox1.Text) + 1
Because .Text is string. Which will append the 1s as "11111111"

Replace string if followed by any character visual basic

I am new to Visual Basic. I want to use the like operator in a textbox to change a character if it is followed by any other character. But it should be on the key-up event.
Anyone please help me: how I can make the following code work?
Public Class Form1
Dim myString As String
Dim sMatch As Boolean = myString Like "x?"
Private Sub TextBox1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
If sMatch = True Then
TextBox1.Text = TextBox1.Text.Replace(myString, "z")
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myString = "x"
End Sub
End Class
Try this and ask. Your variable myString was never getting it's value from the TextBox. You just set it to x in the load event.
Public Class Form1
Private Sub TextBox1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
If TextBox1.Text Like "x?" Then
TextBox1.Text = TextBox1.Text.Replace(TextBox1.Text, "z")
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "x"
End Sub
End Class

giving calculator input from keyboard keys without pressing calculator buttons

I created a VB.net calculator application.It works fine but say I want to add 5+7.Then as my application works I can press 5 from the keyboard as I gave the text of button with digit 5 as &5 but then if I press + from the keyboard it doesn't work.I have to press the + button in the calculator.
I think that's because my add button is designed to handle click event as btn_add_Click.Is there a way I can make this application to work so that without pressing the buttons in the calculator I can press the keys in keyboard and do the computation.Here's my code:
Imports System.Math
Public Class Form1
Private isFirstExist As Boolean
Private inputOperator As String
Private secondNum As Decimal
Private firstNum As Decimal
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtCalc.TextChanged
End Sub
Private Sub btn_zero_Click(sender As System.Object, e As System.EventArgs) Handles btn_zero.Click
removeFrontZero(0)
End Sub
Private Sub btn_one_Click(sender As System.Object, e As System.EventArgs) Handles btn_one.Click
removeFrontZero(1)
End Sub
Private Sub btn_two_Click(sender As System.Object, e As System.EventArgs) Handles btn_two.Click
removeFrontZero(2)
End Sub
Private Sub btn_clear_Click(sender As System.Object, e As System.EventArgs) Handles btn_clear.Click
txtCalc.Clear()
txtCalc.Text = "0"
End Sub
'Remove zero which is at the start
Public Sub removeFrontZero(ByVal digit As Integer)
If txtCalc.Text = "0" Then
txtCalc.Text = CStr(digit)
Else
txtCalc.Text &= digit
End If
End Sub
Private Sub btn_add_Click(sender As System.Object, e As System.EventArgs) Handles btn_add.Click
inputOperator = "+"
isFirst()
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
isFirstExist = False
End Sub
Private Sub btn_equal_Click(sender As System.Object, e As System.EventArgs) Handles btn_equal.Click
If isFirstExist Then
secondNum = CType(txtCalc.Text, Decimal)
End If
'Calculating the result
Dim result As Decimal = calculate(firstNum, secondNum, inputOperator)
txtCalc.Text = result.ToString()
isFirstExist = False
End Sub
Private Function calculate(ByVal num1 As Decimal, ByVal num2 As Decimal, ByVal inputOp As String) As Decimal
Dim output As Decimal
firstNum = num1
secondNum = num2
Select Case inputOp
Case "+"
output = num1 + num2
Case "-"
output = num1 - num2
Case "/"
Dim value As Decimal
Try
isFirst()
value = (num1 / num2)
Catch ex As DivideByZeroException
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK)
End Try
output = value
Case "*"
output = num1 * num2
Case "Mod"
output = (num1 Mod num2)
Case "^"
output = CDec(Math.Pow(num1, num2))
End Select
Return output
End Function
Private Sub isFirst()
If isFirstExist = False Then
firstNum = CType(txtCalc.Text, Decimal)
isFirstExist = True
txtCalc.Text = "0"
End If
End Sub
Assuming you're using the textbox to hold the users input, in the Textchanged or even KeyPressed handler trap the operator keys and run the function instead of displaying the operator.
Something like this:
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtCalc.KeyPress
If e.KeyChar = "+"c Then
inputOperator = "+"
isFirst()
e.Handled = True
End If
End Sub
To display the operator set handled to false.

'While Loop' and multiples of a variable

This is another school project, and I'm stumped, so PLEASE help.
I'm supposed to write a program that outputs the numbers 1 to 100, however there are 4 variables. When you enter a value for Variable 1 it takes the numbers that multiples of that number and changes it to the Variable Word that you type in.
The specific question I have is how do I get my variables to be multiples of that value?
This is all I have...thank you for your help.
Public Class Form1
Private Sub lblDiscription_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblDiscription.Click
End Sub
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub bntCounter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntCounter.Click
Dim intCounter As Integer = 0
Dim intLeftMultiple As Integer = 1
Dim intRightMultiple As Integer = 1
Dim strLeftWord As String
Dim strRightWord As String
While intCounter < 101
lstDisplay.Items.Add(CStr(intCounter))
intCounter += 1
End If
End While
End Sub
Private Sub lblMiddle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblMiddle.Click
End Sub
End Class
This is what my GUI Looks like.