好的,所以
问题 - 如何调用存储在frmResult中的程序,以便frmMenu可以“看到”它。
Public Sub btnExport_Click(sender As Object, e As EventArgs) Handles btnExport.Click
Dim fileSaved As Boolean
Dim filePath As String
Do Until fileSaved
'Request filename from user
Dim saveFile As String = InputBox("Enter a file name to save this message")
'Click Cancel to exit saving the work
If saveFile = "" Then Exit Sub
'
Dim docs As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
filePath = IO.Path.Combine(docs, "Visual Studio 2013\Projects", saveFile & ".txt")
fileSaved = True
If My.Computer.FileSystem.FileExists(filePath) Then
Dim msg As String = "File Already Exists. Do You Wish To Overwrite it?"
Dim style As MsgBoxStyle = MsgBoxStyle.YesNo Or MsgBoxStyle.DefaultButton2 Or MsgBoxStyle.Critical
fileSaved = (MsgBox(msg, style, "Warning") = MsgBoxResult.Yes)
End If
Loop
'the filePath String contains the path you want to save the file to.
Dim rtb As New RichTextBox
rtb.AppendText("Generation, Num Of Juveniles, Num of Adults, Num of Semiles, Total" & vbNewLine)
For Each saveitem As ListViewItem In ListView1.Items
rtb.AppendText(
saveitem.Text & ", " &
saveitem.SubItems(1).Text & ", " &
saveitem.SubItems(2).Text & ", " &
saveitem.SubItems(3).Text & ", " &
saveitem.SubItems(4).Text & vbNewLine)
Next
rtb.SaveFile(filePath, RichTextBoxStreamType.PlainText)
End Sub
答案 0 :(得分:2)
这是你的意思吗?
Public Sub Init()
'... (all the code)
End Sub
Private Sub Results_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call Init()
End Sub
您可以从所需的每种表单加载中调用Init()
。也许你想创建一个模块并存储你的方法。
顺便说一下,当你的方法需要返回一个值时,你只能使用Function
。
如果您的代码使用表单的元素(或其他对象不是常量),您需要将它们传递给方法,如下所示:
Public Sub Init(myListView As ListView)
myListView.Items.Add("something")
'...
End Sub
Private Sub Results_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call Init(ListView1)
End Sub
您可以根据需要添加任意数量的参数。在进一步学习之前,你必须先学习基础知识。
答案 1 :(得分:0)
对于那些像我一样苦苦挣扎的人来说,这是literature的一大部分。 您可以编写代码来访问不同表单上的对象。但是,必须通过在对象名称前加上对象的名称来完全标识对象的名称。
Dim resultsForm As New frmResults
resultsForm.lblAverage.Text = sngAverage.ToString()
在这些语句中,例如,我已经声明了一个名为resultsForm的对象变量,该变量链接到frmResults形式。第二个语句将sngAverage的字符串值分配给frmResults表单上的lblAverage标签框。
在我的代码中,我需要更改说:
的行For Each saveitem As ListViewItem In ListView1.Items
对此:
For Each saveitem As ListViewItem In Results.ListView1.Items
我还需要确保将formResults上的过程设为Public not Private