[英]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. 我正在尝试键入一个名称(NameFirst)+(NameLast)将其存储在StudentName中并转移到默认格式NameForm,然后在按下提交按钮后将该信息显示在StudentName中。 The submit button also opens the main form, "frmMain" 提交按钮还会打开主窗体“frmMain”
I'm doing something wrong, obviously, as the StudentName textbox on the frmMain which is opened after pressing submit, is blank. 显然,我做错了,因为按下提交后打开的frmMain上的StudentName文本框是空白的。
NameForm 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 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
I changed the startup to frmMain just now. 我刚刚将创业公司改为frmMain。 So when you click in the StudentName box on frmMain, the NameForm opens and the name can be entered there. 因此,当您单击frmMain上的StudentName框时,将打开NameForm并在此处输入名称。 Though getting the name from NameForm to frmMain is still perplexing me 虽然从NameForm到frmMain的名字仍然令我感到困惑
Great. 大。 In frmMain, you'd do something like this: 在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
Over in NameForm, once you set your StudentName field, you simply set DialogResult to OK: 在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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.