2015-11-15 60 views
-1

我是新來的visual basic,希望這是一個簡單的問題。我有一個按鈕來調用不同的窗體菜單。表格被設計並且具有標籤和文本字段和按鈕等等。從主菜單我嘗試過兩種不同的方式調用窗體。表單打開並且看起來正確和功能的一種方式。另一種方式是將窗體打開爲一個沒有字段的小空白方塊。最終,我想在主菜單打開時創建一組List對象,並將它們來回傳遞給其他表單以供輸入和處理。我使用並行列表作爲簡單學校實驗室的臨時數據庫。我只是沒有看到我打電話的方式有什麼問題。我甚至都沒有考慮過如何正確傳遞List對象。Visual basic/visual studio 2010窗體加載空白

Public Class frmMain 

Dim arrGames As New List(Of String) 
Dim arrDates As New List(Of String) 
Dim arrPrices As New List(Of Decimal) 
Dim arrSeats As New List(Of Integer) 

Private Sub btnEnterGames_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnterGames.Click 
    'NewEnter.Visible = True 
    Dim frmEnter As New NewEnter(arrGames, arrDates, arrPrices, arrSeats) 
    frmEnter.ShowDialog() 
End Sub 

Private Sub btnReports_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReports.Click 
    'Reports.Visible = True 
    Dim frmReports As New Reports(arrGames, arrDates, arrPrices, arrSeats) 
    frmReports.Visible = True 
End Sub 

Private Sub btnSellTickets_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSellTickets.Click 
    'SellTickets.Visible = True 
    Dim frmSell As New SellTickets(arrGames, arrDates, arrPrices, arrSeats) 
    frmSell.Visible = True 
End Sub 

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click 
    Close() 
End Sub 
End Class 

這是NewEnter形式的代碼。我有新的例程接受4個列表,基本上什麼都沒有做。在主菜單中執行「NewEnter.Visible = True」會正確加載表單,但我必須在表單中註釋New子例程或者出現錯誤。

Public Class NewEnter 

Private _arrGames As List(Of String) 
Private _arrDates As List(Of String) 
Private _arrPrices As List(Of Decimal) 
Private _arrSeats As List(Of Integer) 

Sub New(ByVal arrGames As List(Of String), ByVal arrDates As List(Of String), ByVal arrPrices As List(Of Decimal), ByVal arrSeats As List(Of Integer)) 
    ' TODO: Complete member initialization 
    ' _arrGames = arrGames 
    ' _arrDates = arrDates 
    ' _arrPrices = arrPrices 
    ' _arrSeats = arrSeats 
End Sub 

Private Sub btnSaveGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveGame.Click 

    Dim arrGames As New List(Of String) 
    Dim arrDates As New List(Of String) 
    Dim arrPrices As New List(Of Decimal) 
    Dim arrSeats As New List(Of Integer) 

    Dim strGame As String 
    Dim strPrice As String 
    Dim strSeats As String 
    Dim intSeats As Integer 
    Dim decPrice As Decimal 
    Dim bolGameErr As Boolean 
    Dim bolDateErr As Boolean 
    Dim bolPriceErr As Boolean 
    Dim bolSeatErr As Boolean 

    strGame = txtGame.Text 
    strPrice = txtPrice.Text 
    strSeats = txtSeats.Text 

    '~~~~~~~~~~~~verify a game is entered 
    If String.IsNullOrEmpty(strGame) Or String.IsNullOrWhiteSpace(strGame) Then 
     bolGameErr = True 
    Else 
     '~~~~~~~~~~~~verify price is numeric 
     If IsNumeric(strPrice) Then 
      decPrice = strPrice 
      '~~~~~~~~~~~~~~~verify seats are numeric 
      If IsNumeric(strSeats) Then 
       intSeats = Convert.ToInt32(strSeats) 
       ' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ add elements to array lists 
       arrGames.Add(New String(strGame)) 
       arrDates.Add(dtpDate.Text) 
       arrPrices.Add(New Decimal(decPrice)) 
       arrSeats.Add(intSeats) 

       lblSaveSuccessful.Visible = True 
       ClearInput() 
       ' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ add elements to array lists 
      Else 
       bolSeatErr = True 
      End If 

     Else 
      bolPriceErr = True 
     End If 
    End If 

    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Check flags for input errors 
    If bolDateErr = True Then 
     lblErr.Text = "Invalid date" 
     lblErr.Visible = True 
    End If 
    If bolGameErr = True Then 
     lblErr.Text = "Must enter a game name" 
     lblErr.Visible = True 
     txtGame.Focus() 
    End If 
    If bolDateErr = True And bolGameErr = True Then 
     lblErr.Text = "Must enter a game name and valid date" 
     lblErr.Visible = True 
     txtGame.Focus() 
    End If 
    If bolPriceErr = True Then 
     lblPriceErr.Visible = True 
     txtPrice.Text = "" 
     txtPrice.Focus() 
    End If 
    If bolSeatErr = True Then 
     lblSeatErr.Visible = True 
     txtSeats.Text = "" 
     txtSeats.Focus() 
    End If 
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Check flags for input error 
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Display output 
    Dim i As Integer 
    i = 0 

    lblData.Text = arrGames.Count.ToString 
    Do While i < arrGames.Count 
     lblData.Text = Convert.ToString(arrGames(i)) & " on " & Convert.ToString(arrDates(i)) & " Price: " & _ 
      Convert.ToString(arrPrices(i)) & " Available Seats: " & Convert.ToString(arrSeats(i)) 
     i += 1 
    Loop 
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Display output 

    lblData.Visible = True 
End Sub 

Private Sub ClearInput() 
    'lblErr.Visible = False 
    'lblPriceErr.Visible = False 
    'lblSeatErr.Visible = False 
    txtGame.Text = "" 
    txtPrice.Text = "" 
    txtSeats.Text = "" 
    txtGame.Focus() 
End Sub 

Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    'Me.Visible = True 
    'Me.BackColor = Color.BurlyWood 
    'Me.ResumeLayout() 
    'Me.Activate() 
    'Me.Focus() 
    'Me.Show() 
    'Me.lblGameHdr.Visible = True 
End Sub 

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click 
    Close() 
End Sub 
End Class 
+1

它看起來像你創建自己的構造函數,而不是使用一個VS所需要的。沒有它,沒有控制或表單道具會初始化。刪除你的'Sub New',然後在代碼窗口中,在左邊的下拉列表中選擇表單對象,然後在右邊的下拉列表中選擇New。注意那裏列出的設計師警告。在這種情況下,我實際上添加了一個AddData方法(或一組道具),並且在這種情況下將這些東西傳遞給了ctor。 – Plutonix

+0

將InitializeComponents()添加到您的構造函數類。 –

回答

0

將InitializeComponent()添加到您的構造方法類中。
這是默認添加到所有Visual Basic窗體的New(構造函數)函數。它要求它在表單上設置UI組件。

+0

我簡直不敢相信。我無法相信我沒有在某個地方找到答案。謝謝! –

+0

如果你可以提出我的答案,那就太棒了,哈哈。我之前犯過同樣的錯誤,所以我知道它有多令人沮喪。 –

+0

我的聲望太低,無法增加公衆的分數:( –