将文本框信息从一种形式传递到另一种形式

时间:2013-11-29 16:42:17

标签: vb.net textbox

我正在尝试键入一个名称(NameFirst)+(NameLast)将其存储在StudentName中并转移到默认格式NameForm,然后在按下提交按钮后将该信息显示在StudentName中。提交按钮还会打开主窗体“frmMain”

显然,我做错了,因为按下提交后打开的frmMain上的StudentName文本框是空白的。

NameForm

Public Class NameForm

  Public StudentName As String

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


        StudentName = (NameFirst.Text) + (NameLast.Text)

        Dim openForm As frmMain
        openForm = New frmMain()
        openForm.Show()
        openForm = Nothing


    End Sub

    Private Sub ButtonClear_Click(sender As Object, e As EventArgs) Handles ButtonClear.Click
        NameFirst.Text = ""
        NameLast.Text = ""
    End Sub

    Private Sub ButtonExit_Click(sender As Object, e As EventArgs) Handles ButtonExit.Click
        Me.Close()
    End Sub

End Class

frmMain

Option Explicit On
Option Strict On
Option Infer Off



Public Class frmMain



    Private Sub ButtonCalc_Click(sender As Object, e As EventArgs) Handles ButtonCalc.Click

        Dim SumForum As Integer
        Dim SumAssign As Integer
        Dim FAverage As Double
        Dim AAverage As Double

        Dim Grades() As String = {"A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "D-", "F"}


        'Total and then average of Forum grades, displayed in Forum Average text box
        SumForum = CInt(Val(Forum1.Text) + Val(Forum2.Text) + Val(Forum3.Text) + Val(Forum4.Text) + Val(Forum5.Text) + Val(Forum6.Text) + Val(Forum7.Text) + Val(Forum8.Text))

        FAverage = (SumForum / 8)

        ForumAverage.Text = CStr((FAverage))


        'Total and then average of Assignment grades, displayed in Assignment Average text box
        SumAssign = CInt(Val(Assign1.Text) + Val(Assign2.Text) + Val(Assign3.Text) + Val(Assign4.Text) + Val(Assign5.Text) + Val(Assign6.Text) + Val(Assign7.Text) + Val(Assign8.Text))

        AAverage = (SumAssign / 8)

        AssignAverage.Text = CStr(AAverage)

    End Sub




    Private Sub ButtonExit_Click(sender As Object, e As EventArgs) Handles ButtonExit.Click
        Me.Close()
    End Sub



    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        NameForm.StudentName = Me.StuName.Text
    End Sub
End Class

1 个答案:

答案 0 :(得分:2)

  

我刚刚将创业公司改为frmMain。所以当你点击时   frmMain上的StudentName框,NameForm打开,名称可以   进入那里。虽然从NameForm获取名称到frmMain是   还在困扰我

大。在frmMain,你会做这样的事情:

Dim nf As New NameForm
If nf.ShowDialog = DialogResult.OK Then ' <-- code stops here until "nf" is dismissed
    Me.StuName.Text = nf.StudentName
End If

在NameForm中,一旦设置了StudentName字段,只需将DialogResult设置为OK:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    StudentName = NameFirst.Text & NameLast.Text
    Me.DialogResult = DialogResult.OK ' <-- returns execution back to frmMain
End Sub