Results 1 to 10 of 10

Thread: [RESOLVED] code help

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2016
    Posts
    45

    Resolved [RESOLVED] code help

    Code:
    Public Class Converter
        'the dim stores the variable'
        Dim myCurr As Decimal
        Dim myPounds As Decimal
        Dim myAmount As Decimal
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboCurrency.SelectedIndexChanged
            'To change each currencies exchange rate change the number after myCurr = '
            'To add another currency see Tech Description Document'
            Select Case cboCurrency.Text
                Case "Euro"
                    myCurr = 1.12
                Case "USDollar"
                    myCurr = 1.24394
                Case "AUDollar"
                    myCurr = 1.61
                Case "CADollar"
                    myCurr = 1.66
                Case "HKDollar"
                    myCurr = 9.61
                Case "RUB"
                    myCurr = 79.03
            End Select
        End Sub
    
    
        Private Sub btnConvert_Click(sender As Object, e As EventArgs) Handles btnConvert.Click
            myPounds = txtBP.Text
            myAmount = myCurr * myPounds 'multiplies the pounds by the exchange rate'
            txtAmount.Text = myAmount.ToString("N2") 'displays the answer and rounds answer to 2 decimal places'
    
            If myPounds < 5 Then
                lblError.Text = "ERROR only sales over 5 may be processed"
                lblError.ForeColor = Color.Red
                txtAmount.Text = "Invalid"
            Else
                lblError.Text = ""
            End If
    
        End Sub
        'The code below provides code for a button to log out the staff member from the app'
        Private Sub btnLogout_Click(sender As Object, e As EventArgs) Handles btnLogout.Click
            MsgBox("Logout Successful")
            Login.Show()
            Login.txtUsername.Text = ""
            Login.txtPassword.Text = ""
            Me.Close()
        End Sub
    So above is my current code i need to make it so people cant input any letters or special charcters except . and numbers

  2. #2
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    477

    Re: code help

    The easiest way is to use a NumericUpDown (NUD for short) control instead of a TextBox for user input. It doesn't allow letters or special characters to be put in.

    IF you want the NUD to look like a Text box just put the following line of code in the Form Load Event for each NUD you use:
    Code:
    NumericUpDown1.Controls(0).Visible = False

    NOTE: I have heard that sometimes the above code to hide the NUD Controls doesn't paint correctly on Win7 and earlier versions of Windows. If this doesn't work for you then use this:

    Code:
    Public Class NumericUpDownEditBox
        Inherits NumericUpDown
        Public Sub New()
            MyBase.New()
            Controls(0).Visible = False
            Me.DecimalPlaces = 2
        End Sub
        Protected Overrides Sub OnTextBoxResize(ByVal source As Object, ByVal e As System.EventArgs)
            Controls(0).Size = New Size(0, ClientSize.Height)
            Controls(1).Size = New Size(ClientSize.Width, ClientSize.Height)
        End Sub
    End Class
    Then use the NumericUpDownEditBox for your input field.
    Last edited by Maverickz; Dec 23rd, 2016 at 07:52 PM.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,033

    Re: code help

    There are real currency rates, updated daily, available for download at:

    http://www.ecb.europa.eu/stats/eurof...xref-daily.xml

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,033

    Re: code help

    Also, you should turn on Option Strict

  5. #5
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    477

    Re: code help

    Quote Originally Posted by .paul. View Post
    There are real currency rates, updated daily, available for download at:

    http://www.ecb.europa.eu/stats/eurof...xref-daily.xml
    Oh that could be really cool to use a web request to load that xml dynamically when the form loads, so the program is always using the latest values.

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2016
    Posts
    45

    Re: code help

    none of that helps

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,033

    Re: code help

    If it doesn't help, you need to explain what doesn't work.
    All of the suggestions in this thread should help in some way.
    My suggestion to turn Option Strict on will seem to cause a lot of errors. It doesn't. It just enforces efficient programming...

  8. #8
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    477

    Re: code help

    Quote Originally Posted by knightsoluck View Post
    none of that helps
    Umm you asked:

    Quote Originally Posted by knightsoluck View Post
    So above is my current code i need to make it so people cant input any letters or special charcters except . and numbers

    My first post explicitly answers that question. Stop using a TextBox for user input and use a NUD instead. That is the correct, most efficient way to prevent users from inputting non-numerical characters. The only other way is to have your code manually validate the input.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,033

    Re: code help

    @knightsoluk
    Don't give up. Our answers really do help

  10. #10
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,369

    Re: code help

    May be you should ask a question, rather than letting us guess what the actual problem is then.

    Kris

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width