Calculator for newbs - vb.net

I'm working on a calculator in which I want to get some numbers on a circle.
Private Sub Button6_Click(
ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button6.Click
Dim radius As Integer = TextBox13.Text
Dim diameter As Integer = TextBox14.Text
Dim length As Integer = TextBox15.Text
TextBox13.Text = diameter / 2
TextBox14.Text = radius * 2
TextBox15.Text = radius * 2 * Math.PI
TextBox15.Text = diameter * Math.PI
End Sub
That is the current code, but I'm experiencing a problem with "the number must be less than infinity".
Note: I'm a COMPLETE noob.

The error lies in the fact that you tried to assign an integer value of type string.
Use the method provided by struct TryParse integer, this will also run in any runtime exception FormatException bran and do not send the application.
Here's an example:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim diameter As Integer = 0
Dim radius As Integer = 0
Dim lenght As Integer = 0
If Integer.TryParse(Me.TextBox13.Text, diameter) Then
'your code
End If
If Integer.TryParse(Me.TextBox14.Text, radius) Then
'your code
End If
If Integer.TryParse(Me.TextBox15.Text, lenght) Then
'your code
End If
End Sub
More information about TryParse at this link:
http://msdn.microsoft.com/it-it/library/f02979c7.aspx
Bye

See if this works:
Dim radius As Integer = Integer.Parse(TextBox13.Text)
Dim diameter As Integer = Integer.Parse(TextBox14.Text)
Dim length As Integer = Integer.Parse(TextBox15.Text)

Related

dividing number without rounding it up and without decimal

I'm trying to divide input number and show the result without decimals and without rounding it up, so if I divide 80/50 I want to get 1, not 2 (1.6).
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim box1 As Decimal = TextBox1.Text()
TextBox6.Text = box1 / 50
End Sub
End Class
You can use Truncate Method of Decimal:
Dim result As Decimal = Decimal.Truncate(CDec(80 / 50))
Console.WriteLine("result : " & result.ToString)
You can use the \ operator to do integer division.
Dim result = 80 \ 50 'result is Integer with value 1

Make specific randomly generated codes with timer

I would like to make randomly generated codes when a timer is finished.
But I would like to use this code ABCDEFGHIJKLOMNOPQRSTUVWXYZ0123456789
on this format XXXX-XXXX-XXXX (replace X with a randomly generated number/letter)
And I would like to insert the code in this
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ProgressBar1.Increment(3)
If ProgressBar1.Value = 100 Then
TextBox1.Text = "replace textbox by code"
Timer1.Stop()
End If
End Sub
Here's a solution using LINQ.
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ProgressBar1.Increment(3)
If ProgressBar1.Value = 100 Then
Dim chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Dim random = New Random()
Dim num1 = New String(Enumerable.Repeat(chars, 4).Select(Function(s) s(random.Next(s.Length))).ToArray())
Dim num2 = New String(Enumerable.Repeat(chars, 4).Select(Function(s) s(random.Next(s.Length))).ToArray())
Dim num3 = New String(Enumerable.Repeat(chars, 4).Select(Function(s) s(random.Next(s.Length))).ToArray())
Dim result = num1 & "-" & num2 & "-" & num3
TextBox1.Text = result
Timer1.Stop()
End If
End Sub
With this, you'd just change your TextBox1.Text = to point to the getNewRandomString() function.
Private Function getNewRandomString() As String
Dim sb As New StringBuilder
For i = 1 To 3
For j = 1 To 4
sb.Append(getNewRandomLetter())
Next
sb.Append("-")
Next
Return sb.ToString.Trim("-")
End Function
Private Function getNewRandomLetter() As Char
Dim chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Return (chars(GetRandom(0, chars.Length - 1)))
End Function
Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
Static Generator As System.Random = New System.Random()
Return Generator.Next(Min, Max + 1)
End Function

Counter for multiplication game

Public Class Form1
' num1 and num2 now generate a number between 1 and 10
Dim num1 As Integer = CInt(Int((10 * Rnd()) + 1))
Dim counter As Integer
Dim num2 As Integer = CInt(Int((10 * Rnd()) + 1))
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
' Generates a new question everytime the button is clicked
num1 = CInt(Int((10 * Rnd()) + 1))
num2 = CInt(Int((10 * Rnd()) + 1))
'Displays question in textbox2
TextBox2.Text = num1 & "*" & num2
End Sub
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
If TextBox1.Text = num1 * num2 Then
' If they get the answer right, they recieve postive feedback
Label2.Text = "Correct!!11"
Else
' Any other answer results in negative feedback
Label2.Text = "Incorrect, sorry about
This multiplication game is functioning as it should. I just need a way for the user of the game to be able to see how many questions they have gotten wrong and right. I need to implement a counter, but most of the guides online have proven useless. I decided to just post the code and see if someone could help me.
You could declare 2 new variables to store wrong and correct question values.
Dim wrongQuestions as Integer = 0
Dim correctQuestions as Integer = 0
If TextBox1.Text = num1 * num2 Then
' If they get the answer right, they recieve postive feedback
Label2.Text = "Correct!!11"
correctQuestions += 1
Else
' Any other answer results in negative feedback
Label2.Text = "Incorrect, sorry about"
wrongQuestions += 1
Then use
msgbox("Correct Questions: " & correctQuestions & " Incorrect Questions: " & wrongQuestions )
Using Random and with user input error checking
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Me.AcceptButton = Button2
Button1.PerformClick()
End Sub
Dim prng As New Random
Dim answer As Integer
Dim incorrect As Integer = 0
Dim correct As Integer = 0
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Generates a new question everytime the button is clicked
Dim num1 As Integer = prng.Next(1, 11)
Dim num2 As Integer = prng.Next(1, 11)
TextBox2.Text = String.Format("{0} * {1}", num1, num2)
answer = num1 * num2
TextBox1.Text = ""
TextBox1.Select()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'check answer
Dim useranswer As Integer
If Integer.TryParse(TextBox1.Text, useranswer) Then
If useranswer = answer Then
correct += 1
Label2.Text = "Correct "
Else
incorrect += 1
Label2.Text = "Incorrect"
End If
Label2.Text &= String.Format(" Totals: correct = {0}, incorrect = {1}", _
correct, incorrect)
Button1.PerformClick()
Else
Label2.Text = "answer should be numeric"
End If
End Sub

Making a string that adapts itself to the text inside it

I'm making a program to solve second grade equations (e.g. 2x²+2x+2) and I was trying to have the user input the whole equation in a single text box. The computer then stores what has been typed into the text box in a string, and then it parses the string to find the coefficents. For an equation like 2x²+2x+2, the coeficients are 2, 2 and 2 and they are stored in the string at locations 0, 4 and 7. The big problem is, what if it's a bigger equation like 32x²+32x+45 or 123x²+45x+6? My logic won't work in that case. Does anyone know how to do it?
Here's my code that only works for the small equations:
Public Class Form1
Dim i1 As Double
Dim i2 As Double
Dim i3 As Double
Dim delta As Double
Dim x1 As Double
Dim x2 As Double
Dim leters As String
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
i1 = T1.Text
i2 = T2.Text
i3 = T3.Text
delta = (i2 * i2) - 4 * (i1 * i3)
If (delta < 0) Then
Ld.Text = delta
L1.Text = "Impossível"
L2.Text = "Impossível"
Else
x1 = (-i2 + Math.Sqrt(delta)) / (2 * i1)
x2 = (-i2 - Math.Sqrt(delta)) / (2 * i1)
Ld.Text = delta
L1.Text = x1
L2.Text = x2
End If
End Sub
Private Sub RadioButton1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton1.CheckedChanged
GroupBox1.Text = "Equação"
GroupBox1.Width = 200
GroupBox1.Height = 58
T1.Width = 188
T3.Hide()
T2.Hide()
Label1.Hide()
Button1.Hide()
Button2.Show()
End Sub
Private Sub RadioButton2_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton2.CheckedChanged
GroupBox1.Text = "Coeficientes"
GroupBox1.Width = 200
GroupBox1.Height = 143
T1.Width = 119
T3.Show()
T2.Show()
Label1.Show()
Button1.Show()
Button2.Hide()
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
leters = T1.Text
leters.ToString()
End Sub
End Class
You could use RegEx to parse the string, which would be nice because you could change the RegEx expression as a setting without rebuilding your assembly. However, if the equation string format is always going to be the same, as appears to be the case here (because the rest of the code would fail if it weren't), then you could simply use String.Split to parse the string. For instance:
Dim equation As String = "32x²+32x+45"
Dim parts() As String = equation.Split(New Char() {"+"c, "x"c}, StringSplitOptions.RemoveEmptyEntries)
Dim coefficient1 As Integer = Integer.Parse(parts(0))
Dim coefficient2 As Integer = Integer.Parse(parts(2))
Dim coefficient3 As Integer = Integer.Parse(parts(3))

Why is this not showing any results in PictureBox?

I'm trying to change an image to black and white on a variable threshold for use in an ocr program. My problem is that I'm not seeing any results in the image that is supposedly processed. I do experience a small wait when rendering, so i am to assume that it is actually doing something.
Imports System.Object
Imports System.Drawing.Bitmap
Public Class Form1
Dim x As Integer, y As Integer
Dim imgx As Integer, imgy As Integer
Dim img As Bitmap
Dim thresh As Bitmap
Dim pixelColor As Color
Dim threshcolor As Color
Dim tempcolor As Color
Public Function getpixel(ByRef x As Integer, ByRef y As Integer) As Color
End Function
Public Sub find_img_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles find_img.Click
open_img.ShowDialog()
img_dsp.Text = open_img.FileName()
img_loc.Text = open_img.FileName
img_dsp.ImageLocation = img_dsp.Text
End Sub
Public Sub find_img_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles find_img.LostFocus
img = (img_dsp.Image)
img_dsp.Refresh()
img_dsp.Text = open_img.FileName()
img_dsp.ImageLocation = img_dsp.Text
img = (img_dsp.Image)
img_dsp.Refresh()
End Sub
Public Sub render_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles render.Click
Dim myx As Integer
Dim myy As Integer
img_threshold.Image = img
thresh = img_threshold.Image
For myy = 1 To (img.Height - 1)
For myx = 1 To (img.Width - 1)
tempcolor = img.GetPixel(myx, myy)
If tempcolor.GetBrightness < NumericUpDown1.Value Then
thresh.SetPixel(x, y, Color.Black)
End If
If tempcolor.GetBrightness > NumericUpDown1.Value Then
thresh.SetPixel(x, y, Color.White)
End If
Next myx
Next myy
img_threshold.Image = thresh
img_threshold.Refresh()
End Sub
End Class
Do you know what a reference is? writing A = B, and changing A and writing B = A?
if you specify that A and B point the same object (a reference to the same object) changing one changes the other too, they occupy the same storage in memory! Teach yourself basic, before writing programs, please.