Calling properties of Form1 into another form (loginform).

jeshra_279

Member
Joined
Oct 28, 2009
Messages
6
Programming Experience
3-5
I have 2 forms, main Form1 and a loginform. When I start the application, first "Form1" is loaded and then with a button click, user can load "loginform" if required to enable/disable some features/controls on Form1.
The sequence is like this:

1) Load main form Form1
2) click a button on Form1
3) Load the loginform.
4) enter user/pwd and click OK button
5) enable/disable "GroupBox" control on Form1 if login sucessful.

For this,
I made a click event for OK button in loginform, so once user clicks the OK button, I want to call one function which is defined in Form1 as a Public, which enables/disables the "GroupBox" control on Form1. The code structure is like this:

in main Form1, i created a function like this:

Class Form1()

Public Function DoThis() ' Function to enable/disable GroupBox control

....some task...

End Function

End

now in loginform, in the click event for OK button:


Class loginform()

Click_OK()
{

DoThis().... 'call function DoThis() of Form1

}

End

I am not able to call the DoThis() function in loginform. Please suggest me with a piece of code how to do this??

Please remember that Form1 is my main form which is loading first. If anyother way is there to make a password protected feature for GroupBox control in Form1, it will be highly appriciated.
Hope my problem is clear..Thanks.
 
That's the wrong way to go about it. The login form should even have to know that the main form exists. In your main form you are presumably displaying the login form by calling its ShowDialog method. what you should be doing is simply notifying the caller that the OK button was clicked, which you would do like this:
VB.NET:
Private Sub okButton_Click() Handles okButton.Click
    Me.DialogResult = Windows.Forms.DialogResult.OK
End Sub
The value that you set will then be returned by ShowDialog. The main forma can test for that value and then call its own method:
VB.NET:
Dim dialogue As New LoginForm

If dialogue.ShowDialog() = Windows.Forms.DialogResult.OK Then
    Me.DoThis()
End If
 
but where to write the 2nd function? in Form1 or on in loginform?
Please let me know as per my code.

Thanks.
Read my post and the code it should be obvious where it goes. My post says:
The main form can test for that value and then call its own method
So if it's the main form calling its own method, how could the code be in the login form? Also, the code is creating a login form and showing it. How could the login form be creating itself? Finally (which may not be so obvious if you don't know what Me means) the code is calling Me.DoThis(). We already know that the DoThis method is in the main form and Me refers to the current instance, so the current instance must be a main form.
 
I am little bit confused, just want to clarify a small thing. Correct me if I am wrong.
I know I have to call this in main Form1, but where? You mentioned the following code to be in main For,1:

Dim dialogue As New LoginForm

If dialogue.ShowDialog() = Windows.Forms.DialogResult.OK Then
Me.DoThis()
End If


But in which function? I am not gettting the complete picture, if You can help on this.
Thanks.
 
I am little bit confused, just want to clarify a small thing. Correct me if I am wrong.
I know I have to call this in main Form1, but where? You mentioned the following code to be in main For,1:

Dim dialogue As New LoginForm

If dialogue.ShowDialog() = Windows.Forms.DialogResult.OK Then
Me.DoThis()
End If


But in which function? I am not gettting the complete picture, if You can help on this.
Thanks.
You tell me where. You're already showing the other form so you already know where. This is code to show the other form. You replace whatever code you're currently using with that, or something like that anyway.
 
Back
Top