2013-11-15 46 views
1

所以基本上你有一個只顯示一個標籤(lblTotalPrice)的主表單和一個按鈕,當你點擊它時,它會打開另一個包含不同價格包(lstPackages)的列表框。您如何從lstPackages中選擇一個項目並將其返回到lblTotalPrice,它位於主窗體上?如何將第二種形式的列表框上的選定項目返回到Visual Basic中的主窗體?

我試圖用一個函數代碼(認爲這將是對計算很有用),但似乎沒有奏效:

在我的第二個形式,我填充lstPackages與它的使用並行陣列名:

Dim strPackages As String = {"Package 1", "Package 2", "Package 3", "Package 4", "Package 5"} 
Dim decPrice As String = {100D, 200D, 300D, 400D, 500D} 

Private Sub SecondForm_Activated(sender As Object, e As EventArgs) Handles MyBase.Activate 
    Dim frmMain as New 
    Dim i As Integer 

    For i = 0 To strPackages.Length - 1 
     lstPackages.Items.Add(strPackages(i)) 
    Next 
End Sub 

之後,我嘗試選擇上lstPackages的值,但我不是太肯定,如果我用的是正確的代碼來選擇項目,並返回到主窗體:

Function CalcPackage() 
    Dim decPackages As Decimal = 0D 

    If lstPackages.SelectedIndex = -1 Then 
     MessageBox.Show("Select a package") 
    Else 
     For i = 0 To lstPackages.SelectedItem - 1 
      decPackages = lstPackages.SelectedItem(i) 
     Next i 
    End If 

    Return decPackages 
End Function 

當我選擇了lstPackages的項目,我想選擇的項目發回的主要形式,但遇到了一個問題在這裏:

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnClose.Click 
    Dim frmMainForm As New MainForm 
    Dim decTotal As Decimal 

    decTotal = CalcPackage() 

    'I was stuck in this part and wasn't sure how to return it to lblTotalPrice (and also close the second form too) 
    frmMainForm.lblTotalPrice.Text = decTotal.ToString("c") 

    frmMainForm.Show() 
    Me.Close() 
End Sub 

所以啊,對不起,如果我的代碼很奇怪,但我'm希望得到如何返回項目的價值,以在主窗體上的lblTotalPrice的幫助,謝謝你們

+1

表格是其他對象。將第一個表單的引用傳遞給第二個表單實例,通過從第一個表單調用方法/屬性/字段來設置值。 – SJuan76

+0

你可以在名爲'SelectedPackage'的窗體上創建一個屬性,並通過選擇的方式。 – Plutonix

回答

1

你的問題的一部分是,你正在按鈕出口點擊事件新主窗體的一個實例第二種形式的處理程序,如以下代碼所示:

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnClose.Click 
    Dim frmMainForm As New MainForm 
    Dim decTotal As Decimal 

    decTotal = CalcPackage() 

    'I was stuck in this part and wasn't sure how to return it to lblTotalPrice (and also close the second form too) 
    frmMainForm.lblTotalPrice.Text = decTotal.ToString("c") 

    frmMainForm.Show() 
    Me.Close() 
End Sub 

而是通過其構造主要形式的一個實例傳遞給第二個形式,像這樣:

Public Class SecondForm Inherits Form 
    Dim theMainForm As MainForm 
    Public Sub New(mainForm As MainForm) 
     theMainForm = mainForm 
    End Sub 
End Class 

現在你可以通過theMainForm變量在MainForm類引用lblTotalPrice,就像這樣:

theMainForm.lblTotalPrice.Text = lstPackages.SelectedItem 

theMainForm.lblTotalPrice.Text只有在第二個表單關閉時纔會更新,以響應從lstPackages中選擇的項目;這樣處理主要形式的標籤的更新在SelectedIndexChanged事件lstPackages,像這樣:

Protected Sub lstPackages_IndexChanged(sender As Object, e As EventArgs)   
    Dim decTotal As Decimal 
    decTotal = CalcPackage() 

    theMainForm.lblTotalPrice.Text = decTotal.ToString("c") 
End Sub 

全部退出按鈕單擊事件處理程序應該做的是展示的主要形式,並關閉第二種形式,就像這樣:

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnClose.Click 
    theMainForm.Show() 
    Me.Close() 
End Sub 
+0

但我的函數,我用來從lstPackages工作中選擇一個項目?只是想知道如何將這個放在btnExit代碼中,以便在您選擇了物品後關閉表單? – PatrickGamboa

+0

查看更新的答案。 –

+0

現在分離計算和退出表單代碼現在更有意義了,謝謝! – PatrickGamboa

0

只是顯示SecondForm與ShowDialog的(),然後與引用它,你已經檢索值:

Public Class MainForm 

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 
     Dim sf As New SecondForm 
     If sf.ShowDialog = Windows.Forms.DialogResult.OK Then 
      lblTotalPrice.Text = sf.TotalPrice 
     End If 
    End Sub 

End Class 

Over SecondForm:

Public Class SecondForm 

    Private _TotalPrice As Decimal 
    Public ReadOnly Property TotalPrice As Decimal 
     Get 
      Return _TotalPrice 
     End Get 
    End Property 

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnClose.Click 
     If lstPackages.SelectedIndex = -1 Then 
      MessageBox.Show("Select a package") 
     Else 
      _TotalPrice = lstPackages.SelectedItem(0) 
      Me.DialogResult = Windows.Forms.DialogResult.OK 
     End If 
    End Sub 

End Class 
相關問題