JLM
JLM

Reputation: 57

Passing Textbox information from one form to another

I'm trying to type a name (NameFirst)+(NameLast) store it in StudentName and transfer to the default form NameForm and then have that information displayed in StudentName once the submit button is pressed. The submit button also opens the main form, "frmMain"

I'm doing something wrong, obviously, as the StudentName textbox on the frmMain which is opened after pressing submit, is blank.

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

Upvotes: 0

Views: 736

Answers (1)

Idle_Mind
Idle_Mind

Reputation: 39132

I changed the startup to frmMain just now. So when you click in the StudentName box on frmMain, the NameForm opens and the name can be entered there. Though getting the name from NameForm to frmMain is still perplexing me

Great. In frmMain, you'd do something like this:

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

Over in NameForm, once you set your StudentName field, you simply set DialogResult to 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

Upvotes: 2

Related Questions