Passing Value back to the Main Form

G

Guest

Hello, everyone I'm new to C# and I was wondering if someone can tell me how
do I pass a value from 1 form back to the calling routine. What I mean is in
my Parent form I have this code:

private void frmParent_Load(object sender, EventArgs e)
{
//When the form loads show the login prompt for the user to login.
Form loginForm = new frmLogin();
loginForm.ShowDialog();

}

Now in the loginform I have the following:

private bool blnFormFlag;

public bool GetFormFlag
{ get { return blnFormFlag; }
}

private void btnlogin_Click(object sender, EventArgs e)
{
bool blnLoginFlag = false;

//Check for blank username or password.
if (String.IsNullOrEmpty(txtlogin.Text)== true ||
String.IsNullOrEmpty(txtpassword.Text) == true)
{
MessageBox.Show("Blank username/password is not permitted!",
"Invalid Login", MessageBoxButtons.OK,
MessageBoxIcon.Stop);
}
else
{
sqlClass sClass = new sqlClass(); //Create sqlclass object.

//Grab flag variable.
blnLoginFlag = sClass.RequestLogin(this.txtlogin.Text,
this.txtpassword.Text);
if (blnLoginFlag == false)
MessageBox.Show("Username or password is incorrect!",
"Invalid User", MessageBoxButtons.OK,
MessageBoxIcon.Stop);
else
this.Close(); //close the form.
blnFormFlag = true;

}

}

In the calling procedure in the Parent form the method GetFormFlag isn't
made public to the object loginForm. Meaning I tried doing the following and
get an error message after I initiate the object loginForm:

private bool temp;

temp = loginForm.GetFormFlag; //not valid

How can I get the flag back to the Parent form? Thanks.
 
C

Chris Dunaway

Terrance said:
public bool GetFormFlag
{ get { return blnFormFlag; }
}
In the calling procedure in the Parent form the method GetFormFlag isn't
made public to the object loginForm. Meaning I tried doing the following and
get an error message after I initiate the object loginForm:

Since the GetFormFlag is a public propertyof the loginForm class, it
should be available right after the call to ShowDialog. What error are
you getting?

Chris
 
G

Guest

Chris:

That's what I thougtht, but I get an error stating
"System.Windows.Forms.Form" does not contain a definition for 'GetFormFlag'

What am I doing wrong?
 
G

Guest

I tried that in my login form and it doesn't like it. The version I'm using
is C# Express; I don't think this will make a difference but who knows.

Thanks
 
C

Chris Dunaway

Terrance said:
What am I doing wrong?
--
private void frmParent_Load(object sender, EventArgs e)
{
//When the form loads show the login prompt for the user to login.
Form loginForm = new frmLogin();
loginForm.ShowDialog();

}

I think your problem lies in the way you are calling the loginForm.

Instead of

Form loginForm = new frmLogin();

Try using

frmLogin loginForm = new frmLogin();


You were declaring your form as a generic Form object. A Form does not
have the property you added to it. Only the type frmLogin has that
property.

Chris
 
G

Guest

Thanks, a bunch Chris; that worked.
--
TC


Chris Dunaway said:
I think your problem lies in the way you are calling the loginForm.

Instead of

Form loginForm = new frmLogin();

Try using

frmLogin loginForm = new frmLogin();


You were declaring your form as a generic Form object. A Form does not
have the property you added to it. Only the type frmLogin has that
property.

Chris
 

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