Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change form from another form.

Status
Not open for further replies.

WelshyWizard

IS-IT--Management
Apr 23, 2006
89
GB
Hi all,

I've got 2 forms, Form1 and Form2.
Pressing a button on Form1 sets Form1.enabled = False and also opens Form2. Pressing a button on Form2 should set Form1.Enabled = True and close itself. Although Form2 does close, Form1 remains disabled.

My code for enabling Form1 is as follows:

Code:
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

        Dim M1 As New Form1

        M1.Enabled = True

        Me.Close()

    End Sub

Does anyone have any tips on how to get this to work and what I'm doing wrong?

Cheers

Today is the tomorrow you worried about yesterday - and all is well.....
 
I think your problem lies in this line:

Dim M1 As New Form1

You already have Form1 open with some name...you don't need to create a new instance of the form.

So your code above should be something like:

Code:
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

        Dim M1 As New Form1

        Form1.Enabled = True
        'if you created an instance using Dim something As Form1 before opening Form1 then replace the Form1 above with something

        Me.Close()

    End Sub

Basically, each time you use the New directive, a new INSTANCE of the form is being opened. So you are never reenabling the Form1 you originally started with.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Oops...clicked Submit too fast...code should be:

Code:
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

        Form1.Enabled = True
        'if you created an instance using Dim something As Form1 before opening Form1 then replace the Form1 above with something

        Me.Close()

    End Sub

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
get rid of this line
Dim M1 As New Form1

and change this line
from M1.Enabled = True to Form1.Enabled = True
 
Thanks guys,

I tried this first time around and for some reason, when I type this in I get the ominous squiggly blue line under Main1.Enabled. When I hover over this the tooltip tells me 'Reference to a non-shared member requires an object refernce'. Any ideas?

Cheers

Today is the tomorrow you worried about yesterday - and all is well.....
 
There are a number of FAQs on this forum covering interaction between Forms.


Hope this helps.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top