TheSwamp

Code Red => .NET => Topic started by: fxcastil on August 10, 2012, 05:09:28 PM

Title: Windows Form Application vs Class Library Application and Userforms
Post by: fxcastil on August 10, 2012, 05:09:28 PM
I created a Windows Form application and Class Library application with one UserForm and One module.
The UserForm name is formTest it contains one textbox.

The Useform is a PUBLIC class in both applications.

I can use ( formTest.TextBox1.Text = "hello" ) in the Windows Form Application but not in the Class Library Application.

Why the difference ???


Code: [Select]

Option Explicit On
Option Strict On

Module Mod_Test
    Sub test()
        formTest.TextBox1.Text = "hello"
    End Sub

End Module

Title: Re: Windows Form Application vs Class Library Application and Userforms
Post by: TheMaster on August 10, 2012, 06:51:02 PM
I created a Windows Form application and Class Library application with one UserForm and One module.
The UserForm name is formTest it contains one textbox.

The Useform is a PUBLIC class in both applications.

I can use ( formTest.TextBox1.Text = "hello" ) in the Windows Form Application but not in the Class Library Application.

Why the difference ???


Code: [Select]

Option Explicit On
Option Strict On

Module Mod_Test
    Sub test()
        formTest.TextBox1.Text = "hello"
    End Sub

End Module


I'm going to assume you're new to VB and or .NET.

You don't show the code in the Winforms Application, only the code from the classlibarary project.

Could it be that the code in the winforms application is in the formTest class?

If so, then the reason it works is because it is a member of the formTest class, and has access to non-public members of that class. The TextBox1 member variable in your form is most-likely declared as Private, which means it is not visible to code outside of the formTest class.
Title: Re: Windows Form Application vs Class Library Application and Userforms
Post by: fxcastil on August 12, 2012, 03:21:12 AM
Quote
You don't show the code in the Winforms Application, only the code from the classlibarary project.
Could it be that the code in the winforms application is in the formTest class?
If so, then the reason it works is because it is a member of the formTest class, and has access to non-public members of that class. The TextBox1 member variable in your form is most-likely declared as Private, which means it is not visible to code outside of the formTest class.

I have created a seperate module, in both applications.  Both applications have the same modue (Mod_Test) in which the code resides.
The userforms are the same in both applications, the userform is a public class. 
I have double checked the userform of both application to ensure they are identical.   
I can't access the properties (buttons and other controls ) of a public class userform in the seperate module of the Class Library Application.













Title: Re: Windows Form Application vs Class Library Application and Userforms
Post by: Kerry on August 12, 2012, 03:57:40 AM

Perhaps you should post the full solution.
.. or a cut down version that displays the problem you are having.
Title: Re: Windows Form Application vs Class Library Application and Userforms
Post by: fxcastil on August 13, 2012, 01:14:15 PM
Here are the two projects
Title: Re: Windows Form Application vs Class Library Application and Userforms
Post by: Keith™ on August 13, 2012, 02:11:20 PM
Let me get this straight, you want to set the value of the textbox on the form from a class.

You should understand that a form is a class within itself. To access a member of a class, you should use a public property or method. This ensures encapsulation.

In the class formTest add a public property.
Code - Visual Basic: [Select]
  1.     Public Property TextBoxValue As String
  2.         Get
  3.             Return Me.TextBox1.Text
  4.         End Get
  5.         Set(ByVal value As String)
  6.             Me.TextBox1.Text = value
  7.         End Set
  8.     End Property

This allows you to access the value of the textbox from outside the class.

Then in your class1 class, you can access the value like this:
Code - Visual Basic: [Select]
  1. 'This sets the value
  2. formTest.TextBoxValue = "some value"
  3.  
  4. 'this retrieves the value
  5. Dim MyVar as String = formTest.TextBoxValue
Title: Re: Windows Form Application vs Class Library Application and Userforms
Post by: Keith™ on August 13, 2012, 02:19:01 PM
Oh, you could also declare your controls as Friend and then declare your form as that class as opposed to the base class of Form.

Code - Visual Basic: [Select]
  1.         Dim myTestForm As formTest = New formTest
  2.         myTestForm.TextBox1.Text = "some value"
  3.         myTestForm.ShowDialog()
  4.         Dim Myvar As String = myTestForm.TextBox1.Text
Title: Re: Windows Form Application vs Class Library Application and Userforms
Post by: Jeff H on August 13, 2012, 02:52:20 PM
It is a bunch of crap that VB does behind the scenes you can access the form controls from just about anywhere in the application.
 
You can fill the textbox like this
Code - Visual Basic: [Select]
  1.  
  2.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  3.         Dim cls As New Class1()
  4.     End Sub
  5. ....
  6. .....
  7. ......
  8. ....
  9.  
  10.  Public Class Class1
  11.     Sub New()
  12.         formTest.TextBox1.Text = "New"
  13.     End Sub
  14. End Class
  15.  

