VB.NET:从第二个表单检索值的最佳方法是什么?

发布于 2024-07-16 08:10:13 字数 427 浏览 3 评论 0原文

我正在自学 VB.Net。

这是我最近遇到的一个问题。 假设我的申请中有一个主 Form1。 Form1 调用第二个 LoginForm(顾名思义)是一个带有用户名/密码类型字段的登录窗口。 预期的行为是 LoginForm 将捕获登录详细信息并将其传递回 Form1。

做这个的最好方式是什么?

在我看来,我正在考虑像“doLogin”这样的函数调用,它将“显示”登录表单,捕获输入的数据,处理表单并返回登录详细信息(可能在某种bean中)。 不知怎的,我不认为这是可能的,

我目前拥有的不太优雅。 LoginForm由Form1模态显示(即showDialog); “me”引用被传递到第二个窗口。 在 LoginForm 上收到用户输入后,我在 Form1 上设置一个值,然后进行处理。

每个人都是这样做的吗?

I'm teaching myself VB.Net.

Here is a problem I have recently come across. Say I have a main Form1 in my application. Form1 calls a second LoginForm which (like the name suggests) is a login window with username/password type fields. Expected behaviour is that LoginForm will capture login details and pass them back to Form1.

What is the best way to do this?

In my mind, I was thinking along the lines of a function call like 'doLogin' that would 'show' the LoginForm, capture the data entered, dispose of the form and return the login details (probably in some kind of bean). Somehow I don't see this as being possible

What I have currently is less elegant. LoginForm is shown by Form1 modally (i.e. showDialog); a 'me' reference is passed to the second window. After user input has been received on LoginForm, I set a value on Form1, then dispose.

Is this the way everybody does it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半城柳色半声笛 2024-07-23 08:10:13

我总是将委托传递给第二个表单,可以调用该委托将第二个表单中的值“传回”到第一个表单中。

这样你就可以避免任何紧密耦合。

经典的观察者模式。


示例实现如下:

向 Form1 添加委托签名。 在 Form1 的按钮单击事件处理程序中,实例化 Form2 类和 Form1 的委托。 将 Form2 的函数分配给委托,并调用委托:

'Form1.vb
Public Delegate Sub delPassData(ByVal text As TextBox)

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
  Dim frm As Form2 = New Form2
  Dim del As delPassData = New delPassData(AddressOf frm.funData)
  del(Me.textBox1)
  frm.Show()
End Sub

在 Form2 中,添加委托将指向的函数。 该函数会将textBox1 的文本分配给label1。

'Form2.vb
Public Sub funData(ByVal text As TextBox)
  label1.Text = text.Text
End Sub

要将数据传回 Form1,只需将 funData 设为返回所需值的函数即可。

I've always passed in a delegate to the second form which can be called to 'pass back' the values from the second form into the first.

That way you are avoiding any tight coupling.

Classic observer pattern.


An example implementation is as follows:

Add a delegate signature to Form1. In Form1's button click event handler, instantiate the Form2 class and Form1's delegate. Assign a function of Form2 to the delegate, and call the delegate:

'Form1.vb
Public Delegate Sub delPassData(ByVal text As TextBox)

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
  Dim frm As Form2 = New Form2
  Dim del As delPassData = New delPassData(AddressOf frm.funData)
  del(Me.textBox1)
  frm.Show()
End Sub

In Form2, add a function to which the delegate will point. This function will assign textBox1's text to label1.

'Form2.vb
Public Sub funData(ByVal text As TextBox)
  label1.Text = text.Text
End Sub

To pass data back to Form1, just make funData a function that returns the values you want.

扮仙女 2024-07-23 08:10:13

您还可以使用“My.Forms”在 VB.NET 中检索数据。

下面从 LoginForm 中的文本框中获取文本,并将其放入主窗体中的文本框中。

Me.RetrievedDataTextBox.Text = My.Forms.LoginForm.Textbox1.Text

You can also retrieve data in VB.NET using "My.Forms"

The below takes the text from a textbox in the LoginForm and puts it in a textbox in the main form.

Me.RetrievedDataTextBox.Text = My.Forms.LoginForm.Textbox1.Text
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文