2012-04-11 48 views
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 

任何幫助什麼那麼,WLD是一個很大的幫助.. IV已經在好幾個小時了。

+0

Metro?的WinForms? WPF? Silverlight的? ASP.Net? MonoTouch的? – SLaks 2012-04-11 20:43:42

+0

我猜想它的代碼的WinForms。 – nikhil 2012-04-11 20:46:14

+0

是啊,即時通訊使用visual basic窗體 – MightyMouse 2012-04-11 22:28:48

回答

0

你的這部分代碼是有問題的:

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

使用並以這種方式不會給你可能希望結果。它對SelectedIndexValues執行按位和操作。如果你想檢查是否所有選擇的指標具有值-1,你應該這樣做是這樣的:

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

這也將是使用AndAlso的來代替,而一個好主意。

此外,沒有理由在代碼中使用Try-Catch子句。只需在If語句中顯示消息框。

1

是否有一些代碼丟失?我看不到對SubmitForm_OpenBooking的調用。

無論如何,你需要分開你的邏輯。

  1. 您應該從SelectedIndexChanged中刪除按鈕單擊事件。我真的很驚訝它的作品。
  2. 刪除try/catch部分。它與SelectedIndexChanged無關,您不應該將它們用於邏輯流程。
  3. 爲提交按鈕添加驗證事件處理程序,該處理程序檢查至少一個組合已被選中(不要使用例外)。你甚至可以爲此添加一個ErrorProvider。
  4. 爲您的提交按鈕添加一個OnClick事件處理程序以顯示一個新表單。
相關問題