未从类方法更新的表单控件 [英] Form Controls not updated from Class methods

查看:19
本文介绍了未从类方法更新的表单控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Visual Studio 2013 的新手,所以我的问题可能很简单,希望如此.

I am a newcomer to Visual Studio 2013, so my question is probably -and hopefully- a simple one.

我目前正在编写一个小程序(在 VB 中),主要用于从表中添加/更新/删除用户.

I am currently writing a small program (in VB) that will essentially add/update/delete users from a table.

主表单有三个文本框(ID#、姓名、姓氏)、一个用于检查用户是否已存在的按钮和几个按钮(保存和取消)

The main form has three TextBoxes (ID#, name, last name), a button to check if the user already exists and a couple more buttons (Save and Cancel)

我还创建了一个类 (dataLookup),其中存储了所有用于添加、更新或删除用户的函数.

I have also created a Class (dataLookup) where all the functions for adding, updating or deleting users are stored.

程序的工作方式如下:

1.- 用户在主表单的 ID 字段中输入 ID#,然后单击检查用户"按钮.

1.- The user enters an ID# in the main Form's ID field and clicks on the "check user" button.

2.- 系统调用存储在 datalookup 类中的函数,该函数验证 ID# 是否已存在.

2.- The system calls a function stored in the datalookup Class that verifies if the ID# already exists.

3.- 如果是,该函数从表中检索姓名和姓氏,将它们分配给两个局部变量(vName 和 vLastName),填充主窗体上的相应字段并返回 TRUE.然后用户可以更新数据或取消(请参阅下面的代码示例)

3.- If it does, the function retrieves the name and last name from the table, assigns them to two local variables (vName and vLastName) populates the corresponding fields on the Main Form and returns TRUE. User can then either update data or Cancel (See code sample below)

MainFormName.TextBox1.Text = vName
MainFormName.TextBox2.Text = vLastName
return True

4.- 如果 ID# 不存在,函数返回 FALSE.然后用户可以在三个文本框中输入新数据.

4.- If the ID# doesn't exist, the function returns FALSE. User is then able to enter new data in the three textboxes.

我的问题是我无法从存储在 dataLookup 类中的函数填充 TextBox 字段.处理完指令后,文本框(均已启用且只读属性设置为 false)保持为空.

My problem is that I can't populate the TextBox fields from the function stored in the dataLookup Class. After the instructions are processed, the TextBoxes (which are both Enabled and have their Read Only property set to false) remain empty.

如果我将填充字段的完全相同的代码添加到主表单代码,并将值分配给 vName 和 vLastName 变量,它会完美运行:

If I add the exact same code that populates the fields to the Main Form code, and assign values to the vName and vLastName variables, it works perfectly:

vName = "John"
vLastName = "Doe" 
MainFormName.TextBox1.Text = vName
MainFormName.TextBox2.Text = vLastName

仅供参考,我编译/运行程序时没有报告错误.

FYI, no errors are reported when I compile/run the program.

我知道我可以修改函数,因此它也将返回姓名和姓氏,然后我将能够从主窗体更新文本框字段,但我只是好奇:为什么我不能这样做来自存储在类中的函数?

I am aware that I can modify the function so it will also return the name and last name and then I will be able to update the TextBox fields from the Main Form, but I am just curious: Why can't I do that from the function stored in the Class?

希望我的描述相当清楚:) 任何帮助将不胜感激.非常感谢!

Hope my description was reasonably clear :) Any help will be much appreciated. Many thanks in advance!

兰迪

推荐答案

表单就是类(在每个表单的顶部都这么说):

Forms are classes (it says so at the top of each of them):

Public Class MainForm
  ....

作为一个类,应该创建一个实例,但是,VB 允许使用类名的默认实例:MainForm.Show()

As a class, an instance should be created however, VB allows what is called a default instance using the class name: MainForm.Show()

在幕后,编译器创建一个名为 MainFormMainForm 实例并使用它.这对于涉足代码的开发人员业余爱好者来说很方便,但有很多方法可以让您感到厌烦:

Under the hood, the compiler creates an instance of MainForm named MainForm and uses it. This is handy for developers hobbyists dabbling in code, but there are numerous ways this can bite you:

Sub DoSomeThing()
    Dim frm As New Form1
    frm.TextBox1.Text = cp.ToString
End Sub

这里创建并使用了 Form1local 实例,它与 VB 创建的 Global Form1 无关.本地对象在 Sub 结束时超出范围,永远不会被应用程序的其余部分使用.

Here a local instance of Form1 is created and used which has no relation to the Global Form1 which VB created. The local object goes out of scope at the end of the Sub never to be used by the rest of the app.

' in sub main:
Application.Run(New Form1)     ' main form/pump

... elsewhere
Form1.TextBox1.Text = "hello, world!"

尽管使用相同的名称,但它们实际上是不同的实例.文本将不会显示,如果下一行是 Form1.Show(),则 Form1 的第二个副本将显示完整的hello, world"文本.这些还将创建/显示新实例:

In spite of using the same name, these are actually different instances. The text will not show up and if the next line was Form1.Show(), a second copy of Form1 would display complete with the 'hello, world' text. These will also create/show new instances:

Form2.Show()

' or
Dim frm As New Form2
frm.Show()

<小时>

一般来说,应用程序越复杂,使用默认实例就越不合适.对于严肃的应用,创建显式表单实例:


In general, the more complex the app, the less appropriate using default instances is. For serious apps, create explicit form instances:

Dim myFrm = New Form7()     ' myFrm is an instance of Form7
 ...
myFrm.TextBox1.Text = vName
myFrm.TextBox2.Text = vLastName
myFrm.Show

类或其他形式可以通过各种方式(例如通过属性或构造函数)告知此形式:

Classes or other forms can be told about this form various ways such as via a property or the constructor:

Class Foo
    Private myFrm As Form7

    Public Sub New(frm As Form7)
        myFrm = frm
    End Sub
    ...
End Class

Dim myFoo = New Foo(Me)

对于主/启动表单,可以帮助创建一个全局引用:

For the main/startup form, it can help to create a global reference:

Module Program
    Friend frmMain As MainForm
End Module

然后在主窗体加载时设置变量:

Then set the variable when the main form loads:

Public Class MainForm
   Private Sub MainForm_Load(sender ...)

       frmMain = Me
   End Sub
   ...

frmMain 将是对整个应用程序主窗体的有效引用.

frmMain will be a valid reference to the main form for the entire application.

这篇关于未从类方法更新的表单控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