Author Topic: Windows Form Application vs Class Library Application and Userforms  (Read 5684 times)

0 Members and 1 Guest are viewing this topic.

fxcastil

  • Guest
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


TheMaster

  • Guest
Re: Windows Form Application vs Class Library Application and Userforms
« Reply #1 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.
« Last Edit: August 10, 2012, 09:23:50 PM by TT »

fxcastil

  • Guest
Re: Windows Form Application vs Class Library Application and Userforms
« Reply #2 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.














Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Windows Form Application vs Class Library Application and Userforms
« Reply #3 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.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

fxcastil

  • Guest
Re: Windows Form Application vs Class Library Application and Userforms
« Reply #4 on: August 13, 2012, 01:14:15 PM »
Here are the two projects

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Windows Form Application vs Class Library Application and Userforms
« Reply #5 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
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Windows Form Application vs Class Library Application and Userforms
« Reply #6 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
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Windows Form Application vs Class Library Application and Userforms
« Reply #7 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.  

 
« Last Edit: August 13, 2012, 02:55:55 PM by Jeff H »

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Windows Form Application vs Class Library Application and Userforms
« Reply #8 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.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

fxcastil

  • Guest
Re: Windows Form Application vs Class Library Application and Userforms
« Reply #9 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.








Jeff H

  • Needs a day job
  • Posts: 6144
Re: Windows Form Application vs Class Library Application and Userforms
« Reply #10 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.

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Windows Form Application vs Class Library Application and Userforms
« Reply #11 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

fxcastil

  • Guest
Re: Windows Form Application vs Class Library Application and Userforms
« Reply #12 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

« Last Edit: August 13, 2012, 03:41:39 PM by fxcastil »

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Windows Form Application vs Class Library Application and Userforms
« Reply #13 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


 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
From Link
Quote

 My.Forms object is available in a Windows Forms application but not available in a console application
« Last Edit: August 13, 2012, 04:27:23 PM by Jeff H »

fxcastil

  • Guest
Re: Windows Form Application vs Class Library Application and Userforms
« Reply #14 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.



« Last Edit: August 13, 2012, 04:44:52 PM by fxcastil »

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Windows Form Application vs Class Library Application and Userforms
« Reply #15 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.