在 VB.NET 中的表单之间传递对象的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28058459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:41:57  来源:igfitidea点击:

Passing values of an object between forms in VB.NET

vb.netwinformsformsclassproperties

提问by finxie

Here's my problem.
I'm making a project in VB.NET that (currently) exists out of 1 class (let's call it User.vb here) and 2 WinForms (frmDisplay & frmMain). Let's say User.vb is currently looking like this:

这是我的问题。
我正在 VB.NET 中创建一个项目,该项目(当前)存在于 1 个类(这里我们称之为 User.vb)和 2 个 WinForms(frmDisplay & frmMain)中。假设 User.vb 目前看起来像这样:

Public Class User
  Private mName As String

  Public Sub New(ByVal name As String)
    Me.Name = name
  End Sub

  Public Property Name As String
    Get
        Return mName
    End Get
    Set(value As String)
        mName = value
    End Set
  End Property
End Class

Let's also say the form frmDisplay is just a form with a textfield txtString and a button btnSend.

假设表单 frmDisplay 只是一个带有文本字段 txtString 和按钮 btnSend 的表单。

Public Class frmDisplay
   Dim usr As New User()

   Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
      usr.Name = txtString.Text
      frmMain.Show()
      Me.Hide()
   End Sub
End Class

On the form frmMain I want to reach the value in the property Name that I stored in the class User on the first form. The basic idea is (I know it doesn't work):

在表单 frmMain 上,我想获取存储在第一个表单上的类 User 中的属性 Name 中的值。基本思想是(我知道它行不通):

Public Class frmMain
   Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      lblStoredString.Text = usr.Name << This is where I'm stuck
   End Sub
End Class

I googled my problem and read many posts, but I just can't seem to understand it. Maybe you guys can help me.
I am new to VB.NET and WinForm-stuff (about 3 months of exp.), but I have done some programming in the past in C# with webapplications.
Every bit of help is greatly appreciated.

我用谷歌搜索了我的问题并阅读了很多帖子,但我似乎无法理解它。也许你们可以帮助我。
我是 VB.NET 和 WinForm-stuff 的新手(大约 3 个月的经验),但我过去曾用 C# 和 Web 应用程序进行过一些编程。
非常感谢每一位帮助。

Thanks a lot in advance!

非常感谢!

采纳答案by Idle_Mind

Will there only ever be oneUser.Namethat you are interested in throughout the app?

整个应用程序中是否只有一个User.Name您感兴趣?

If yes, then change the class to:

如果是,则将类更改为:

Public Class User
    Public Shared Name As String
End Class

Then you can use User.Namefrom any form (or anywhere in the application) to get/set that value.

然后您可以使用User.Name任何形式(或应用程序中的任何地方)来获取/设置该值。

Note that you can still wrap the field in a property if you like:

请注意,如果您愿意,您仍然可以将该字段包装在一个属性中:

Public Class User

    Private Shared _Name As String

    Public Shared Property Name As String
        Get
            Return _Name
        End Get
        Set(value As String)
            If (value.Trim <> "") Then
                _Name = value.Trim
            End If
        End Set
    End Property

End Class

回答by Michael J. Heier

My focus is ASP.NET, and I prefer C#, but I'll chime in. There are numerous ways of providing data between the forms. The first one that comes to mind is to use a cache of some kind. The idea is that once the cache is made available to your program, you can add the value to the cache when the button is clicked, and then safely read the value whenever you need it. This can be a static class with a Dictionary, or you can look into using the functionality provided by the System.Web.Caching namespace. http://www.codeproject.com/Articles/8977/Using-Cache-in-Your-WinForms-Applicationshas an example.

我的重点是 ASP.NET,我更喜欢 C#,但我会插话。有很多方法可以在表单之间提供数据。想到的第一个是使用某种缓存。这个想法是,一旦缓存对您的程序可用,您就可以在单击按钮时将值添加到缓存中,然后在需要时安全地读取该值。这可以是带有 Dictionary 的静态类,或者您可以研究使用 System.Web.Caching 命名空间提供的功能。 http://www.codeproject.com/Articles/8977/Using-Cache-in-Your-WinForms-Applications有一个例子。

Another way would be to use a shared data source. The concept is similar to the caching, but this would allow you to pass more complex relational data between your forms, assuming your real goal is more complicated than you describe. Here is a walkthrough for that: https://msdn.microsoft.com/en-us/library/ms171925.aspx.

另一种方法是使用共享数据源。这个概念类似于缓存,但是这将允许您在表单之间传递更复杂的关系数据,假设您的真正目标比您描述的更复杂。这是一个演练:https: //msdn.microsoft.com/en-us/library/ms171925.aspx

You could be quick and dirty, and write the values to a text file at some location, and then read the values from the second form.

您可以快速而肮脏,将值写入某个位置的文本文件,然后从第二种形式读取值。

The simplest way is probably to define a custom constructor for the second form, and pass the values you need when you instantiate the second form. This is best suited if the values from the first form can be considered "parameters" to the instance of the second form. Passing a textbox value from one form to another in windows application

最简单的方法可能是为第二个表单定义一个自定义构造函数,并在实例化第二个表单时传递您需要的值。如果可以将第一种形式的值视为第二种形式的实例的“参数”,则这是最合适的。 在Windows应用程序中将文本框值从一种形式传递到另一种形式

回答by SSS

On the other hand, if you want to do it "properly"...

另一方面,如果你想“正确地”做到这一点......

Public Class frmDisplay
  Private usr As User

  Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
    usr = New User(txtString.Text)
    Dim f As New frmMain(Me, usr)
    f.Show()
    Me.Hide()
  End Sub
End Class

and frmMain...

并从主...

Public Class frmMain
  Private myParent As Form
  Private usr As User

  Sub New(parent As Form, _usr As User)

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    usr = _usr
    myParent = parent
  End Sub

  Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Label1.Text = usr.Name
  End Sub

  Private Sub frmMain_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
    myParent.Show()
  End Sub

End Class

Here we instantiate frmMainand pass the User object to its constructor. We also pass the calling form so we can display it again when frmMainis closed.

在这里,我们实例化frmMainUser 对象并将其传递给它的构造函数。我们还传递调用表单,以便我们可以在frmMain关闭时再次显示它。

回答by SSS

Declare the usr variable Friend

声明 usr 变量 Friend

Public Class frmDisplay
  Friend usr As New User()

It will then be available from the other form

然后它可以从其他形式获得

Public Class frmMain
  Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    lblStoredString.Text = frmDisplay.usr.Name
  End Sub
End Class

It's a quirk of VB.NET that forms are automatically created with a public variable name the same as the class name. That's why you are able to use frmMainwithout having to create it (e.g. Dim frmMain as New frmMain). You can turn off this behaviour, but it isn't relevant to your problem.

VB.NET 的一个怪癖是使用与类名相同的公共变量名自动创建表单。这就是为什么您可以使用frmMain而无需创建它(例如Dim frmMain as New frmMain)。您可以关闭此行为,但这与您的问题无关。