Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 11th, 2007, 01:39 AM
Registered User
 
Join Date: Jan 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Windows Form Application help

Hi,

i am currently working on an application in C# that allows users to add data to a database, search the database as well as update and delete entries in the database. The problem i'm having is this:

When i click on the update button (in Form 1), a second form opens up prompting a user to enter a password. this is to prevent unauthorised users from updating or deleting the database. In this second form there is only 1 text box in which the user must input the password. After clicking on submit, the form verifies the password. This is where my problem lies. How do i get the original form to proceed with the updating of the database (in Form 1) if the password entry within Form 2 is successful??

I thought of passing back a boolean variable from Form 2 to Form 1. But i just dont know how to go about passing back variables. I know how to pass variables from Form 1 to Form 2 but how do i pass variables from Form 2 to Form 1?? I desperately need help on this. Thanks.

 
Old January 11th, 2007, 02:49 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

There are many ways to do this I'm sure.
My prefered method, because its easy and encapsulated, is to make an event on the second form that executes if the user details are correct.
When you create your Form2 in Form1 wire the Form2 event to to your update process in Form1.
With this method you can create one pasword form and use it for authentication from anywhere.
Passing variables and Form objects around the place is too inter-dependant for my liking, but also works.


======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old January 11th, 2007, 03:01 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Here is a code sample.
The important thing is that Form2 does not need any other form to exist and is not aware of why you want authentication.
This can be modified to to return anything you like from the event, such as an AuthenticationLevel or something lik that.

FORM1
Code:
private void button1_Click(object sender, EventArgs e)
        {
            //Check for validation
            Form2 authenticationForm = new Form2();
            authenticationForm.ShowDialog();
            //Wire the event
            authenticationForm.Authenticated += new OnAuthentication(authenticationForm_Authenticated);
        }

        //This event will be fired when Form2.Authenticated() is called
        void authenticationForm_Authenticated()
        {
            //Your Update Code
        }
FORM2
Code:
//This is the delegate
    public delegate void OnAuthentication();

    public partial class Form2 : Form
    {
        //This is the event
        public event OnAuthentication Authenticated;

        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "YourPasswordTestCode")
            {
                //If the event has been wired, call it.
                //If the event is not wired nothing will happen
                if (Authenticated != null) { Authenticated(); }
            }
            else
            {
                //Password Failure code
            }
        }
    }
======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old January 11th, 2007, 03:08 AM
Registered User
 
Join Date: Jan 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks heaps, that really helped.

 
Old January 11th, 2007, 07:15 AM
Authorized User
 
Join Date: Sep 2003
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

tycotrix,

Greetings. Your question is an excellent one. The most elegent way, in my humble opinion (some might argue otherwise) is to give the second form (the validation form) a public property that will flag whether or not the password was found to be valid. Note that this will likely entail a member variable to hold the boolean flag as well as the property to allow the "outside world" a glimpse at that flag.

I've written a bit of code (I apologize since it's a bit lengthy) illustrating how to handle this. The main form (Form1) has a single button (btnValidatePassword) which when clicked, opens the password validation dialog, as you describe it.

The password validation dialog (PasswordValidationDialog) contains a TextBox (to receive the password input) and two buttons. The first button is an OK button, the second a Cancel button.

When the user clicks the Cancel button, the password validation dialog closes, passing back a value of "DialogResult.Cancel". This is how Form1 knows that the user hit Cancel.

When the user clicks the OK button, the password validation dialog checks to see if the password is valid. It stores the results of this check in the member variable "m_isPasswordValid". Finally, it closes the form, returning the result "DialogResult.OK".

Form1, at this point knows that the user clicked OK, it asks the password validation dialog the results of the validation via the public property "IsPasswordValid".

Nuff of that, here's the code...

Code:
  public partial class Form1 : Form
  {

    #region Event Handlers

    //-------------------------

    private void btnValidatePassword_Click(object sender, EventArgs e)
    {
      PasswordValidationDialog dlg = new PasswordValidationDialog();

      // Show the dialog.  When the dialog is closed, check to see if the user closed it by clicking the OK button.
      if (dlg.ShowDialog(this) == DialogResult.OK)
      {
        if (dlg.IsPasswordValid)
        {
          // Password was valid.
        }

        else
        {
          // Password was invalid.
        }
      }

      else
      {
        // Else the user closed the dialog by hitting the Cancel button.
      }

      dlg.Dispose();
    }

    //-------------------------

    #endregion

  }

  //=========================

  public partial class PasswordValidationDialog : Form
  {

    #region Member Variables

    //-------------------------

    private bool m_isPasswordValid = false;

    //-------------------------

    #endregion

    #region Public Properties

    //-------------------------

    public bool IsPasswordValid
    {
      get { return m_isPasswordValid; }
    }

    //-------------------------

    #endregion

    #region Event Handlers

    //-------------------------

    private void btnCancel_Click(object sender, EventArgs e)
    {
      this.DialogResult = DialogResult.Cancel;
      this.Close();
    }

    //-------------------------

    private void btnOK_Click(object sender, EventArgs e)
    {
      this.AttemptValidation(txtPassword.Text);
      this.DialogResult = DialogResult.OK;
      this.Close();
    }

    //-------------------------

    #endregion

    #region Private Methods

    //-------------------------

    private void AttemptValidation(string password)
    {
      if (password == "This is a valid password")
        m_isPasswordValid = true;

      else
        m_isPasswordValid = false;
    }

    //-------------------------

    #endregion

  }

Cheers.

- Roger Nedel





Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with windows application and DLL naikpalak722 General .NET 0 October 2nd, 2007 03:08 PM
Web Application OR Windows Application adesilva .NET Framework 2.0 2 May 4th, 2007 07:12 AM
Hide application windows when using Pop-Up form mmcdonal Access VBA 4 October 25th, 2005 06:31 AM
Multithreading in c# windows application dhol General .NET 2 September 10th, 2005 02:04 AM
Web application Vs Windows Application Ned .NET Web Services 2 January 20th, 2004 01:27 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.