.NET Accessing form's controls from another form

Dear sir,
I bought the new book, Professional VB.NET. In that book you have not covered any thing regarding reports.

And also i have the following doubt while programming.

I created one windows Application and I added two forms namely Form1 and Form2.

And I placed one command button on each form.

Form1 is the startup form

Here is the code in form1
''''''''''''''''''''''''''''''''''''''''
'' Form1 Code
''''''''''''''''''''''''''''''''''''''''
Public Class Form1

Inherits System.Windows.Forms.Form

" Windows Form Designer generated code "

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

Dim frm As New Form2()

frm.Show()

End Sub

End Class
'''''''''''''''''''''''''''''''''
''form1 Code end here'''
'''''''''''''''''''''''''''''''''


Now the code in the Form2 is as follows.

'''''''''''''''''''''''''''
'''''''''''''''''form2 Code
''''''''''''''''''''''''''''
Public Class Form2

Inherits System.Windows.Forms.Form

" Windows Form Designer generated code "

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

Dim frm As New Form1()

frm.Button1.Text = "Thomas"

End Sub

End Class
'''''''''''''''''''''''''''
'''form2 code end here'''''
'''''''''''''''''''''''''''


when i clicked the button in Form1 it displays the form2. And when i clicked the button in form2 , it does not chenage the Form1's button's text property?

Please give me the solution as soon as possible

Thomas Rajan

Comments

  • : Dear sir,
    : I bought the new book, Professional VB.NET. In that book you have not covered any thing regarding reports.
    :
    : And also i have the following doubt while programming.
    :
    : I created one windows Application and I added two forms namely Form1 and Form2.
    :
    : And I placed one command button on each form.
    :
    : Form1 is the startup form
    :
    : Here is the code in form1
    : ''''''''''''''''''''''''''''''''''''''''
    : '' Form1 Code
    : ''''''''''''''''''''''''''''''''''''''''
    : Public Class Form1
    :
    : Inherits System.Windows.Forms.Form
    :
    : " Windows Form Designer generated code "
    :
    : Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    :
    : Dim frm As New Form2()
    :
    : frm.Show()
    :
    : End Sub
    :
    : End Class
    : '''''''''''''''''''''''''''''''''
    : ''form1 Code end here'''
    : '''''''''''''''''''''''''''''''''
    :
    :
    : Now the code in the Form2 is as follows.
    :
    : '''''''''''''''''''''''''''
    : '''''''''''''''''form2 Code
    : ''''''''''''''''''''''''''''
    : Public Class Form2
    :
    : Inherits System.Windows.Forms.Form
    :
    : " Windows Form Designer generated code "
    :
    : Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    :
    : Dim frm As New Form1()
    :
    : frm.Button1.Text = "Thomas"
    :
    : End Sub
    :
    : End Class
    : '''''''''''''''''''''''''''
    : '''form2 code end here'''''
    : '''''''''''''''''''''''''''
    :
    :
    : when i clicked the button in Form1 it displays the form2. And when i clicked the button in form2 , it does not chenage the Form1's button's text property?
    :
    : Please give me the solution as soon as possible
    :
    : Thomas Rajan
    :
    :

  • : : Dear sir,
    : : I bought the new book, Professional VB.NET. In that book you have not covered any thing regarding reports.
    : :
    : : And also i have the following doubt while programming.
    : :
    : : I created one windows Application and I added two forms namely Form1 and Form2.
    : :
    : : And I placed one command button on each form.
    : :
    : : Form1 is the startup form
    : :
    : : Here is the code in form1
    : : ''''''''''''''''''''''''''''''''''''''''
    : : '' Form1 Code
    : : ''''''''''''''''''''''''''''''''''''''''
    : : Public Class Form1
    : :
    : : Inherits System.Windows.Forms.Form
    : :
    : : " Windows Form Designer generated code "
    : :
    : : Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    : :
    : : Dim frm As New Form2()
    : :
    : : frm.Show()
    : :
    : : End Sub
    : :
    : : End Class
    : : '''''''''''''''''''''''''''''''''
    : : ''form1 Code end here'''
    : : '''''''''''''''''''''''''''''''''
    : :
    : :
    : : Now the code in the Form2 is as follows.
    : :
    : : '''''''''''''''''''''''''''
    : : '''''''''''''''''form2 Code
    : : ''''''''''''''''''''''''''''
    : : Public Class Form2
    : :
    : : Inherits System.Windows.Forms.Form
    : :
    : : " Windows Form Designer generated code "
    : :
    : : Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    : :
    : : Dim frm As New Form1()
    : :
    : : frm.Button1.Text = "Thomas"
    : :
    : : End Sub
    : :
    : : End Class
    : : '''''''''''''''''''''''''''
    : : '''form2 code end here'''''
    : : '''''''''''''''''''''''''''
    : :
    : :
    : : when i clicked the button in Form1 it displays the form2. And when i clicked the button in form2 , it does not chenage the Form1's button's text property?
    : :
    : : Please give me the solution as soon as possible
    : :
    : : Thomas Rajan


    Hi Thomas your code looks fine, try naming your forms and buttons something different like:

    Dim frmNewform as New Form1
    frmNewform.btnMyButton.Text= "Thomas."


    Dim frmNewForm2 as New Form2
    frmNewForm2.Show()

    Cheers.
    : :
    : :
    :
    :

  • Thomas,

    Your code will not achieve what you want because in the Form2 Button_Click, you are creating a [blue]New[/blue] instance of Form1 and not referring to original instance of Form1. If you add the line
    [code]
    frm.Show
    [/code]
    after you set the Button Text to "Thomas", you will see the second instance of Form1. To achieve what you're trying to do you may need to either use Events or use a Public Collection or ArrayList to keep track of the forms in your application.

    Cheers,
    Chris

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories