none
I need help (Case) RRS feed

  • Question

  • Hello

    I am new to Visual Basics. I am taking a class for University and I can't afford to fail things. 

    This is for the professor McNabb, see it's my own work! 

    Public Class Form1
        Private lblMessage As Object
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim currentTextBox As TextBox
    
            'Loop through all the top level controls.>>
            For Each ctrl As Control In Me.Controls
    
                'If the control is a TextBox do the next action.>>
                If TypeOf ctrl Is TextBox Then
                    'Convert the type of ctrl to a TextBox and assign
                    'it to "currentTextBox"
                    currentTextBox = CType(ctrl, TextBox)
                    'Clear the current TextBox.>>
                    currentTextBox.Clear()
                End If
            Next
            ' Sets focus to the first text box.
            TextBox1.Focus()
        End Sub
        Private Sub Label1_Click(sender As Object, e As EventArgs) Handles MessageBox.Click
    
        End Sub
        Public Shared Function Val(
        Label1 As String
    ) As Double
    
        End Function
    
        Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
    
        End Sub
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            'closes the application
            End
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim a As Decimal = 0.02D
            Dim b As Decimal = 0.03D
            Dim c As Decimal = 0.04D
            Dim d As Decimal = 0.05D
    
    
            Select Case Label1
                Case "1"
                    Label1 = 500 + a * TextBox2
                Case "2"
                    Label1 = 700 + b * TextBox2
                Case "3"
                    Label1= 1000 + c * TextBox2
                Case "4"
                    Label1= 1250 + d * TextBox2
                Case Else
    
                    Exit Select
            End Select
    
    
    
        End Sub
    
        Private Function GetTextBox2() As TextBox
            Return TextBox2
        End Function
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            'checks if the Present Value is not a numeric value
            If Not IsNumeric(TextBox1.Text) Then
                Windows.Forms.MessageBox.Show("The Present Value must be numeric")
                TextBox1.Select() 'places the cursor in the textbox
            End If 'breaks out of the procedure
    
            'checks if the Present Value is between 1 and 4
            If Val(TextBox1.Text) < 1 Or Val(TextBox1.Text) > 4 Then
                Windows.Forms.MessageBox.Show("The Present Value must be between 1 and 4")
                TextBox1.Select()
            End If
    
        End Sub
    
        Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
            'checks if the Present Value is not a numeric value
            If Not IsNumeric(TextBox2.Text) Then
                Windows.Forms.MessageBox.Show("The Present Value must be numeric")
                TextBox2.Select() 'places the cursor in the textbox
            End If 'breaks out of the procedure
    
            'checks if the Present Value is between 1 and 100,000
            If Val(TextBox2.Text) < 1 Or Val(TextBox2.Text) > 100000 Then
                Windows.Forms.MessageBox.Show("The Present Value must be between 1 and 100,000")
                TextBox2.Select()
            End If
        End Sub
    
    
    End Class

    The Case part I need it to post as Label1 but it keeps telling me that can't be converted to the string.

    Secondly, I need to get the equation so <g class="gr_ gr_610 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" data-gr-id="610" id="610">its</g> meant to be 2 or 3, 4, 5 % of the amount of money they put into the textbox2

    <g class="gr_ gr_611 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" data-gr-id="611" id="611">Its</g> not working. <g class="gr_ gr_614 gr-alert gr_tiny gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" data-gr-id="614" id="614">i</g> can't get it to work so I put decimal but then it says Textbox cannot be converted to Decimal.

    Help


    • Edited by Lisa213 Monday, March 19, 2018 1:01 PM
    Saturday, March 17, 2018 9:13 PM

All replies

  • Hi

    Here is an example with comments

    ' this is a valid decimal number Dim a As Decimal = 0.02D ' this results in a string of the number produced ' by the calculation NOTE: the string representation ' in the TextBox has to be converted into a valid ' number before it can be used as it is a string ' THEN convert the result to a string for display in ' the Label

    Label1.Text = (500 + a * CDec(TextBox2.Text)).ToString

    BTW: if you put these options at the top of your code, it would help to prevent a lot of the errors.

    Option Strict On
    Option Explicit On


    Regards Les, Livingston, Scotland



    • Edited by leshay Saturday, March 17, 2018 9:33 PM
    Saturday, March 17, 2018 9:30 PM
  • Hello

    Thanks so much for the help

    Been playing with it and trying so hard. It continues to tell me that the Textbox2 cannot be converted to decimal. Should I recopy code in case you need to see how I added it.  As for Label <g class="gr_ gr_255 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" data-gr-id="255" id="255">can not</g> be converted to String

    Option Strict On
    Option Explicit On
    Public Class Form1
        Private lblMessage As Object
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ' this is a valid decimal number
            Dim a As Decimal = 0.02
    
            ' this results in a string of the number produced
            ' by the calculation NOTE: the string representation
            ' in the TextBox2 has to be converted into a valid
            ' number before it can be used as it is a string
            ' THEN convert the result to a string for display in
            ' the Label
    
            Dim a As Decimal = 0.02
            Dim b As Decimal = 0.03
            Dim c As Decimal = 0.04
            Dim d As Decimal = 0.05
    
            Select Case Label1
                Case "1"
                    Label1 = (500 + a * CDec(TextBox2)).ToString
                Case "2"
                    Label1 = 700 + b
                Case "3"
                    Label1 = 1000 + c
                Case "4"
                    Label1 = 1250 + d
    
    
                    Exit Select
            End Select
    
    
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim currentTextBox As TextBox
    
            'Loop through all the top level controls.>>
            For Each ctrl As Control In Me.Controls
    
                'If the control is a TextBox do the next action.>>
                If TypeOf ctrl Is TextBox Then
                    'Convert the type of ctrl to a TextBox and assign
                    'it to "currentTextBox"
                    currentTextBox = CType(ctrl, TextBox)
                    'Clear the current TextBox.>>
                    currentTextBox.Clear()
                End If
            Next
            ' Sets focus to the first text box.
            TextBox1.Focus()
    
        End Sub
        Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
    
        End Sub
        Public Shared Function Val(
        Label1 As String
    ) As Double
    
        End Function
    
        Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
    
        End Sub
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            'closes the application
            End
        End Sub
    
        Private Function GetTextBox2() As TextBox
            Return TextBox2
        End Function
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    
            'checks if the Present Value is not a numeric value
            If Not IsNumeric(TextBox1.Text) Then
                Windows.Forms.MessageBox.Show("The Present Value must be numeric")
                TextBox1.Select() 'places the cursor in the textbox
            End If 'breaks out of the procedure
    
            'checks if the Present Value is between 1 and 4
            If Val(TextBox1.Text) < 1 Or Val(TextBox1.Text) > 4 Then
                Windows.Forms.MessageBox.Show("The Present Value must be between 1 and 4")
                TextBox1.Select()
            End If
    
    
        End Sub
    
        Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
            'checks if the Present Value is not a numeric value
            If Not IsNumeric(TextBox2.Text) Then
                Windows.Forms.MessageBox.Show("The Present Value must be numeric")
                TextBox2.Select() 'places the cursor in the textbox
            End If 'breaks out of the procedure
    
            'checks if the Present Value is between 1 and 100,000
            If Val(TextBox2.Text) < 1 Or Val(TextBox2.Text) > 100000 Then
                Windows.Forms.MessageBox.Show("The Present Value must be between 1 and 100,000")
                TextBox2.Select()
            End If
        End Sub
    
    
    End Class
    


    Saturday, March 17, 2018 11:19 PM
  • Hi

    The Label1.Text = is the property to use - you just have Label1 =

    (as per the sample code I posted)

    ALSO, you have just copied some of my example into your code - this is not the thing to do. You are now showing Dim a as Decimal twice - this will fail.

    My post was an example that you can use to adjust your own code - not just copy it.


    Regards Les, Livingston, Scotland



    • Edited by leshay Saturday, March 17, 2018 11:25 PM
    Saturday, March 17, 2018 11:22 PM
  • Take one small part.

            Select Case Label1
                Case "1"
                    Label1 = 500 + a * TextBox2

    Label1 is a variable that refers to a Control object.  That's not what you want to test for the Case statement - you want to test the value of the Text property of the label.

    Label1 is a variable that refers to a Control object. You can't set it to the result of a mathematical expression.   You probably want to refer to the Text property of that label.

    A Text property is a string - you can't directly assign the result of a numerical expression to it.  You need to convert the result of that expression to a string.

    TextBox2 is a variable that refers to a Control object - you can't use it in a mathematical expression. You probably intended to use the number typed into the text box - that would be the Text property.  But that's a String, and you can't use a String in a mathematical expression.   So the Text property of the text box needs to be converted to a number. 

    Therefore your code becomes

       Select Case Label1.Text
        Case "1"
            Label1.Text = (500 + a * CDec(TextBox2.Text)).ToString

    CDec is a very simplistic way of getting a number from a string, and can fail if the user types something invalid, or it can return a value you don't expect - there are much better ways of doing that conversion. But at least it makes the expression consistent.

    Similar problems occur elsewhere in your code.



    • Edited by Acamar Saturday, March 17, 2018 11:55 PM sp
    Saturday, March 17, 2018 11:52 PM
  • Well you look like you are trying pretty hard. However the 2 functions below seem unecessary IMO as one does nothing and the other returns TextBox2 which can be called directly. Plus I don't believe you should use a controls name, i.e. Label1, as a variable in a Function. Nor should you accidentally use a class name or namespace name like Integer As String or something similar. The variable should be a name not used anywhere else really otherwise it could "shadow" something else and cause issues to my knowledge. Or maybe not but it would be good practice not to get into that habit.

     Public Shared Function Val(Label1 As String) As Double
    
     End Function
    
    
    Private Function GetTextBox2() As TextBox
         Return TextBox2
    End Function

    Then the below code for TextBox1.TextChanged seems like it could be simplified by using Decimal.TryParse probably. And End If is just the end of an If, Then, Else type procedure.

       Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    
            'checks if the Present Value is not a numeric value
            If Not IsNumeric(TextBox1.Text) Then
                Windows.Forms.MessageBox.Show("The Present Value must be numeric")
                TextBox1.Select() 'places the cursor in the textbox
            End If 'breaks out of the procedure
    
            'checks if the Present Value is between 1 and 4
            If Val(TextBox1.Text) < 1 Or Val(TextBox1.Text) > 4 Then
                Windows.Forms.MessageBox.Show("The Present Value must be between 1 and 4")
                TextBox1.Select()
            End If
    End Sub


    Maybe for the above code you could try modifying it as below. Decimal.TryParse returns a boolean of true or false but also if true sets a decimal variable with the value. Note there is a Nested If Statement within the outer If Statement so you have to follow the If, Then, Else, EndIf for both the outer and the inner so as not to get the inners confused with the outers.

    Or maybe TextBox1 should only have 1, 2, 3 or 4 in it so instead of Decimal.TryParse maybe use Int32.TryParse. And change the variable to Integer type from Decimal type.

     Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            'checks if the Present Value is not a numeric value
            Dim TempDec As Decimal = 0
            If Decimal.TryParse(TextBox1.Text, TempDec) Then ' If it equals True - For outer If Statement
                ' Code for if it is True - Outer If Statement
                If TempDec >= 1 AndAlso TempDec <= 4 Then ' If it equals True for nested Inner if Statement
                    ' Code for if it is True - Inner If Statement
                Else
                    ' Code for if it is False - Inner If Statement
                End If
            Else
               ' Code for if it is False - Outer If Statement
            End If
    End Sub

    There is also code availalbe that will only allow numbers to be typed into a TextBox if you would like to see that.


    La vida loca

    Sunday, March 18, 2018 9:57 AM
  • Here's the tope code that disallows anything but numbers as well as the period or decimal point from being entered in a TextBox. The commented out second code disallows the period or decimal point also so you could use one or the other. The third code disallows pasting into the TextBox so the user can't paste a sentence or something into it.

        Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
            If Char.IsPunctuation(e.KeyChar) And e.KeyChar.ToString <> "." Or Char.IsLetter(e.KeyChar) Or Char.IsSymbol(e.KeyChar) Then 'Allows only numbers, "." and some keys like delete, backspace, enter, etc in TextBox
                e.Handled = True
            End If
        End Sub
    
        'Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
        'If Char.IsPunctuation(e.KeyChar) Or Char.IsLetter(e.KeyChar) Or Char.IsSymbol(e.KeyChar) Then 'Allows only numbers and some keys like delete, backspace, enter, etc in TextBox
        'e.Handled = True
        'End If
        'End Sub
    
        Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
            If e.Control AndAlso e.KeyCode = Keys.V Then e.SuppressKeyPress = True 'Disallows paste into TextBox1
        End Sub


    La vida loca

    Sunday, March 18, 2018 10:34 AM
  • It just dawned on me that you might be using VB6 or a VB6 text book or tutorial. We are mostly using VB.net now. In the old days Controls had default properties and you could refer to Textbox1 without the .Text but now that won't work. Can you verify what version of VB you are using?


    Mary

    Sunday, March 18, 2018 11:03 AM
  • I just wanted to see if it worked. My apologies. I am just trying so hard to figure this out. Having a rough time. I think I am going to start again. 
    Monday, March 19, 2018 1:07 PM
  • I will try the label1.text 

    CDec is there more information on that? Does TextBox2.Text)) need two brackets at the end? If I use this will it be considered cheating because I used your helpful information?

    Monday, March 19, 2018 1:09 PM
  • The textbook I was sent from the university is called Murach's Visual Basic 2012? Too Old? Its mandatory at least that I pass this because <g class="gr_ gr_166 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" data-gr-id="166" id="166">its</g> for the degree I was heading for. 
    Monday, March 19, 2018 1:11 PM
  • The textbook I was sent from the university is called Murach's Visual Basic 2012? Too Old? Its mandatory at least that I pass this because <g class="gr_ gr_166 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" data-gr-id="166" id="166">its</g> for the degree I was heading for. 

    Visual Basic 2012 is fine. Visual Studio from 2012 would be using VB from 2012 which actually is relative to the .Net Framework released in 2012 although you could use a latter VB simply bu using a later .Net Framework with Visual Studio 2012 when creating/compiling an app. Visual Basic 6 or less is from the 1990's time frame, I believe 1996 is when VB.Net began which is what you are using and is later than VB6 or less.

    Also see this link MSDN Library search Engine and you can search for any Namespaces, Classes, etc. CDec can be searched for too which stands for Convert to Decimal. There are many type conversion functions for converting one type to another type when possible for example CStr is to convert a type to a string.

    And don't apologize for trying. Trying is better than doing nothing at all and trial and error is supportive in learning so don't apologize for trying, IMO.


    La vida loca

    Monday, March 19, 2018 1:28 PM
  • CDec is there more information on that?

    https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/functions/type-conversion-functions

    The complete solution:
    https://msdn.microsoft.com/en-us/library/9zbda557(v=vs.110).aspx

    Does TextBox2.Text)) need two brackets at the end?

    No.  But the expression used with .ToString should be bracketed. That second bracket is paired with the one before the 500.

    If I use this will it be considered cheating because I used your helpful information?

    No. That code is just an example you use to work out how to make the required changes to the rest of your code. The information is what your instructor would provide if you asked.


    • Edited by Acamar Monday, March 19, 2018 8:43 PM sp
    Monday, March 19, 2018 8:40 PM
  • The textbook I was sent from the university is called Murach's Visual Basic 2012? Too Old?  

    The text is quite OK.   It is only some very advanced features that have changed since 2012.

    Other issues with that code that you might like to consider are:
    You have posted more code than is required for the actual problem.  Keeping the code very specific to the actual problem helps to identify the issue.

    Empty methods that you accidentally created by clicking in a control in the designer should be removed.

    You must be familiar with the basics of the debugger. See:
    https://vbdotnetblog.wordpress.com/overview/debugging/

    Monday, March 19, 2018 8:53 PM
  • If I use this will it be considered cheating because I used your helpful information?

    You need to ask your professor what would be considered cheating. The technical term is plagiarism. If you are not vulnerable to being expelled or otherwise affected by having violated a rule about plagiarizing someone else's work then the next person to ask is yourself. Are you learning? When people help others it is often easier to just show the code that solves a problem but when a person is learning and they just use some code without understanding it then it does not matter what the code is, if the student does not learn from the code then the code might be a lousy answer.

    The best answer for a student is usually an explanation (lesson) that the student learns from. Nearly always it can be much easier to just give the student some code but that often does not help the student learn.

    I think that questions should not be how to write an entire program. That usually requires too much work to answer. It is very appropriate to post an entire program that duplicates a specific problem but the question should not be about the entire program. Then if you add to that a question from a student then it can become a really big problem.

    It would work better if a student can learn how to write a program; how to analyze the requirements and understand how to convert that to something the computer understands. Once the problem has been analyzed and understood then there can be questions about how to implement specific pieces. Ideally it is possible to objectively choose one post or just a few posts that clearly answer the question or significantly contribute to the answer.



    Sam Hobbs
    SimpleSamples.Info


    Tuesday, March 20, 2018 1:12 AM
  • I just wanted to see if it worked. My apologies. I am just trying so hard to figure this out. Having a rough time. I think I am going to start again. 

    Hi

    You could benefit by starting over, and you would get all the help you want right here on the forum.

    If you do decide to start again, first of all, start a new thread with a suitable title - such as "Help with creating code to calculate totals" (or, more suited to your project)

    Then, decide on what the User would see on the Form, what the User would need to enter onto the Form (TextBoxes etc), Buttons the User would use to do something etc.

    Then decide what you as the programmer needs to consider - such as: the User has entered their name into a TextBox where I expected them to enter a number - this *could* be your first question - how do I prevent errors when the User enters an unexpected value - how to deal with such a scenario?

    It is up to you as the programmer to think of these kind of things and deal with all eventualities (the truth is, it can be very difficult to think of ALL of the unexpected inputs that a User can try)- how about a User just clicking a Button which needs to use a value in a TextBox but the User didn't bother to enter anything at all - an empty TextBox to deal with right there!

    Now, if you can organize your thoughts about what you want, start asking questions based on your attempts to get started.

    The value of the responses you get on the Forum depend almost exclusively on the quality of the questions (and information you give).


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, March 20, 2018 1:50 AM
    Tuesday, March 20, 2018 1:47 AM