从字符串“ Large”到类型“ Double”的转换无效

时间:2019-02-14 11:57:09

标签: vb.net

所以我试图让我的代码正常工作,这意味着要显示取决于大小和金额的价格,但是当我尝试运行代码并获得价格时,会出现错误

从字符串“ Large”到类型“ Double”的转换无效。

当我单击价格按钮时。

Dim large As Integer
Dim total As Integer

Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
End Sub

Private Sub PreviewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PreviewButton.Click
    PictureBox1.Image = System.Drawing.Image.FromFile _
      ("H:\My Pictures\" + ColourComboBox.SelectedItem + "Tshirt.PNG")

    PictureBox1.ClientSize = New Size(200, 200)
    PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage

    str = "The colour chosen is " & ColourComboBox.SelectedItem _
        & vbCrLf & " and the size chosen is " & SizeComboBox.SelectedItem

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub SizeComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SizeComboBox.SelectedIndexChanged
    small = 10
    medium = 12
    large = 15

End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbuy.Click
    total = SizeComboBox.SelectedItem * AmountComboBox.SelectedItem
    MessageBox.Show(total)
End Sub

结束班级

2 个答案:

答案 0 :(得分:0)

听起来像这样的错误消息:

  

从字符串“ Large”到类型“ Double”的转换无效。

发生在此行(Button2_Click()子):

total = SizeComboBox.SelectedItem * AmountComboBox.SelectedItem

两个选定的项目似乎都是字符串值,并且其中之一等于"Large"。也许您应该修复检索分配给“大”项目的整数值的方式。

VB.NET隐式地将字符串转换为整数-仅在不启用Option Strict的情况下,强烈建议不要这样做(强烈建议 )(但Konrad Rudolph指出),但是在这里失败,因为该值不t代表整数。

答案 1 :(得分:0)

我建议您将NumericUpDown用作“金额”(数量)输入。您可以设置最小值和最大值。

<canvas></canvas>
<img src="https://via.placeholder.com/200x200"/>
<p id="fooId">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin eget lorem luctus nisi fermentum imperdiet in ac tortor. Vestibulum interdum risus vitae metus finibus pretium. Nullam facilisis lacus nec pellentesque faucibus. In tempus lorem ut mi sodales, vitae scelerisque quam pretium. Duis venenatis enim in nunc laoreet venenatis. Aliquam at magna vitae purus tincidunt posuere. Donec dictum pharetra ipsum, eu auctor lorem aliquet vitae. Donec faucibus metus quis laoreet ultricies. Aliquam aliquet, lectus a tempor tristique, diam sem auctor felis, sed ultrices magna nunc ut sem. Curabitur congue diam lacinia risus sodales luctus. In nec maximus ex. Nulla ultrices diam a erat imperdiet, nec convallis nisl pulvinar. Etiam quis placerat arcu, eu elementum felis. Phasellus lectus massa, faucibus faucibus nibh ut, dignissim tempor neque.
<p/>

您上面的代码除了表单级变量的值外,实际上没有任何作用。组合框中的文本与这些变量无关。

Private Sub SizeComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SizeComboBox.SelectedIndexChanged
    small = 10
    medium = 12
    large = 15
End Sub

我将这些项目添加到Form.Load中的组合框中,但这可以在设计时完成。

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ComboBox1.Items.Add("small")
    ComboBox1.Items.Add("medium")
    ComboBox1.Items.Add("large")
End Sub

在这里,我们添加了一个表单级变量来保存所选的大小。

最后,算术完成并显示结果。在小数总计上调用.ToString(“ C”)时,C会告诉它以货币显示结果。

Private ProductSize As Decimal = 0

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    Select Case ComboBox1.SelectedItem.ToString
        Case "small"
            ProductSize = 10
        Case "medium"
            ProductSize = 12
        Case "large"
            ProductSize = 15
        Case Else
            MessageBox.Show("Please make a size selection.")
    End Select
End Sub