windows application in vb.net for calculating student marks - vb.net

I need to make a vb.net windows application for calculating the student marks.I have three valuations.For example if valution1 is 45 marks and if valuation2 has 30 then there is 15 marks difference so valuation3 has to be done.Then average will be calculated by adding valuation 3 with maximum score of valuation1 and valuation2.valuation3 is done only when there is a difference of 15 or more marks in valuation1 and valuation2.Please help me to make this application as i am new to coding.

Get three textboxes, set the third textbox's enabled property to false. The following code will help you (under textbox's textchanged event).
Private Sub Hello () Handles Textbox1.Textchanged, Textbox2.Textchanged
If Val(Textbox1.Text) - Val(Textbox2.Text) >= 15 then
Textbox3.Enabled = True
Else
Textbox3.Enabled = False
End If
End Sub
For average, you need to add all the values and then divide by number of terms. Suppose you display the average in a label:
Dim a as integer
If Textbox3.Enabled = True then
a = Val(Textbox1.Text) + Val(Textbox2.Text) + Val(Textbox3.Text)
Label1.Text = a/3
Else
a = Val(Textbox1.Text) + Val(Textbox2.Text)
Label1.Text = a/2
End If
I hope you understand! This was a type of homework, but I did it just because I was bored. Please don't ask such questions in the future. Read the 'How to Ask' guide next time.

Related

I'm having difficulty calculating time differences in Visual Basic [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Layout of Calculator
I am trying to build a program that calculates times differences in minutes. I do this by subtracting the start time from the finish time. I have four text boxes, two of which are for the time unit of hours (h), and the other two are for minutes (m).
a is the starting time in hours and c is the finish time in hours. They are being multiplied by 60 to convert the hours to minutes. I want to calculate the time difference between 5:40pm and 7:15pm but some how end up with 567 when the answer should be 95.
This is not a homework task, I'm a lazy learner driver in Australia who wants to create a simple program that calculates the time of a journey in minutes.
Can somebody please tell me what I'm doing wrong?
Here is my code:
Public Class Calc
Dim Product
Dim a, b, c, d As Integer
Private Sub Enter_Click(sender As Object, e As EventArgs) Handles Enter.Click
a = Val(TextBox1.Text) * 60
b = Val(TextBox2.Text)
c = Val(TextBox3.Text) * 60
d = Val(TextBox4.Text)
Product = (c - a + d - b)
Time.Text = ("Driving Time: " & Product)
End Sub
End Class
I wasn't going to post this but I couldn't let the use of TimeSerial by Dy.Lee go unanswered. This code uses reasonable variable names, uses the correct type for time periods, i.e. TimeSpan, and also compiles with Option Strict On, which it should pretty much always be. I'd get rid of that Val usage too but I couldn't be bothered here.
Private Sub Enter_Click(sender As Object, e As EventArgs) Handles Enter.Click
Dim startHours = CInt(Val(TextBox1.Text))
Dim startMinutes = CInt(Val(TextBox2.Text))
Dim endHours = CInt(Val(TextBox3.Text))
Dim endMinutes = CInt(Val(TextBox4.Text))
Dim startTime As New TimeSpan(startHours, startMinutes, 0)
Dim endTime As New TimeSpan(endHours, endMinutes, 0)
Dim timeDifference = endTime - startTime
Time.Text = ("Driving Time: " & timeDifference.TotalMinutes)
End Sub
EDIT: It also declares variables in the appropriate place, i.e. in the method they're being used in. If you're using those same variables elsewhere then you'd have to stick with fields but I'm guessing that you're not doing so.
EDIT: Here's a version without the dodgy Val calls and some proper validation. You could combine all the If statements into one but separating them allows you to display different messages based on the type of issue.
Private Sub Enter_Click(sender As Object, e As EventArgs) Handles Enter.Click
Dim startHours As Integer
Dim startMinutes As Integer
Dim endHours As Integer
Dim endMinutes As Integer
If Integer.TryParse(TextBox1.Text, startHours) AndAlso
Integer.TryParse(TextBox2.Text, startMinutes) AndAlso
Integer.TryParse(TextBox3.Text, endHours) AndAlso
Integer.TryParse(TextBox4.Text, endMinutes) Then
If startHours < 24 AndAlso
startMinutes < 60 AndAlso
endHours < 24 AndAlso
endMinutes < 60 Then
Dim startTime As New TimeSpan(startHours, startMinutes, 0)
Dim endTime As New TimeSpan(endHours, endMinutes, 0)
If startTime < endTime Then
Dim timeDifference = endTime - startTime
Time.Text = ("Driving Time: " & timeDifference.TotalMinutes)
Else
'Notify user of invalid input.
End If
Else
'Notify user of invalid input.
End If
Else
'Notify user of invalid input.
End If
End Sub
#PaulHebert pointed out to me that I needed to swap around textbox 3 & 4 because I was treating the wrong fields as hours. The math had made sense in my head so I probably overlooked a rather simple yet inconvenient mistake. I want to thank everyone who tried to help :) Merry Christmas!
Your math is wrong you are subtracting the hours and then adding them to the difference of minutes. You need to get the total number of minutes since midnight from each time and then subtract those. Then get the absolute value so you don't have negative minutes
Private Sub Enter_Click(sender As Object, e As EventArgs) Handles Enter.Click
a = Val(TextBox1.Text) * 60
b = Val(TextBox2.Text)
c = Val(TextBox3.Text) * 60
d = Val(TextBox4.Text)
Product = Math.Abs((c +d) - (a + b))
Time.Text = ("Driving Time: " & Product)
End Sub
Try using this code
Private Sub Enter_Click(sender As Object, e As EventArgs) Handles
a = CInt(Trim(TextBox1.Text)) * 60
b = CInt(Trim(TextBox2.Text))
c = CInt (Trim(TextBox3.Text)) * 60
d = CInt(Trim(TextBox4.Text))
Product = (c + d) - (a + b)
Time.Text = ( "Driving Time: " & Product)
End Sub
Also make sure that the text boxes are properly arranged. Text box 1 should come first followed by the others.

