jace808 Posted July 22, 2003 Posted July 22, 2003 I got this info from the FAQ's at the top of this forum and have been looking at it for over an hour and cannot get this to work. I come from a VB6 background with no OOP knowledge. Global data in .NET Earlier I mentioned that global variables are not available in Visual Basic .NET, and now I'm going to tell you how to make something global. How will you ever trust me after this? Actually, I am not being dishonest in either case; global variables are not allowed in Visual Basic .NET, but you can achieve similar functionality using Shared (called static in C#) class members. A Shared class member, as used by the Visual Basic Upgrade Wizard when it adds the DefInstance property to your forms, is available without creating an instance of a class and, if it is a property, its value is shared across your entire application. You could therefore create a class like this: Public Class myForms Private Shared m_CustomerForm As CustomerForm Public Shared Property CustomerForm() As CustomerForm Get Return m_CustomerForm End Get Set(ByVal Value As CustomerForm) m_CustomerForm = Value End Set End Property End Class When you first create an instance of your form you could store it into this class: Dim myNewCust As New CustomerForm() myNewCust.Show() myForms.CustomerForm = myNewCust After the CustomerForm property has been populated with an instance of your form, you could then use this class to access that same instance from anywhere in your code: Module DoingStuffWithForms Sub DoExcitingThings() myForms.CustomerForm.Text = _ DateTime.Now().ToLongTimeString End Sub End Module Storing your form in this manner comes as close to Visual Basic 6.0's Global variables as you are going to get. The next level of variable scope below this level is class (module, class or form, actually) scope, where a variable is declared within a class and is available to any code in that class, and below that is procedure scope, where a variable is local to a single routine. I put together a "myforms" class like the given example, but I get lost when it says this: When you first create an instance of your form you could store it into this class: What does that mean?? Does it mean the myforms class? If so, where in the class? I have such a migraine right now. :( Quote
*Experts* mutant Posted July 22, 2003 *Experts* Posted July 22, 2003 In the code shown below the text you have trouble understanding they are showing that you first created a shared variable which can be access globally and then what is done in that code is putting an actual instance of the form into the variable that is shared. So now you have a shared variable that contains an instance of the form, that was assigned to it by first creating the instance and then making the shared variable store it. Quote
jace808 Posted July 22, 2003 Author Posted July 22, 2003 I tried that and it didn't like it. I will post the code tommorrow when I have access to it. Maybe it's something else... Quote
Leaders dynamic_sysop Posted July 22, 2003 Leaders Posted July 22, 2003 if you want to pass some info from Form2 , back to Form1 ( to say a TextBox ) you can do this : in Form1 : Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim frm2 As New Form2() Me.AddOwnedForm(frm2) '/// add it as an owned form frm2.Show() '/// show the second form End Sub in Form2 : Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim frm1 As Form1 = Me.Owner frm1.TextBox1.Text = "some text from another form!" End Sub Quote
jace808 Posted July 23, 2003 Author Posted July 23, 2003 Me.AddOwnedForm(frmLog) Gives me a syntax error... any ideas why? Quote
jace808 Posted July 23, 2003 Author Posted July 23, 2003 Ok.. let me break out my code and maybe you guys can get a better idea of what I'm doing. First off I am trying to make a separate logon window pop up after a menu selection. I have made a small function in a module to fade in and out going back and forth between the 2 forms. If you guys have any ideas on a better/easier way of doing what I'm trying to do please let me know. VB.NET is quite difficult for me right now. Public Class frmMain Inherits System.Windows.Forms.Form Dim frmLog As New frmLogin() Me.AddOwnedForm(frmLog) Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged If ComboBox1.SelectedItem = "Shift Operation Manager" Then FadeOut(Me, frmLog) End If If ComboBox1.SelectedItem = "D/3 Developer" Then FadeOut(Me, frmLog) End If End Sub Private Sub frmStart_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Show() frmLog.Show() End Sub End Class Public Class frmLogin Inherits System.Windows.Forms.Form Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim frmMenu As frmMain = Me.Owner FadeOut(Me, frmMenu) End Sub End Class Module Module1 Public Function FadeOut(ByVal FormName1 As Form, ByVal FormName2 As Form) Dim x As Single = 1 Dim y As Single = 0 Do Until x = 0 Application.DoEvents() FormName1.Opacity = x FormName2.Opacity = y x -= 0.01 y += 0.01 Loop End Function End Module Quote
*Experts* mutant Posted July 23, 2003 *Experts* Posted July 23, 2003 Me.AddOwnedForm(frmLog) Gives me a syntax error... any ideas why? From what I see in your code, that line is not in any sub or function (cant do that), put it in your form load event handler. Quote
jace808 Posted July 23, 2003 Author Posted July 23, 2003 Ok.. I just overlooked that and put it in my form load, but I still can't get this to work. I'm going back and forth on ways to achieve what I need to do, but nothing seems to work for me. Why in the world did MS make it so difficult to work with multiple windows? Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.