Click here to Skip to main content
15,891,607 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to extract part of HTML source? Pin
DerekT-P5-Nov-20 0:26
professionalDerekT-P5-Nov-20 0:26 
GeneralRe: How to extract part of HTML source? Pin
Member 5697395-Nov-20 3:56
Member 5697395-Nov-20 3:56 
QuestionHow to switch the whole DataGridView Left-To-right? Pin
Alex Dunlop4-Nov-20 7:46
Alex Dunlop4-Nov-20 7:46 
AnswerRe: How to switch the whole DataGridView Left-To-right? Pin
BillWoodruff4-Nov-20 14:21
professionalBillWoodruff4-Nov-20 14:21 
AnswerRe: How to switch the whole DataGridView Left-To-right? Pin
Mycroft Holmes5-Nov-20 10:50
professionalMycroft Holmes5-Nov-20 10:50 
QuestionMoving VS 2019 solution from on-premise to cloud Pin
Member 144746074-Nov-20 7:27
Member 144746074-Nov-20 7:27 
AnswerRe: Moving VS 2019 solution from on-premise to cloud Pin
Richard Andrew x644-Nov-20 14:05
professionalRichard Andrew x644-Nov-20 14:05 
QuestionWinForm - Localize DataGridView with automatical generated columns Pin
Mc_Topaz3-Nov-20 23:08
Mc_Topaz3-Nov-20 23:08 
AnswerRe: WinForm - Localize DataGridView with automatical generated columns Pin
BillWoodruff4-Nov-20 2:35
professionalBillWoodruff4-Nov-20 2:35 
QuestionCsharp InTheHand.Net read bluetooth data on received event Pin
nowis773-Nov-20 8:07
nowis773-Nov-20 8:07 
AnswerRe: Csharp InTheHand.Net read bluetooth data on received event Pin
jsc423-Nov-20 10:33
professionaljsc423-Nov-20 10:33 
GeneralRe: Csharp InTheHand.Net read bluetooth data on received event Pin
nowis7710-Nov-20 21:01
nowis7710-Nov-20 21:01 
QuestionBusiness Layer Question Pin
Kevin Marois2-Nov-20 6:31
professionalKevin Marois2-Nov-20 6:31 
AnswerRe: Business Layer Question Pin
Richard Deeming2-Nov-20 6:45
mveRichard Deeming2-Nov-20 6:45 
GeneralRe: Business Layer Question Pin
Kevin Marois2-Nov-20 7:27
professionalKevin Marois2-Nov-20 7:27 
Questionuse connection from login form in other forms Pin
Newkid on the block31-Oct-20 5:43
Newkid on the block31-Oct-20 5:43 
AnswerRe: use connection from login form in other forms Pin
OriginalGriff31-Oct-20 5:57
mveOriginalGriff31-Oct-20 5:57 
GeneralRe: use connection from login form in other forms Pin
Newkid on the block31-Oct-20 10:21
Newkid on the block31-Oct-20 10:21 
GeneralRe: use connection from login form in other forms Pin
Dave Kreskowiak31-Oct-20 11:26
mveDave Kreskowiak31-Oct-20 11:26 
AnswerRe: use connection from login form in other forms Pin
Dave Kreskowiak31-Oct-20 6:30
mveDave Kreskowiak31-Oct-20 6:30 
AnswerRe: use connection from login form in other forms Pin
BillWoodruff1-Nov-20 15:12
professionalBillWoodruff1-Nov-20 15: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 5:45
Newkid on the block3-Nov-20 5:45 
GeneralRe: use connection from login form in other forms Pin
BillWoodruff3-Nov-20 14:46
professionalBillWoodruff3-Nov-20 14:46 
QuestionCrystal reports and datagridviews Pin
Damir Djordjevic31-Oct-20 4:39
Damir Djordjevic31-Oct-20 4:39 
AnswerRe: Crystal reports and datagridviews Pin
Gerry Schmitz31-Oct-20 5:15
mveGerry Schmitz31-Oct-20 5:15 

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.