Click here to Skip to main content
14,776,114 members
Home » Discussions » C#
   

C#

 
AnswerRe: Business Layer Question Pin
Richard Deeming2-Nov-20 7:45
mveRichard Deeming2-Nov-20 7:45 
GeneralRe: Business Layer Question Pin
Kevin Marois2-Nov-20 8:27
professionalKevin Marois2-Nov-20 8:27 
Questionuse connection from login form in other forms Pin
Newkid on the block31-Oct-20 6:43
MemberNewkid on the block31-Oct-20 6:43 
AnswerRe: use connection from login form in other forms Pin
OriginalGriff31-Oct-20 6:57
mveOriginalGriff31-Oct-20 6:57 
GeneralRe: use connection from login form in other forms Pin
Newkid on the block31-Oct-20 11:21
MemberNewkid on the block31-Oct-20 11:21 
GeneralRe: use connection from login form in other forms Pin
Dave Kreskowiak31-Oct-20 12:26
mveDave Kreskowiak31-Oct-20 12:26 
AnswerRe: use connection from login form in other forms Pin
Dave Kreskowiak31-Oct-20 7:30
mveDave Kreskowiak31-Oct-20 7:30 
AnswerRe: use connection from login form in other forms Pin
BillWoodruff1-Nov-20 16:12
mveBillWoodruff1-Nov-20 16:12 
I Parent, Owner

let me clear something up here:

1) when you declare and instantiate a Form within another Form: they do not have a Parent-Child relationship. It is simply the case at run-time that the instance of the Form that creates the second Form has a valid reference to the instance of the second Form.

2) imho, it's regrettable that it's legal to set a Form to be the Parent of another Form: that leads to a form-within-a-form which is a code smell: you can use the light-weight ContainerControls, like Panel, instead.

3) you can set a Form to be the Owner of another Form ... imho, that is a kind of Parent-Child relationship ... but, all it does is make sure the owned Form appears in front of the owning Form at run-time.
.
II Security in getting and passing data from one Form to another

0) the goal here is to implement security by encapsulation/separation of concerns: all objects/delegates created in the main form Load event do not persist outside that method. the log in form has no "world knowledge" of the main form. there is only one point of access to the log in form in the main form: being able to set the Action delegate 'SendResult in the log in form.

just for the sake of over-kill, we inject the executable code using the lambda format. for a good summary of using Action and Func rather than delegates: [^]

1) code example in a Main Form named Form1's Load EventHandler:
private void Form1_Load(object sender, EventArgs e)
{
    LogInForm loginfrm = new LogInForm();

// declaring and instantiating the login form here means
// it "disappears" ... goes out of scope ... when this method is finished

    string username, password;

    loginfrm.SendResult = (uname, pword) =>
    {
        username = uname;
        password = pword;
    };

// this injects executable code (as a Delegate) into the new log in form

    DialogResult loginResult = loginfrm.ShowDialog();

    if (loginResult == DialogResult.OK)
    {
        // login success
        // user input now  in variables username, password;

        // now validate, make your connection string, etc.

    }
    else if (loginResult == DialogResult.Cancel)
    {
        // user canceled
    }
    else if (loginResult == DialogResult.Abort)
    {
        // login abort: SendResult not defined in login form
    }
}
2) code in the LogInForm
using System;
using System.Windows.Forms;

namespace YourNameSpace
{
    public partial class LogInForm : Form
    {
        public LogInForm()
        {
            InitializeComponent();
        }

        public Action<string, string> SendResult;

//  Action<string, string> is equivalent to void Delegate(string, string)