LOOP PROBLEMS in VB.Net

I am creating a math tutorial app for kids in the primary level. And I am having a hard time in creating a loop that would generate 2 random numbers,then check if the answer is right/wrong for 5 times. also, the button would be disabled after clicking 5 times. The other things are clear to me except the idea of how to put it in a loop. can someone help me please? thanks! I tried using the FOR LOOP, but sadly, it would just loop 5 times but it would only check the answer 1 time.I need it to check 5 different answers.
For ctr As Integer = 1 To 5
Button3.Enabled = False
initialize()
If TextBox3.Text = sum Then
MsgBox("correct")
point = point + 1
TextBox3.Focus()
Else
MsgBox("wrong")
MsgBox(sum)
TextBox3.Focus()
End If
Next
MsgBox(point)
If you want to process five different TextBoxes in a loop, one way to do that is to create an array containing all the TextBoxes and loop through the array.
Dim boxes() As TextBox = {TextBox3, TextBoxX, TextBox22, TextBoxBB, TextBoxA}
For Each box As TextBox in boxes
box.Focus()
If box.Text = sum Then
MsgBox.("correct")
point += 1
Else
MsgBox("wrong")
End If
Next
Alternatively, if you want the user to enter five answers one at a time in a single TextBox, you can have the user click a button each time an answer is entered. You should define the counter outside the button's click handler.
Private ctr As Integer
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
ctr += 1
If ctr > 5 Then Button3.Enabled = False
initialize()
If TextBox3.Text = sum Then
MsgBox("correct")
point += 1
Else
MsgBox("wrong")
MsgBox(sum)
End If
MsgBox(point)
End Sub

Getting even/odd numbers specifically with a random number generator in visual basic [duplicate]

This question already has answers here:
Generating Even Random Numbers
(2 answers)
Closed 8 years ago.
trying to make a random number generator using visual basic for a school project. The user would enter in 2 different values in textbox1 and textbox 2, press a button and a random number would be generated between these 2 digits (this random number would be displayed in textbox3). This was too basic for the project, so i decided to add in 2 checkboxs which, when checked would make the generated number either even or odd.
Really need some help with an algorithm that limits the random number to be even or odd. Any help is greatly appreciated! :) (checkbox1 is for making it even, checkbox2 for odd)
Dim answer As Integer
Dim result As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox3.Clear()
TextBox3.Text = answer
If CheckBox1.Checked = False And CheckBox2.Checked = False Then
answer = CInt(Int((TextBox2.Text * Rnd() + TextBox1.Text)))
End If
^ the above code also seems to generate random numbers in a specific order, always starting from 0, any help with this would be greatly appreciated :)
If CheckBox1.Checked = True Then
Do Until result = 0
result = CDec(TextBox1.Text / 2) - CInt(TextBox1.Text / 2)
Loop
If result = 0 Then
answer = CInt(Int((TextBox2.Text * Rnd() + TextBox1.Text)))
End If
End If
End Sub
This is what I'd do to solve this problem:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 'Handles Button1.Click
'Parse the two numbers
Dim minValue = Integer.Parse(TextBox1.Text)
Dim maxValue = Integer.Parse(TextBox2.Text)
'Create a list of all possible valid numbers
Dim values = Enumerable.Range(minValue, maxValue - minValue).ToArray()
'Keep only the even numbers if selected
If CheckBox1.Checked Then
values = values.Where(Function (v) v Mod 2 = 0).ToArray()
End If
'Keep only the odd numbers if selected
If CheckBox2.Checked Then
values = values.Where(Function (v) v Mod 2 = 1).ToArray()
End If
'Check there are numbers
If values.Length = 0 Then
TextBox3.Text = "There no available numbers to choose."
Else
'`rnd` here is `System.Random` as I didn't know what `Rnd()` was.
TextBox3.Text = values(rnd.Next(0, values.Length)).ToString()
End If
End Sub
You could use a function to generate either an even or odd number depending on which checkbox is checked, the functions would use mod to determine whether the generated number id even/odd. If it is not what you require, then it would try again until the generated number matches. For example:
Private Sub btnGenerate_Click(sender As System.Object, e As System.EventArgs) Handles btnGenerate.Click
If chkOdd.Checked Then
GenerateOdd()
ElseIf chkEven.Checked Then
GenerateEven()
End If
End Sub
Private Function GenerateOdd()
Dim r = CInt(Math.Ceiling(Rnd() * 100))
If ((r Mod 2) = 0) Then
'r is even, generate another number and try again
GenerateOdd()
Else
'r is odd we have a match
yourTextBox.Text = r
Return r
End If
Return Nothing
End Function
Private Function GenerateEven()
Dim r = CInt(Math.Ceiling(Rnd() * 100))
If ((r Mod 2) = 0) Then
'r is even, we have a match
yourTextBox.Text = r
Return r
Else
'r is odd, generate another number and try again
GenerateEven()
End If
Return Nothing
End Function
I haven't tried this but you get the point!
*Edit - the (Rnd() * 100)) is a random number between 1 and 100

VB textbox and width prob

