merc208 Posted August 22, 2003 Posted August 22, 2003 Hi Guys, I've read all the posts on this forum about this subject but am still totally in the dark. I have been a VB programmer for 8+ years and am really finding 'converting' to .NET difficult so any help would be greatly appreciated! The Problem: I went out and bought SAMS 'Teach Yourself VB.NET in 21 Days' as an 'easy' way to start learning .NET from scratch rather than just playing around with .NET. So far, so good! I'm getting an understanding of it all. The problem came on Day 4 when in the 'Exercises'. It asks to create two forms with Textboxes on each. When one textbox is updated on one form it was to replicate the text to the second. I managed to get it to create two forms, with the required textboxes and when the textbox on the base form (Form1) has text typed in it puts it in the textbox on form2. However what I can't seem to do is get it to change the text in the box on form1. I've tried creating a new object to point to form1 but it just errors.... and I am getting mad with it now! Can anyone out there please sooth my suffering just a little and give me some pointers?! Thanks in advance Sid Quote
Leaders quwiltw Posted August 22, 2003 Leaders Posted August 22, 2003 You could search this forum for "passing forms" and it'll return some relevant results. One of them turned up a link to this which should get you started. If you run into specific problems, post a relevant chunk of code. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchWorkingWithMultipleFormsInVisualBasicNETUpgradingToNET.asp Quote --tim
merc208 Posted August 22, 2003 Author Posted August 22, 2003 OK.... I've had a look around and I'm still stuffed! I've tried every combination of example I can find. This is my code.... Form1 is the startup form. It's creates a new instance of form two and allows the textbox on form2 to be altered to contain the text from the textbox on form1 Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Dim MyForm2 As New Form2() Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged MyForm2.TextBox1.Text = TextBox1.Text End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Create the second form MyForm2.Show() End Sub End Class This works fine! Now on form two is where I am getting confused. I believe I need to pass a reference to Form1 before I am able to change the contents of the textbox on form1. So I create an instance (myForm1 as Form1????????), but when calling it later in the Textchanged event...... I get an error (An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication9.exe. Additional information: Object reference not set to an instance of an object.) I presume because it isn't a new instance.... but if I declare myForm1 as NEW Form1 it doesn't like it either!. Public Class Form2 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Dim myForm1 As Form1 Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged myForm1.TextBox1.Text = TextBox1.Text End Sub End Class Quote
*Gurus* divil Posted August 22, 2003 *Gurus* Posted August 22, 2003 Generally the best way of doing this is to alter the constructor of Form2 so that it accepts an instance of Form1 as a parameter, and then you can store it in the variable you've already declared. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Mehyar Posted August 22, 2003 Posted August 22, 2003 Do this : Just when you want to open Form 2 Dim myform2 as new Form2() Me.AddOwnedForm(myform2) myform2.Show() Now in form 2 do : Ctype(Me.Owner, Form1).TextBox1.Text = "Hello World" Hope this helps, Quote Dream as if you'll live forever, live as if you'll die today
merc208 Posted August 22, 2003 Author Posted August 22, 2003 Errors Thanks for that, but when I run it, it gives me An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication9.exe Additional information: Object reference not set to an instance of an object. As for the previous answer by Divil, would it be possible to give me some example code based upon my own?! Form1 Code Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents TextBox1 As System.Windows.Forms.TextBox <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.TextBox1 = New System.Windows.Forms.TextBox() Me.SuspendLayout() ' 'TextBox1 ' Me.TextBox1.Location = New System.Drawing.Point(8, 112) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(280, 20) Me.TextBox1.TabIndex = 0 Me.TextBox1.Text = "TextBox1" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox1}) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub #End Region Dim MyForm2 As New Form2() Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged MyForm2.TextBox1.Text = TextBox1.Text End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Create the second form MyForm2.Show() End Sub End Class Form2 Code Public Class Form2 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents TextBox1 As System.Windows.Forms.TextBox <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.TextBox1 = New System.Windows.Forms.TextBox() Me.SuspendLayout() ' 'TextBox1 ' Me.TextBox1.Location = New System.Drawing.Point(8, 112) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(272, 20) Me.TextBox1.TabIndex = 0 Me.TextBox1.Text = "TextBox1" ' 'Form2 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox1}) Me.Name = "Form2" Me.Text = "Form2" Me.ResumeLayout(False) End Sub #End Region Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged End Sub Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub End Class Quote
*Experts* mutant Posted August 22, 2003 *Experts* Posted August 22, 2003 This part in your form2: Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub Change it to: Dim firstform As Form1 'variable that will hold the instance of the passed in form Public Sub New(ByVal f1 As Form1) 'accept an object of Form1 type MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call firstform = f1 'assign the passed in instance to a local variable End Sub Then from your second form you can reference your first one using the forstform variable, or whatever you want to call the variable. Then when you declare Form2 in your Form1 then do this: Dim f2 As New Form2(Me) This will pass in the current form to the constructor of Form2. Quote
merc208 Posted August 22, 2003 Author Posted August 22, 2003 OK... I understand that so far but when I've done that and then in to 'textchanged' event attached to the textbox on Form2 I put firstform.TextBox1.Text = "TEST" which I thought would then pass back the string to the textbox on form1 It still gives me..... An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication9.exe Additional information: Object reference not set to an instance of an object. Have I got some problem with my installation of .net?! Sorry, I know I'm being hard work today! Quote
*Experts* mutant Posted August 22, 2003 *Experts* Posted August 22, 2003 Can you show the exact code you have now? Quote
merc208 Posted August 22, 2003 Author Posted August 22, 2003 Yeah, sure..... I added your suggestion 'variable for variable' so now I've got this: Form1 Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents TextBox1 As System.Windows.Forms.TextBox <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.TextBox1 = New System.Windows.Forms.TextBox() Me.SuspendLayout() ' 'TextBox1 ' Me.TextBox1.Location = New System.Drawing.Point(8, 112) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(280, 20) Me.TextBox1.TabIndex = 0 Me.TextBox1.Text = "TextBox1" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox1}) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub #End Region Dim MyForm2 As New Form2(Me) Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged MyForm2.TextBox1.Text = TextBox1.Text End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Create the second form MyForm2.Show() End Sub End Class Form2 Public Class Form2 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Dim FirstForm As Form1 Public Sub New(ByVal f1 As Form1) MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() FirstForm = f1 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents TextBox1 As System.Windows.Forms.TextBox <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.TextBox1 = New System.Windows.Forms.TextBox() Me.SuspendLayout() ' 'TextBox1 ' Me.TextBox1.Location = New System.Drawing.Point(8, 112) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(272, 20) Me.TextBox1.TabIndex = 0 Me.TextBox1.Text = "TextBox1" ' 'Form2 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox1}) Me.Name = "Form2" Me.Text = "Form2" Me.ResumeLayout(False) End Sub #End Region Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged FirstForm.TextBox1.Text = "TEST" End Sub Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub End Class When I run it, It highlights the FirstForm.Textbox1.Text = "Test" and I get the strange error message (as in above posts) Thanks again Quote
*Experts* mutant Posted August 22, 2003 *Experts* Posted August 22, 2003 Ah I see now. What happens is the TextChanged event fires before the instance of that form is set to that variable so the variable itself doesnt contain an instance yet. What you can do is add a TextChanged event handler to the TextBox dynamically after initialization is all done. What other people also use sometimes is a boolean value that will indicate whether the initialization is done and if not dont execute the code in the TextChanged event. Quote
wyrd Posted August 22, 2003 Posted August 22, 2003 (edited) It's quite simple, really. :) There are two easy ways to handle this; Change Public Sub New(ByVal f1 As Form1) MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() FirstForm = f1 'Add any initialization after the InitializeComponent() call End Sub To Public Sub New(ByVal f1 As Form1) MyBase.New() FirstForm = f1 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub Or you can change Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged FirstForm.TextBox1.Text = "TEST" End Sub To Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged If FirstForm Is Not Nothing Then FirstForm.TextBox1.Text = "TEST" End If End Sub It might be a good idea to go ahead and add both changes to your form for extra precaution. Edited August 23, 2003 by wyrd Quote Gamer extraordinaire. Programmer wannabe.
merc208 Posted August 22, 2003 Author Posted August 22, 2003 Thanks!!!!!!!!!!!! Cheers guys!!! With a bit of swapping around I managed to get it to work. I appreciate your time and advice on this issue........ I think this VB6 -> .NET learning 'conversion' is gonna be a long uphill struggle! 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.