Form controls not updated from class methods

I'm new to Visual Studio 2013, so my question is probably - and hopefully - simple.

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

In the main form, there are three TextBoxes (ID #, first name, last name), a button to check if the user exists and a few more buttons (Save and Cancel)

I also created a class (dataLookup), which stores all the functions for adding, updating or deleting users.

The way the program works is as follows:

1.- The user enters the identifier # in the main field of the form ID and clicks the "verify user" button.

2.- The system calls the function stored in the datalookup class, which checks if the identifier # exists.

3.- If so, the function returns the first and last name from the table, assigns them to two local variables (vName and vLastName), fills in the corresponding fields in the main form and returns TRUE. The user can then refresh the data or Cancel (see Code Example below)

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

4.- If ID # does not exist, the function returns FALSE. Then the user can enter new data in three text boxes.

My problem is that I cannot populate TextBox fields from a function stored in the DataLookup class. After processing the instructions, the text fields (which have the value "Enabled" and have the property "Read-only" equal to "false") remain empty.

If I add the same code that fills the fields for the code of the main form and assigns values ​​to the vName and vLastName variables, it works fine:

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

FYI, when compiling / starting the program errors are not reported.

I know that I can change the function so that it also returns the first and last name, and then I can update the TextBox fields from the main form, but I'm just wondering: why can't I do this? what of the function stored in the class?

Hope my description was clear enough :) Any help would be greatly appreciated. Thank you very much in advance!

Randy

+1
source share
1 answer

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

 Public Class MainForm .... 

As a class, an instance must be created, however VB allows you to use the default instance using the class name: MainForm.Show()

Under the hood, the compiler creates an instance of MainForm named MainForm and uses it. It is convenient for developers involved in a hobby in the code, but there are many ways that can bite you:

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

This creates and uses a local instance of Form1 , which is not related to Global Form1 , which was created by VB. The local object goes beyond the scope at the end of the Sub, which will never be used by the rest of the application.

 ' in sub main: Application.Run(New Form1) ' main form/pump ... elsewhere Form1.TextBox1.Text = "hello, world!" 

Despite using the same name, these are actually different instances. The text will not be displayed, and if the next line was Form1.Show() , the second copy of Form1 will be displayed complete with the text "hello, world". They will also create / show new instances:

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

In general, the more complex the application, the less suitable is the use of default instances. For serious applications, create explicit instances of the form:

 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 in various ways, for example, through a property or 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 / start form, this can help create a global link:

 Module Program Friend frmMain As MainForm End Module 

Then set the variable when loading the main form:

 Public Class MainForm Private Sub MainForm_Load(sender ...) frmMain = Me End Sub ... 

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

+1
source

Source: https://habr.com/ru/post/958412/


All Articles