hi there im trying to design a section in my program that builds layers represented by rectangles, depending on the sizes inputed will decide upon the outcome of the rectangle width. Im having a problem when entering < 0 it will revert to 1 or 0.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim rectangle As Integer
rectangle = Val(TextBox1.Text)
TextBox1.Text = Convert.ToString(rectangle)
Form2.RectangleShape1.Width = Val(TextBox1.Text)
If Val(TextBox1.Text) >= 1.0 Or Val(TextBox1.Text) <= 1.5 Then
Form2.RectangleShape1.Width = 75
End If
If Val(TextBox1.Text) >= 1.5 Then
Form2.RectangleShape1.Width = 120
End If
If Val(TextBox1.Text) <= 1.0 Then
Form2.RectangleShape1.Width = 55
End If
Form2.RectangleShape1.Show()
Me.Hide()
It's hard to say with the provided information and I don't understand exactly what you are doing but a few things seems wrong there.
The you lost me part
Let's look at this first :
Dim rectangle As Integer
rectangle = Val(TextBox1.Text)
TextBox1.Text = Convert.ToString(rectangle)
Form2.RectangleShape1.Width = Val(TextBox1.Text)
So you declare an integer
Take the text of some textbox and select only the numbers from it by taking out characters
You assign back that value in the Textbox with a ToString() on the Integer
You assign the value of the textbox once again taking out characters (you already did that)
You can resume this line with :
Form2.RectangleShape1.Width = CDbl(Val(TextBox1.Text))
I changed the conversion to doubles because I'm certain you want decimals. Integers can't have decimals. Without decimals, these two conditions would be the exact same thing :
If Val(TextBox1.Text) >= 1.0 Or Val(TextBox1.Text) <= 1.5 Then
Form2.RectangleShape1.Width = 75
End If
If Val(TextBox1.Text) >= 1.5 Then
Form2.RectangleShape1.Width = 120
End If
Since you can either have 1 or 2. (So checking if it's between 1 and 1.5 OR 1.5 and 2 is pointless).
Val
You probably know this but val will return the numbers in a string. So as I asked in the comments, why don't you prevent the user from inputting something else than numbers ?
Let's say you want to have this as result : "< 0".
If you prevent letter and symbol he will have to input only "0"
If you do it like you're doing right now he will input "< 0" the val function will only return "0" so telling me you need the "<" is against your current logic.
But I want the number and the symbol...
Don't worry. If you are make sure the user will only input what you want (by putting restriction on the possible characters to input), you can catch the whole content of the textbox.
If myTextBox.Text = "< 1.5" Then
'Do something cool
Else If myTextBox.Text = "< 0" Then
'Do something cooler
Else
'Do nothing
End If

Random Generator in Loop

I am having some problems with an assignment. The case project is:
Create an application that allows the user to guess a random number generated by the computer. When the user makes an incorrect guess, the application should move an image either upr or down, depending on how the guess compares to the random number. If the random number is greater than the user's guess, the application should move the image up to indicate that the user needs to guess a higher number. If the random number is less than the user's guess, the application should move the image down to indicate that the user needs to guess a lower number. The game ends when the user guesses the random number. However, the application should allow the user to stop the game prematurely. When that happens the application should siplay the random number.
I have tried every which way I can think of including using a textbox instead of an inputbox and playing around with the syntax - but just can't seem to get it right. Advice would be much appreciated. Thanks.
My code:
Public Class Form1
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Me.Close()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Number As Integer
Dim inputNumber As Integer
Dim answer As Integer
Dim isconverted As Boolean
Dim computerchoice As New Random
answer = computerchoice.Next(1, 20)
inputNumber = InputBox("Please guess number", "Random Number Game")
Do
isconverted = Integer.TryParse(inputNumber, Number)
If isconverted = True Then
If answer = Number Then
MessageBox.Show("You Win")
ElseIf answer > Number Then
PictureBox1.SetBounds(0, 90, 0, 0, BoundsSpecified.Y)
ElseIf answer < Number Then
PictureBox1.SetBounds(0, 220, 0, 0, BoundsSpecified.Y)
End If
Else
MessageBox.Show("Please enter a valid number between 1 - 20 only")
End If
inputNumber = InputBox("Please guess number", "Random Number Game")
Loop While answer <> Number
MessageBox.Show("Answer:" & answer.ToString)
End Sub
End Class
Your code actually almost worked. A few things though:
The only thing that really didn't work was the picture moving up and down. All you have to do for that is to increment/decrement the .Top property.
Because you converted your input to a number at the beginning of the loop and not evaluating till the end, you were looping through an extra time after you got the right answer.
The number comparison after the conversion was redundant, since you know they got the number if they exit the loop.
If you're new to Visual Studio and don't know about breakpoints and other debugging, it is worth it to look into those. With these tools you can pause your code at given points in your program, look at the values variables hold, and step through your code line-by-line.
Here's the working code:
Do
If isconverted = True And Number >= 1 And Number <= 20 Then
If answer > Number Then
PictureBox1.Top -= 10
ElseIf answer < Number Then
PictureBox1.Top += 10
End If
Else
MessageBox.Show("Please enter a valid number between 1 - 20 only")
End If
inputNumber = InputBox("Please guess number", "Random Number Game")
isconverted = Integer.TryParse(inputNumber, Number)
Loop While (answer <> Number)
MessageBox.Show("You Win! The answer is " & answer.ToString)