New to VB.net, hence Newbie question

R

Richard Aubin

Hello all,

My name is Richard Aubin and I'm new to VB.NET 2003 programming. (Actually
new to programming period.)

I purchased "Sam's Teach Yourself VB.net 2003 in 21 days" as a starting
point.

My question is:

One of the exercises in the book wants me to create the following:

Build windows application that opens with 2 forms open (form1 & form2)

Each form will have a text box.

By entering text into form1's textbox, the resulting changes will
automatically be applied to form2's textbox.

By entering text info form2's textbox, the resulting changes will
automatically be applied to form1's textbox.

Hence keeping the 2 forms synchronized.

I've been racking my brains for over 2 hours on this one, and can't seem to
come up with a way for form2 to get data from form1.

Getting form1 to fetch data from form2 is no problem.

It's the reverse that I can't figure out.

In form1 I create an object for form2 (Dim frmTwo as new Form2)

But I can't create an object for form1 on form2.

Can anyone point me in the right direction please?

Thanks.

Richard Aubin
 
R

Richard Aubin

Hello Scott,

I already have that.

By changing the text in form2, form1 gets updated as well.

However, I want to be able to change the text in form1.textbox1.text and
update form2.textbox1.text at the same time.
 
G

Guest

August 7, 2004


I have had that type of problem a lot. The trouble is that you
don't have the instance of Form1. I don't know how to get the instance, but I
have found a workaround that I use frequently. The easiest way to do it
would be to put Form2 at the page level. To do this, put the declaration
under the "Windows Form Designer generated code" label, and outside of a sub
or function in Form1. Then in Form2, create a page varible named something
like Form1Text. You can then change this from Form1's textchanged event for
the textbox. You can then access the Form1Text varible from Form2 and that
will contain the text you need.
The following example code assumes that the textbox on each form is called
Textbox1. Form2 will be updated when its Button1 is clicked.

Public Class Form1

Windows Form Designer Generated Code

dim frm2 as new Form2

Private Sub Textbox1_TextChanged(byval sender as ...........)
frm2.Form1Text = Textbox1.text
End Sub

End Class


Public Class Form2

Windows Form Designer Generated Code

Dim Form1Text as string

Private Sub Button1_Click(byval sender as ........)
textbox1.text = Form1Text
End Sub

End Class

Or you could just put this code in Form1's textchanged event...

frm2.textbox1.text = textbox1.text

I hope that my explanation was clear enough!!!!!! If not, just ask again!
Good luck.



Joseph MCP
 
G

Guest

August 7, 2004

After posting my last message and reading your recent one...

Just put the code that I wrote at the end of my last message in the
Textbox1.TextChanged event :

frm2.textbox.text = textbox1.text

This will update Form2's textbox when you change Form1's textbox!
Have a nice day!


Joseph
 
R

Richard Aubin

Hello Joseph,

I kind of understand what it is you are trying to say, unfortunately, I
can't see where Form2 is getting the information from Form1.

Public Class Form1

Inherits System.Windows.Forms.Form

Windows Generated...

Dim frmTwo As New Form2

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
frmTwo.Show()
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged
frmTwo.TextBox1.Text = TextBox1.Text
End Sub

End Class
 
R

Richard Aubin

Joseph, I got that...

Now how do change form1.textbox1.text to reflect the changes typed in
form2.textbox1.text?
 
G

Guest

August 7, 2004

That's a good question and the problem!!!! LOL!! You don't
have the instance of Form1 (like the form2.something that you can use) so I
can't think of a way!!! It is the simple easy things that I can't figure out!
The truth is..

I don't know!! :) I will keep on thinking, for I have the
same problem...



Joseph
 
G

Guest

August 7, 2004

Put the "dim frm2 as new form2" in a module. Then also put in
the module: "dim frm1 as form1". Then in Form1's Load event (double click on
the grid on the form) put:

frm1 = Me

Then use the frm1 as your form1 instance...

In other words, here is the final code...

Public Module Instances
dim frm1 as form1
dim frm2 as new form2
end module

Public class form1
Windows Form .....

private sub Form1_Load(byval .....)
frm1 = me
frm2.show
end sub

