简体   繁体   English

如何从一种形式显示名称到另一种形式

[英]How to display name from one Form to another

I dimmed a variable 我变了一个变量

Dim StudentName as String

When the user inputs their name in the TextBox I have: 当用户在文本框中输入其名称时,我有:

txtName.Text = StudentName

This should save the data input by the user into StudentName . 这会将用户输入的数据保存到StudentName
On Form3 Load I have the StudentName data display on the Label as Form3加载中,我在Label StudentName数据显示为

lblnameout.Text = Form1.StudentName

But it always comes out blank. 但它总是空白。 How do I make it so that the name shows? 如何显示名称?

This: 这个:

txtName.Text = StudentName

should be the other way around: 应该相反:

StudentName = txtName.Text

Otherwise, you'll be replacing what the user typed in with what is already in the variable, which would be Nothing . 否则,您将用变量中已经存在的内容(即Nothing替换用户键入的内容。

I also agree with the module route, but you can declare it as a Public Shared string and then modify it as needed. 我也同意模块路由,但是您可以将其声明为“公共共享”字符串,然后根据需要对其进行修改。 Under Public Class Form1 place this line: Public Class Form1放置以下行:

Public Shared StudentName As String

在此处输入图片说明

Now that you have a global variable, you just need to swap the values around like jmcilhinney suggested so the user data gets applied to the string. 现在您有了一个全局变量,您只需要像建议的jmcilhinney那样交换值,以便将用户数据应用于该字符串。

StudentName = txtName.Text

在此处输入图片说明

Now you can go over to Form3 and set your label up to retrieve the value of that string with: 现在,您可以转到Form3并设置标签以使用以下方法检索该字符串的值:

lblnameout.Text = Form1.StudentName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM