2013-10-06 25 views
-2

我被困在簡單航空公司預訂的問題上。Visual basic 2010簡單航空公司預訂

  • 10文本框被加入的形式
  • 1-5文本框上是爲吸菸者區域僅
  • 6-10文本框僅是非吸菸者區域。

扭曲是通過使用Inputbox輸入用戶輸入1作爲吸菸者和2作爲非吸菸者。

如果用戶輸入1,座位文本框必須由計算機隨機放置,而不是由用戶設置(textbox1-5),則非吸菸者也是如此。

我們的老師給出了一個關於文本框的製作數組的提示,但似乎它對於這個東西的工作方式真的很無知。

基本座位儲備。

不知道在這段代碼中我需要添加什麼。

Dim reserve() As TextBox = {smokingtxt1, smokingtxt2, smokingtxt3, smokingtxt4, smokingtxt5} 

Dim reserve1() As TextBox = {nonsmokingtxt1, nonsmokingtxt2, nonsmokingtxt3, nonsmokingtxt4, nonsmokingtxt5} 
Dim notification As Integer 

notification = InputBox("Enter 1 or 2") 

If notification = 1 Then 
    For Each i As TextBox In reserve 
    i.Text = "Reserve" 
    Next 
ElseIf notification = 2 Then 
    For Each j As TextBox In reserve1 
    Randomize() 
    Next 
Else 
    MessageBox.Show("Invalid operation") 
End If 
+0

你有什麼'Randomize()'方法? – Tim

+0

順便說一句,'InputBox'是Visual Basic的延緩 - 我建議不要使用它,並以更多的.NET方式進行 - 可能是用戶可以輸入值的文本框,或者甚至更好的ComboBox有一個下拉列表,他們可以從中選擇一個值。 – Tim

+0

嗯,我只是想弄清楚這就是爲什麼Randomize()方法在那裏。 但是,我們的老師建議使用Inputbox,爲吸菸者輸入1,非吸菸者輸入2。 一旦進入進程,計算機必須從文本框中隨機分配爲「保留」,直到座位被佔用。我們班的 – JC3196

回答

0

在VB6中,您曾經能夠在窗體上創建一個控件數組。這是不可能在VB.NET(如Visual Studio 2010中)

你可以實現你需要通過添加10個文本框的形式,然後用下面的代碼做了什麼:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    Dim notification As Integer 

    notification = InputBox("Enter 1 or 2") 

    Dim RandomInt As Integer 

    If notification = 1 Then 
    If Textbox1.Text = "RESERVED" And Textbox2.Text = "RESERVED" Then 
     Msgbox "All seats are reserved" 
     Exit Sub 
    End If 
    TryAgain: 
     RandomInt = GetRandomInt(1, 5) 
     Select Case RandomInt 
      Case 1 
       'Check if the current textbox is already reserved 
       If TextBox1.Text = "RESERVED" Then 
        'Get a new number 
        Goto TryAgain 
       End If 
       TextBox1.Text = "RESERVED" 
      Case 2 
       TextBox2.Text = "RESERVED" 
       'etc 
     End Select 
    ElseIf notification = 2 Then 
     RandomInt = GetRandomInt(6, 10) 
     Select RandomInt 
      Case 5 
       TextBox5.Text = "RESERVED" 
      Case 6 
       TextBox6.Text = "RESERVED" 
       'etc 
     End Select 
    Else 
     MsgBox("You entered an invalid number") 
    End If 

End Sub 

Public Function GetRandomInt(ByVal StartNum As Integer, ByVal EndNum As Integer) As Integer 
    Dim RandomClass As New Random() 
    Dim RandomNumber As Integer 
    RandomNumber = RandomClass.Next(StartNum, EndNum) 

    GetRandomInt = RandomNumber 

End Function 

祝你好運該項目。

+0

如果文本框全部被隨機佔用,將會有什麼聲明? – JC3196

+0

我會編輯我的帖子供您查看。 –

+0

我誤解了你需要的東西。我會再次編輯:) –