0

我有 Option Strict On。下面的代码在我打开时不起作用。我能够将问题隔离到这一行

Decimal.TryParse(lblAnnualMid.Text, decAnnualMid)

因为这一行没有将值从文本更改为十进制,所以我的其余代码不起作用,我的标签显示 $0。

我怎样才能解决这个问题。我仍然在 VB.Net 中摸索,所以如果这看起来很明显,请原谅我。

这是我的其余代码:

Option Strict On
Option Explicit On

Public Class frmCustomRanges

    'Variables for the MidPoint TextBoxes
    Dim decAnnualMid As Decimal
    Dim decHourlyMid As Decimal

Private Sub txtRangeSpread_TextChanged(sender As Object, e As EventArgs) Handles txtRangeSpread.TextChanged

        'This event runs when the user enters a number in the 
        'txtRangeSpread text box. The amount is converted to a decimal
        'it then calculates the min and max of the range for annual
        'and hourly ranges.  The ranges change as the user changes the range
        'spread.


        'Variables for the event
        Dim decRangeSpreadResults As Decimal

        'Verify entry is numeric
        If IsNumeric(txtRangeSpread.Text) Then

            'Convert entry to decimal
            Dim decRangeSpread As Decimal = Convert.ToDecimal(txtRangeSpread.Text)

            'convert the Mid point value to decimal
            Decimal.TryParse(lblAnnualMid.Text, decAnnualMid)

            'convert range spread value to percentage
            decRangeSpreadResults = decRangeSpread / 100


            'Display results in dollar string Annual salary
            lblAnnualMin.Text = Convert.ToDecimal(decAnnualMid - (decRangeSpreadResults * decAnnualMid)).ToString("C")
            lblAnnualMax.Text = Convert.ToDecimal(decAnnualMid + (decRangeSpreadResults * decAnnualMid)).ToString("C")

            ''Display results in dollar string in Hourly rate
            lblHourlyMin.Text = Convert.ToDecimal(decAnnualMid + (decRangeSpreadResults * decAnnualMid) / 52 / 40).ToString("C")
            lblHourlyMax.Text = Convert.ToDecimal(decAnnualMid + (decRangeSpreadResults * decAnnualMid) / 52 / 40).ToString("C")

        Else

            MsgBox("You have entered a non-numeric value. Please check value and enter again", vbCritical, "Input Error")


            With txtRangeSpread
                .Text = ""
                .Focus()

            End With

        End If


    End Sub
4

2 回答 2

0

我将线路更改为:

Decimal.TryParse(lblAnnualMid.Text.Replace("$", ""), decAnnualMid)

这很完美。

谢谢你们。

于 2013-06-24T02:39:57.680 回答
0

你实际上想要的是这个

  decimal.TryParse("$35,000",NumberStyles.Currency ,null , out test)

前任:

    using System;
    using System.Globalization;

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                decimal test;
                Console.WriteLine(decimal.TryParse("$35,000",NumberStyles.Currency ,null , out test));
                Console.WriteLine(test);
                Console.Read();
            }
        }
    }

有关 NumberStyles 的更多信息

于 2013-06-24T04:52:21.500 回答