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

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

Related

How can I increase the font size of the user's input by one every 2 seconds in the code below?

Public Class Form1
Dim mypicturebox As New PictureBox
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = "hello " & TextBox1.Text
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
Me.Label1.Text = "enter your name"
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mypicturebox.BackColor = Color.Red
mypicturebox.Width = 50
mypicturebox.Height = 50
mypicturebox.Left = 100
mypicturebox.Top = 100
Me.Controls.Add(mypicturebox)
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim MyFont As New Font(TextBox1.Font.FontFamily, 12, FontStyle.Bold)
Dim size As Integer = 30
Me.Font = New Font(Me.Font.FontFamily, size)
Label1.Font = New Font(Label1.Font.FontFamily, size)
Label1.Font.Size = Label1.Font.Size + 1
End Sub
End Class
When I run the code, it says that the font size is read-only so I cannot change it.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Interval = 2000 '2 sec
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
TextBox1.Font = New Font(TextBox1.Font.FontFamily, TextBox1.Font.Size + 1)
End Sub

So I tried creating a checklist with points in vb.net

I'm still currently trying to create a checklist with points as a reference if you are unsure you have COVID-19. ex. "if you have 1 point then you are still fine"
Right now all checkboxes work except for one which is the second checkbox
Public Class Form1
Dim pts As Byte
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = "You have " & pts
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If Me.CheckBox1.Checked = True Then
pts = pts + 1
End If
End Sub
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
If Me.CheckBox2.Checked = True Then
pts = pts + 1
End If
End Sub
Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
If Me.CheckBox1.Checked = True Then
pts = pts + 98
End If
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
CheckBox1.CheckState = 0
CheckBox2.CheckState = 0
CheckBox3.CheckState = 0
Label1.Text = ""
pts = 0
End Sub
End Class
I don't know what else I can do really
The total values are just added but not subtracted on un-check event. Just simply use Else statement to do so. Look at the following example:
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If Me.CheckBox1.Checked = True Then
pts = pts + 1
Else
pts = pts - 1
End If
End Sub

How to set limit values textbox and show message box when reached maximum limit VB.Net

Sorry for bad english.
I'm beginner in VB.Net, on this question I want to make textbox validation to show messagebox when maximum limit is reached.
below this code
Public Class Form1
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim i As Integer
TextBox1.MaxLength = 6
i = TextBox1.MaxLength
If TextBox1.Text.Length > i Then
MsgBox("Maximum is 6 Character")
End If
End Sub
End Class
In form load event set TextBox1.MaxLength = 6
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
TextBox1.MaxLength = 6
End Sub
and use the following code in TextBox1_KeyDown event
Private Sub TextBox1_KeyDown(ByVal sender As Object _
, ByVal e As System.Windows.Forms.KeyEventArgs _
) Handles TextBox1.KeyDown
If Trim(TextBox1.Text).Length = 6 Then
MsgBox("Maximum is 6 Character")
End If
End Sub
Or
Keep TextBox1.MaxLength as system default,if you use below code then no need to alter it's lenghth to 6
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Trim(TextBox1.Text).Length = 6 Then
e.Handled = True
MsgBox("Maximum is 6 Character")
End If
End Sub

Reset a loop in Visual Basic

Basically If somebody presses button2 then I want the program to reset the measuring. The actually code keeps measuring forever and I only want to reset that measuring. Sorry for my bad english, I hope you understand what I really want, If not I will explain in details.
Here is my code:
Public Class Form1
Private MonitorWidth As Double = 51.5 'cm
Private MonitorHeigth As Double = 31 'cm - This must be enter by the user
Private TotalDistance As Double
Private PixelHeigth As Double 'cm
Private PixelWidth As Double 'cm
Private WithEvents TTimer As New Timers.Timer
Private PointQueue As New Queue(Of Point)(100)
Private Lastpoint As Point
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
TTimer.Stop()
While PointQueue.Count > 0
Dim P As Point = PointQueue.Dequeue
TotalDistance += Math.Sqrt(((Lastpoint.X - P.X) * PixelWidth) ^ 2 + ((Lastpoint.Y - P.Y) * PixelHeigth) ^ 2)
Lastpoint = P
TextBox1.Text = Format(TotalDistance, "Distance: #,##0.0 cm")
TTimer.Start()
End While
End Sub
Private Sub TTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles TTimer.Elapsed
PointQueue.Enqueue(MousePosition)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Button1.Enabled = True Then
Lastpoint = MousePosition
TTimer.AutoReset = True
TTimer.Interval = 5
TTimer.Start()
Dim ScreenBounds As Rectangle = Screen.GetBounds(New Point(0, 0))
Dim Screenwidth_Pixel As Integer = ScreenBounds.Width
Dim screenHeigth_Pixel As Integer = ScreenBounds.Height
PixelHeigth = MonitorHeigth / screenHeigth_Pixel
PixelWidth = MonitorWidth / Screenwidth_Pixel
Timer1.Interval = 200
Timer1.Start()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Button2.Enabled = True Then
Timer1.Stop()
TextBox1.Text = ""
TextBox1.Text = Format(TotalDistance = 0)
End If
End Sub
End Class
In your Button2 event handler (Button2_Click), reset the queue:
PointQueue = New Queue(Of Point)(100)

Multiple camera shots in vb

See I have this simple vb.net codes that counts from 5 to 1 then says capture! I need to do this 4 consecutive times after the start button is clicked .. I tried a do until loop but it didn't work, I'm a newbie here so please help..
Public Class Form_welcome
Dim Count As Integer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Count -= 1
Label2.Text = Count
If (Count = 0) Then
Timer1.Enabled = False
Label2.Hide()
Label3.Show()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Count = 5
Timer1.Enabled = True
Label2.Text = Count
Timer1.Interval = 1000
End Sub
End Class
something like this?
Public Class Form_welcome
Dim Count As Integer
Dim pictureCount as integer = 4
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Count -= 1
Label2.Text = Count
If (Count = 0) Then
pictureCount -=1
If pictureCount = 0 then
Timer1.Enabled = False
End If
'take a picture
Label2.Hide()
Label3.Show()
Count = 5
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Count = 5
Timer1.Enabled = True
Label2.Text = Count
Timer1.Interval = 1000
End Sub
End Class
Public Class Form_welcome
Dim Count As Integer
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Admin_Login.Show()
Me.Hide()
End Sub
Private Sub Form_welcome_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Application.Exit()
End Sub
Private Sub Form_welcome_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button4.Hide()
Button5.Hide()
Button6.Hide()
'Timer1.Enabled = True
'Label2.Text = Count
'Timer1.Interval = 1000
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Count -= 1
'Label2.Text = Count
If (Count = -1) Then
Timer1.Enabled = False
Label3.Show()
Label3.Hide()
Label2.Hide()
Button4.Show()
Button5.Show()
Button6.Show()
PictureBox4.Hide()
PictureBox3.Show()
pict1.Show()
pict2.Show()
pict3.Show()
pict4.Show()
Button2.Hide()
End If
If (Count Mod 3) = 0 Then
Label2.Text = "Captured!"
Else
Label2.Text = Count
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Count = 12
Timer1.Enabled = True
Label2.Text = Count
Label2.Show()
Timer1.Interval = 1000
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
Application.Exit()
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Button2.Show()
Button4.Hide()
Button5.Hide()
Button6.Hide()
PictureBox3.Hide()
pict1.Hide()
pict2.Hide()
pict3.Hide()
pict4.Hide()
PictureBox4.Show()
End Sub
End Class