        private void btnLoginAttempt_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "" && textBox2.Text != "")
            {
                if (SendResult != null) // defensive programming
                {
                    SendResult(textBox1.Text, textBox2.Text); // call the executable code in the main form

                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    // throw error ? do nothing ? or ...
                    this.DialogResult = DialogResult.Abort;  
                }
            }
            else
            {
                // do nothing ? cancel ? put up error warning ?
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
An alternative use of the Delegate/Func: unless the user's log-in data is validated, the log-in form stays open. In real=world code, ypu'd want to keep track of the attempts, and abort the log in after so many attempts;

// in the Main Form
private void Form1_Load(object sender, EventArgs e)
{
    LogInForm loginfrm = new LogInForm();

// declaring and instantiating the login form here means
// it "disappears" ... goes out of scope ... when this method is finished

    string username, password;

    loginfrm.SendResult = SendResult;

// this injects executable code (as a Delegate) into the new log in form

    DialogResult loginResult = loginfrm.ShowDialog();

    if (loginResult == DialogResult.OK)
    {
        // login success

        // user input now  in variables username, password;

        // now validate, make your connection string, etc.

    }
    else if (loginResult == DialogResult.Cancel)
    {
        // user canceled
    }
    else if (loginResult == DialogResult.Abort)
    {
        // login abort: SendResult not defined in login form
    }
}

private bool SendResult(string uname, string pword)
{
    // validate and return true of okay

    username = uname;
    password - pword;

    // validate here and return 'true if success
    return false;

}
the revised log-in form:
using System;
using System.Windows.Forms;

namespace YourNameSpaace
{
    public partial class LogInForm : Form
    {
        public LogInForm()
        {
            InitializeComponent();
        }

        // note change here from Action to Func
        public Func<string, string, bool> SendResult;

        private void btnLoginAttempt_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "" && textBox2.Text != "")
            {
                if (SendResult != null)
                {
                    if (SendResult(textBox1.Text, textBox2.Text))
                    {
                        this.DialogResult = DialogResult.OK;
                    }
                }
                else
                {
                    // throw error, do nothing ? or ...
                    this.DialogResult = DialogResult.Abort;  
                }
            }
            else
            {
                // cancel ? put error warning ?
            }
        }

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

«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali


modified 3-Nov-20 21:08pm.

GeneralRe: use connection from login form in other forms Pin
Newkid on the block3-Nov-20 6:45
MemberNewkid on the block3-Nov-20 6:45 
GeneralRe: use connection from login form in other forms Pin
BillWoodruff3-Nov-20 15:46
mveBillWoodruff3-Nov-20 15:46 
QuestionCrystal reports and datagridviews Pin
Damir Djordjevic31-Oct-20 5:39
MemberDamir Djordjevic31-Oct-20 5:39 
AnswerRe: Crystal reports and datagridviews Pin
Gerry Schmitz31-Oct-20 6:15
mveGerry Schmitz31-Oct-20 6:15 
QuestionIF Else statement on background colour of label. Pin
Member 1497767230-Oct-20 3:29
MemberMember 1497767230-Oct-20 3:29 
SuggestionRe: IF Else statement on background colour of label. Pin
Richard MacCutchan30-Oct-20 3:36
mveRichard MacCutchan30-Oct-20 3:36 
GeneralRe: IF Else statement on background colour of label. Pin
Member 1497767230-Oct-20 3:47
MemberMember 1497767230-Oct-20 3:47 
GeneralRe: IF Else statement on background colour of label. Pin
OriginalGriff30-Oct-20 3:52
mveOriginalGriff30-Oct-20 3:52 
AnswerRe: IF Else statement on background colour of label. Pin
OriginalGriff30-Oct-20 3:50
mveOriginalGriff30-Oct-20 3:50 
GeneralRe: IF Else statement on background colour of label. Pin
Member 1497767230-Oct-20 4:01
MemberMember 1497767230-Oct-20 4:01 
QuestionAdd treasure to my maze game Pin
Member 1497767229-Oct-20 5:10
MemberMember 1497767229-Oct-20 5:10 
AnswerRe: Add treasure to my maze game Pin
OriginalGriff29-Oct-20 5:20
mveOriginalGriff29-Oct-20 5:20 
AnswerRe: Add treasure to my maze game Pin
Gerry Schmitz29-Oct-20 6:14
mveGerry Schmitz29-Oct-20 6:14 
QuestionIs it possible to save DataGridView contents to a file with a specific suffix which can be loaded just by my own application? Pin
Alex Dunlop28-Oct-20 9:09
MemberAlex Dunlop28-Oct-20 9:09 
AnswerRe: Is it possible to save DataGridView contents to a file with a specific suffix which can be loaded just by my own application? Pin
Dave Kreskowiak28-Oct-20 10:21
mveDave Kreskowiak28-Oct-20 10:21 
AnswerRe: Is it possible to save DataGridView contents to a file with a specific suffix which can be loaded just by my own application? Pin
OriginalGriff28-Oct-20 10:27
mveOriginalGriff28-Oct-20 10:27 
AnswerRe: Is it possible to save DataGridView contents to a file with a specific suffix which can be loaded just by my own application? Pin
Gerry Schmitz28-Oct-20 19:31
mveGerry Schmitz28-Oct-20 19:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.