-
Oct 15th, 2020, 02:27 PM
#1
Thread Starter
New Member
VB Help Needed
"I am trying to complete this exercise."
Modify the btnDisplay_Click procedure so that it uses two functions named GetGrade101 and GetGrade201 to get the appropriate grade; the procedure should then display the grade in the lblGrade control. Change the two independent Sub procedures to functions that return the appropriate grade to the statements that invoke them in the btnDisplay_Click procedure. Each function should contain a parameter that accepts the total points passed to it.
This is my code......
Code:
Option Explicit On
Option Strict Off
Option Infer Off
Public Class frmMain
Dim intPoints As Integer
Dim Grade As String
Dim GGrade As String
' Independent Sub procedures.
Private Function GetGrade101(ByVal intPoints As Integer) As String
Select Case intPoints
Case Is >= "90"
Grade = "A"
Case Is >= "80"
Grade = "B"
Case Else
Grade = "F"
End Select
Return Grade
End Function
Private Function GetGrade102(ByVal intPoints As Integer) As String
Select Case intPoints
Case Is >= "75"
Grade = "P"
Case Else
Grade = "F"
End Select
Return intPoints
End Function
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
' Calls independent Sub procedures to display a grade.
If radHis101.Checked Then
lblGrade.Text = GetGrade101(intPoints)
Else
lblGrade.Text = GetGrade102(intPoints)
End If
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub txtPoints_Enter(sender As Object, e As EventArgs) Handles txtPoints.Enter
txtPoints.SelectAll()
End Sub
Private Sub ClearGrade(sender As Object, e As EventArgs) Handles txtPoints.TextChanged, radHis101.CheckedChanged, radHis201.CheckedChanged
lblGrade.Text = String.Empty
End Sub
Private Sub txtPoints_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtPoints.KeyPress
' Accept only numbers and the Backspace key
If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
End Sub
End Class
Any help or guidance will be appreciated.
Last edited by Shaggy Hiker; Oct 15th, 2020 at 03:02 PM.
Reason: Added CODE tags.
-
Oct 15th, 2020, 02:55 PM
#2
Thread Starter
New Member
Re: VB Help Needed
Basically when I click the buttondisplay is should show me a letter grade based on the radbutton I picked while converting the number grade put in the text points text box to a letter grade to display in lblgrade control
-
Oct 15th, 2020, 03:11 PM
#3
Re: VB Help Needed
Welcome to the forums, I edited your post to wrap the code in [CODE][/CODE] tags, which you can do by pressing the # button and pasting the code between the resulting tags, or pasting in the code, then highlighting and pressing the # button.
The first thing I noticed can be seen in this line:
Case Is >= "90"
You are comparing an integer to a string ("90"). That probably means that you have Option Strict OFF, because otherwise you have a conversion going on, and I have no idea which way it would convert, in this case. For the code to work, you'd have to have the string implicitly convert to an integer, which may well happen, but it's a bad idea. If the compiler decided to convert the integer to a string, then compare the two strings, the comparison would not work the way you are hoping for. For example, "100"<"90" even though 100>90.
Another point, that isn't going to cause immediate problems, but which is a bad idea, is that you are returning a class level variable from the functions. You should make a local variable set that, and return that, then use the return to set the form level variable Grade. By using the form level variable the way you are using it, while there may be no problem...ever, you run the risk of some nasty little interactions creeping into the code that would cause bizarre behavior, someday. So, it's not going to cause immediate harm, but it's not a good idea.
A third point that you almost certainly won't be allowed to do anything about, is the simple fact that you are using a textbox for numeric entry, which is why you have to handle the keypress event in a vain attempt to limit what gets entered. It will mostly work...until somebody copies and pastes something into the textbox, in which case the Keypress event won't fire, and they can put anything they want in there, which will then crash the program if it isn't an integer. A far better solution would be to use a NumericUpDown control and get the .Value property of that (it has a .Text property, but don't use it). The NUD already only allows numbers, and you can set the range, which in this case would probably be 0 to 100. Still, from the way you worded the question, I doubt you can make THAT change. It's just something to keep in mind for later.
My usual boring signature: Nothing
-
Oct 16th, 2020, 12:05 AM
#4
Re: VB Help Needed
To remove Pasting...
Code:
Public Class numericTextbox
Inherits TextBox
Const WM_PASTE As Integer = &H302
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
' Accept only numbers and the Backspace key
If (e.KeyChar < 48 OrElse e.KeyChar > 57) AndAlso e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
MyBase.OnKeyPress(e)
End Sub
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_PASTE Then
Return
End If
MyBase.WndProc(m)
End Sub
End Class
For a more comprehensive numeric TextBox...
https://www.vbforums.com/showthread....numericTextbox
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|