private sub Textbox1_textchanged(......)
frm2.textbox1.text = frm1.textbox1.text
end sub
end class

public class form2
windows form ........

private sub Textbox1_textchanged(....)
frm1.textbox1.text = textbox1.text
end sub
end class

That's it!!!!!! Now I will know myself! It took you to get my processor
going and find a solution!


Joseph MCP
 
R

Richard Aubin

Hello Joseph,

I haven't touched Modules as of yet in the lessons in the book: Sams Teach
yourself Visual Basic .net 2003 in 21 days, so I don't think that is the
solution I'm looking for.

The module you want me to create, to I add a module to the current project
or do I add it tor Form1.vb?
 
G

Guest

August 7, 2004

A slight revision to the code is needed. Replace the code in the
module from
dim frm1 as form1 and dim frm2 as new form2 to Public frm1 as form1
and Public frm2 as new form2. I used "dim" unstead of the correct "public.
The code works fine now.

Joseph MCP
 
G

Guest

August 7, 2004

You add the module to the project. You can do this by clicking on
the "Project" menu and clicking on module. You can name the module whatever
you like. I just finished testing the code and it works fine. You will have
to make sure that both textboxes do not have text in them at design time. So
be sure to change their text properties to no text. Remember this workaround,
for you will probably encounter it often. Just remember to create a variable
in a module of the startup form type, and then set it to "variable = Me" in
the load event of the form. I have never been able to solve this problem
until now so this will be very helpful. (It took a lot of posts, though, to
get it right. :) Good luck in your learning!


Joseph MCP
 
B

Brian Henry

incase my zip file didnt post here is the code

form 1 ===============================

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

Friend WithEvents Button1 As System.Windows.Forms.Button

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.TextBox1 = New System.Windows.Forms.TextBox

Me.Button1 = New System.Windows.Forms.Button

Me.SuspendLayout()

'

'TextBox1

'

Me.TextBox1.Location = New System.Drawing.Point(72, 48)

Me.TextBox1.Name = "TextBox1"

Me.TextBox1.TabIndex = 0

Me.TextBox1.Text = "TextBox1"

'

'Button1

'

Me.Button1.Location = New System.Drawing.Point(112, 120)

Me.Button1.Name = "Button1"

Me.Button1.TabIndex = 1

Me.Button1.Text = "Show Form 2"

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(292, 273)

Me.Controls.Add(Me.Button1)

Me.Controls.Add(Me.TextBox1)

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)

End Sub

#End Region

Dim f As Form2



Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged

If Not f Is Nothing Then

f.TextBox1.Text = Me.TextBox1.Text

End If

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

f.Show()

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

f = New Form2(Me)

End Sub

End Class





====== form 2===============================



Public Class Form2

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Private f_parentFrom As Form1



Public Sub New(ByRef parentForm As Form1)

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()



'Add any initialization after the InitializeComponent() call

f_parentFrom = parentForm

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(72, 40)

Me.TextBox1.Name = "TextBox1"

Me.TextBox1.TabIndex = 0

Me.TextBox1.Text = "TextBox1"

'

'Form2

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(292, 273)

Me.Controls.Add(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

If Not f_parentFrom Is Nothing Then

f_parentFrom.TextBox1.Text = Me.TextBox1.Text

End If

End Sub

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

End Sub

End Class
 
O

One Handed Man \( OHM - Terry Burns \)

This works. . . . Not you create an overloaded constructor on form 2 and
pass it the form1 reference when you create it, this way you can refer to
the textbox1 on form 1

Form 1

Public Class Form1
Inherits System.Windows.Forms.Form

Dim F As Form2



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

F = New Form2(Me)

F.Show()

End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged

If Not (F Is Nothing) Then

F.TextBox1.Text = Me.TextBox1.Text

End If

End Sub




Form 2

Public Class Form2

Inherits System.Windows.Forms.Form

Private MyParentForm As Form1



Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged

If Not (MyParentForm Is Nothing) Then

MyParentForm.TextBox1.Text = Me.TextBox1.Text

End If

End Sub





#Region " Windows Form Designer generated code "

Public Sub New(ByVal f As Form1)

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

MyParentForm = f

End Sub


--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top