0

Hiim 对 vb 来说相当新,所以我希望我能正确解释这一点,我遇到的问题是我正在创建一个剧院预订系统,如果没有组合框做同样的事情,我似乎无法获得提交按钮来打开下一个表单。我有四个组合框和一个提交按钮都链接到同一个事件。这样用户就可以选择票的数量,总数将自动显示在总数标签中,当按下提交按钮时,将出现一个新表单,Form3 将出现......

我需要至少选择一个组合框,否则会出现错误提示“请至少选择一张票”,然后如果在按下提交按钮时至少选择了一个,则应该出现下一个表单,但是使用我在选择下拉框时的代码打开一个新表单,我知道我的问题..但我不知道如何得到我想要的。

我的代码如下:

Sub ComboBoxes_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged, ComboBox2.SelectedIndexChanged, ComboBox3.SelectedIndexChanged, ComboBox4.SelectedIndexChanged, Submitbtn.Click
    'Assigned an evet handler to all of the comboboxes then calculates the price and puts in total box

    Dim Totalcombo1, Totalcombo2, Totalcombo3, Totalcombo4, Price As Decimal

    Dim valuecombo1 = (ComboBox1.SelectedIndex + 1)  'finds position of option selected & adds one to get number of tickets
    Dim valuecombo2 = (ComboBox2.SelectedIndex + 1)
    Dim valuecombo3 = (ComboBox3.SelectedIndex + 1)
    Dim valuecombo4 = (ComboBox4.SelectedIndex + 1)


    'if the submit button is selected without there being a value selected from any combobox then error should appear, saying at least 1 ticket should be purchased.
    If (ComboBox2.SelectedIndex = -1) Then
        Totalcombo2 = 0
    Else
        Price = 6.5
        Totalcombo2 = valuecombo2 * Price
    End If

    'determines the ticketprice of combobox 1


    If (ComboBox1.SelectedIndex = -1) Then
        Totalcombo1 = 0
    Else
        Price = 9
        Totalcombo1 = valuecombo1 * Price
    End If
    'determines the ticketprice of combobox 2


    If (ComboBox3.SelectedIndex = -1) Then
        Totalcombo3 = 0
    Else
        Price = 6.5
        Totalcombo3 = valuecombo3 * Price
    End If
    'determines the ticketprice of combobox 3


    If (ComboBox4.SelectedIndex = -1) Then
        Totalcombo4 = 0
    Else
        Price = 6.5
        Totalcombo4 = valuecombo4 * Price
    End If
    'determines the ticketprice of combobox 4

    Try
        If (ComboBox1.SelectedIndex And ComboBox2.SelectedIndex And ComboBox3.SelectedIndex And ComboBox4.SelectedIndex) = -1 Then
            Throw New System.Exception()

        End If

    Catch ex As Exception When (ComboBox1.SelectedIndex And ComboBox2.SelectedIndex And ComboBox3.SelectedIndex And ComboBox4.SelectedIndex) = -1
        MessageBox.Show("Please make at least one ticket selection before continuing. ")

    End Try

    Totallbl.Text = Totalcombo1 + Totalcombo2 + Totalcombo3 + Totalcombo4
    'adds the totals of the ticketprices and then inserts into the Total label


End Sub

Sub SubmitForm_OpenBooking()

    Dim FrmBooking As New Form3
    FrmBooking.Show()

    Me.Hide()
End Sub

任何帮助,无论什么时候,都会有很大的帮助.. iv 一直在这几个小时。

4

2 回答 2

1

是否缺少某些代码?我看不到对 SubmitForm_OpenBooking 的调用。

无论如何,您需要分离您的逻辑。

  1. 您应该从 SelectedIndexChanged 中删除按钮单击事件。我真的很惊讶它的工作原理。
  2. 删除 try/catch 部分。它与 SelectedIndexChanged 无关,无论如何您都不应该将它们用于逻辑流。
  3. 为 Submit 按钮添加 Validating 事件处理程序,该按钮检查至少已选择一个组合(不要使用异常)。您甚至可以为此添加一个 ErrorProvider。
  4. 为提交按钮添加 OnClick 事件处理程序以显示新表单。
于 2012-04-11T21:28:49.087 回答
0

您的这部分代码有问题:

If (ComboBox1.SelectedIndex And ComboBox2.SelectedIndex And ComboBox3.SelectedIndex And ComboBox4.SelectedIndex) = -1 Then

以这种方式使用 And 不会给出您可能期望的结果。它对 SelectedIndexValues 执行按位与运算。如果要检查所有选定索引的值是否为 -1,您应该这样做:

If ComboBox1.SelectedIndex = -1 And ComboBox2.SelectedIndex = -1 And ComboBox3.SelectedIndex = -1 And ComboBox4.SelectedIndex = -1 Then

使用AndAlso代替 And也是一个好主意。

此外,没有理由在代码中使用 Try-Catch 子句。只需在 If 语句中显示消息框。

于 2012-04-11T21:28:39.380 回答