我在互联网上搜索过但无法找到问题的答案,但问题是我有两种形式;
frm_bookManeger
和
frm_addBook
第一个是主表单,有一个书籍列表(名为listBook
),一个TreeView
和一个用于调用第二个表单来添加新书的按钮。
在填写所有TextBoxes
和书籍信息后,按“添加”。然后,第二个表单将被关闭,该书的所有信息将保存在Book
类的实例中。问题是:如何将此实例传递给第一个表单以将其存储在listBook
。
例如: 如果我在表单1中创建一个构造函数来获取表单2,那么在表单2中:
Dim f1 As form1 = New form1(me)
f1.Show()
f2.Close()
我不能这样做,因为当我启动程序时,表单1会立即启动,而现在默认情况下OnCreateMainForm()中没有任何参数:
Protected Overrides Sub OnCreateMainForm()
Me.MainForm = Global.WindowsApplication5.frm1
End Sub
我该怎么做?
第一种形式:
Public Class frm_bookManeger
'list of Book
Dim listBook As List(Of Book) = New List(Of Book)
Private frm_addBook As frm_addBook
Public Sub New(frm_addBook As frm_addBook) 'got error
Me.frm_addBook = frm_addBook
End Sub
第二种形式:
Public Class frm_addBook
Dim Public tempBook As Book = New Book()
'add book
Private Sub btn_add_Click(sender As Object, e As EventArgs) Handles btn_add.Click
tempBook.BookName1 = TextBox_name.Text
tempBook.Author1 = TextBox_author.Text
tempBook.Price1 = TextBox_price.Text
tempBook.Genre1 = TextBox_genre.Text
tempBook.EstablishedDay1 = dtp_established.Value.Date
Dim frm_Mngr As frm_bookManeger = New frm_bookManeger(Me)
End Sub
End Class
答案 0 :(得分:0)
Dim frm As New form1
frm.textbox.Text = Me.passing value.Text
frm.Show()
或者你可以尝试
Public Class Form1
Private loginLabel As String
Public Sub New(ByVal loginParameter As String)
InitializeComponent()
Me.loginLabel = loginParameter
End Sub
End Class
dim frm as new Form1(label.Text)
答案 1 :(得分:0)
您的frm_addBook
需要对frm_bookManeger
实例的引用,以便它可以使用后者中的方法。
可以通过将对frm_bookManeger
的当前实例的引用传递给New
的{{1}}构造函数来完成。
此外,您可能希望将书籍添加为对话框形式而不是普通形式。
我制作了一个简单的“Book”类并使用TextBox来显示书籍,所以第一个形式就是:
frm_addBook
对于添加图书的表单,我添加了“取消”和“确定”按钮。
Imports System.Text
Public Class frm_BookManager
Dim bookList As List(Of Book)
Public Class Book
Property Name As String
Property Author As String
End Class
Public Sub AddBook(b As Book)
If bookList Is Nothing Then
bookList = New List(Of Book)
End If
bookList.Add(b)
End Sub
Private Sub ShowBooks()
Dim sb As New StringBuilder
For Each b In bookList
sb.AppendLine(b.Name & " by " & b.Author)
Next
TextBox1.Text = sb.ToString()
End Sub
Private Sub btn_add_Click(sender As Object, e As EventArgs) Handles btn_add.Click
Using addBook As New frm_addBook(Me)
Dim result = addBook.ShowDialog()
If result = DialogResult.OK Then
ShowBooks()
End If
End Using
End Sub
Private Sub frm_BookManager_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddBook(New Book With {.Name = "Wuthering Heights", .Author = "Emily Brontë"})
ShowBooks()
End Sub
End Class
请注意,可以在Public Class frm_addBook
Dim myParent As frm_BookManager
Private Sub bnOK_Click(sender As Object, e As EventArgs) Handles bnOK.Click
Dim b As New frm_BookManager.Book With {.Name = TextBox_name.Text, .Author = TextBox_author.Text}
myParent.AddBook(b)
End Sub
Public Sub New(parent As frm_BookManager)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
myParent = parent
' set the DialogResult for each button so the parent can tell what happened
bnCancel.DialogResult = DialogResult.Cancel
bnOK.DialogResult = DialogResult.OK
End Sub
End Class
添加新的Book
,因为myParent.AddBook(b)
是指myParent
的实例。
您可以对其进行修改,以便对话框保持打开状态,并且只有一个按钮可以添加一本书而不关闭对话框。我将frm_BookManager
方法设为私有,因此您无法从其所在的类外部调用它 - 您可以修改它。
有很多可能对我展示的代码进行小修改以实现更多功能。我无法抗拒将“Maneger”的拼写纠正为“经理”;)
答案 2 :(得分:0)
我认为最简单的方法是让frm_bookManager
表单具有包含已添加的书籍的属性。在ShowDialog
表单中,使用frm_addBook
显示该表单,如果用户在该表单上单击“确定”,则该属性将包含添加的图书。从公共财产拿到书后,请务必处理Public Class Book
Public Property Name As String
Public Property Author As String
End Class
Public Class frm_bookManager
Dim bookList As New List(Of Book)()
Private Sub btnAddBook_Click(sender As Object, e As EventArgs) Handles btnAddBook.Click
Using addBookForm As New frm_addBook()
If addBookForm.ShowDialog() = DialogResult.OK Then
bookList.Add(addBookForm.BookToAdd)
End If
End Using
End Sub
End Class
Public Class frm_addBook
Public Property BookToAdd As Book
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles
'User filled in the fields and clicked this OK button
Me.BookToAdd = New Book()
Me.BookToAdd.Name = txtName.Text
Me.BookToAdd.Author = txtAuthor.Text
End Sub
End Class
表格。
CFBundlePackageType
我不会将主表单实例传递到添加书形式,因为它会在两个表单之间创建紧密耦合,并且添加书表单只能由主表单使用。您可能希望使用应用程序中其他表单的添加书形式。