The reason you can not access the forms controls in the other project is you do not have the form set as Startup object which VB does a bunch of crap behind the scenes Main method which VB does .
 
Vb creates a main method and sets the startup object and a bumch other crap but you can google it.
 
To not be able to access like that you can also go to properties click on application tab and set Startup object to Main and add this to class
 
Code - Visual Basic: [Select]
  1.  Public Shared Sub Main()
  2.         Dim frm As New formTest
  3.         Application.Run(frm)
  4.     End Sub
  5.  

 
Title: Re: Windows Form Application vs Class Library Application and Userforms
Post by: Keith™ on August 13, 2012, 02:58:59 PM
Interesting

It would seem to me that formTest class is created by class1, not the other way around ... although you can do it that way.

Usually a developer would create a form class that can be called and manipulated from another class, like the open file dialog and the print dialog.
Title: Re: Windows Form Application vs Class Library Application and Userforms
Post by: fxcastil on August 13, 2012, 03:05:44 PM
I really wanted to know why the scope of the formTest appears to be global in the Windows from applicaton and local in the Class Library Application.

Where is the difference in the sample programs.

The formTest is defined as a Public Class in both applications.







Title: Re: Windows Form Application vs Class Library Application and Userforms
Post by: Jeff H on August 13, 2012, 03:05:59 PM
I new in VB it created the Main for you but never knew it made the startup form shared.
 
I just did a quick and search and people post some similar code and get replies that they need a instance of the form to access the controls which I thought but it sure as hell builds and works
 
 
That felt really weird typing that code.
Title: Re: Windows Form Application vs Class Library Application and Userforms
Post by: Jeff H on August 13, 2012, 03:15:36 PM
Nevermind
 
when you use the same name as the form it is a shortcut to My.Forms.FormName and if you change the class library to a window form app them it will add it to settings file but it is not global but a shortcut to a shared method that makes look like one
Title: Re: Windows Form Application vs Class Library Application and Userforms
Post by: fxcastil on August 13, 2012, 03:24:11 PM
Keith and Jeff thanks for your input.  Keith I understand you example and it was very concise.

But again why the difference the two  types of application (attached) ?

Where is the textbox property of the userform defined/exposed as a public property in the WindowsFormApplication?

I found this example about userforms in VB.net and VB.
It talks about userforms in Vb.net not having a global scope
But again (being redundant) the WindowsFormApplication.NET does have global scope of Userforms, but the ClassLibraryApplication.Net does not have global scope of Userforms.

http://msdn.microsoft.com/en-us/library/aa289529(v=VS.71).aspx

Title: Re: Windows Form Application vs Class Library Application and Userforms
Post by: Jeff H on August 13, 2012, 04:07:31 PM
The CLR/CLS offers a set of features and it allows the creation of global variables.
 
Neither C# or VB expose all of the CLR/CLS functionality and global variables is feature NOT exposed by either of them.
 
 
VB hides and does a bunch of things behind the scence.
 
Your application must have an entry point have wondered why you do not see a main?
 
When you type the name of a form class in VB it creates a shortcut to

(http://i1221.photobucket.com/albums/dd473/Jeffrey_H/myforms.png)
 This code

Code - Visual Basic: [Select]
  1.     Sub test()
  2.         formTest.TextBox1.Text = "hello"
  3.     End Sub
  4.  

is changed to this as shown in reflector
Code - Visual Basic: [Select]
  1.  
  2.  Friend NotInheritable Class ModuleTest
  3.     ' Methods
  4.    Public Shared Sub test()
  5.         MyProject.Forms.formTest.TextBox1.Text = "hello"
  6.     End Sub
  7. End Class
  8.  
  9.  

It replaces formTest with MyProject.Forms.formTest
 
The other project is a class library and the MY object depends on project type
How My Depends on Project Type  (http://msdn.microsoft.com/en-US/library/ms172698(v=vs.80).aspx)
From Link
Quote

 My.Forms object is available in a Windows Forms application but not available in a console application
Title: Re: Windows Form Application vs Class Library Application and Userforms
Post by: fxcastil on August 13, 2012, 04:34:37 PM
Jeff,

Now we're getting somewhere !!!!

I will have to save the table which shows how My Depends on Project Type 

http://msdn.microsoft.com/en-US/library/ms172698(v=vs.80).aspx

Thanks for information, the mystery is solved.

This is recommended reading for all.



Title: Re: Windows Form Application vs Class Library Application and Userforms
Post by: Jeff H on August 13, 2012, 04:40:55 PM
It threw me for a loop and did not notice it until I looked at it in reflector as I was completely wrong as you can see in eairler post.
Once I saw the MyProjects object then it was just typing the correct words in Google "VB My object".
 
Searching "Accessing forms control from module" or "Window form shared", etc.. does not get you close.