| |

VerySource

 Forgot password?
 Register
Search
View: 936|Reply: 5

C # Dialog form returns some data, (novice level), please help!

[Copy link]

2

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-3-20 16:00:02
| Show all posts |Read mode
Start the main form frmMain and load the login form frmLogin in the Load event of frmMain by the method ShowDialog ().
Now I process some data in frmLogin, such as operator, registration date, etc. How do I pass these data to frmMain when frmLogin executes Close ()?
A piece of code in frmMain looks like this:

    // Show login window
        private void ShowLogin ()
        {
            frmLogin LoginFrm = new frmLogin ();
            DialogResult dr = LoginFrm.ShowDialog ();
            if (dr == DialogResult.OK)
            {
                // TODO: Write the code to be processed after the login form is successfully logged in and closed.
            }
            else
            {
                Close ();
            }
        }

How to write such code?
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-6-26 22:15:01
| Show all posts
First, define some properties in frmMain to receive the data you send from frmLogin;
Then, frmLogin LoginFrm = new frmLogin(this);
In LoginFrm, set the values ​​you want to pass so that they are equal to the attributes defined in frmMain
Reply

Use magic Report

1

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-6-28 11:15:01
| Show all posts
There should be a FormClosing event~~~
Reply

Use magic Report

4

Threads

12

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-7-22 13:00:01
| Show all posts
for example:
//Define the name in the login form, and assign the text of the login id text box to the name
public class frmLogin: Form
    {
        //Define a string and assign it, it can be used as an attribute of class frmLogin
        public static string name;
        private void btn_Login_Click(object sender, System.EventArgs e)
        {
            name = loginidTextBox.Text;
        }
    }

//Called in the main form
public class frmMain: Form
{
    private void MainFrm_Load(object sender, System.EventArgs e)
    {
        frmLogin LoginFrm = new frmLogin();
        LoginFrm.ShowDialog();
        if(LoginFrm.DialogResult == DialogResult.OK)
            {
                //Use the name attribute of the instance LoginFrm of the class frmLogin for assignment
                string newName = LoginFrm.name;
            }
    }
}
Reply

Use magic Report

0

Threads

27

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 China

Post time: 2020-7-22 16:15:02
| Show all posts
See Passing Values ​​Between Forms.
http://blog.csdn.net/ooloife/archive/2006/06/23/824617.aspx
Reply

Use magic Report

2

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-7-26 15:45:01
| Show all posts
Thanks:withsprite, andooloife

The original problem has been solved, I have one more question, let’s ask it, haha ​​:)
I define variables in the frmLogin class:
public int intYear; //Year of registration date
public byte bytePeriod; //The period of the registration date
Call it in frmMain:
//Display the login window
        private void ShowLogin()
        {
            frmLogin LoginFrm = new frmLogin();
            DialogResult dr = LoginFrm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                SLblOperator.Text = LoginFrm.strOperator;
                SLblPeriod.Text = LoginFrm.intYear.ToString() + "year" + LoginFrm.bytePeriod.ToString() + "period";
                SLblRegDate.Text = LoginFrm.strRegDate;
            }
            else
            {
                Close();
            }
        }

There is a warning after compiling: Since "HLLW.frmLogin.intYear" is a field that refers to the marshaled class, accessing the above members may cause runtime exceptions. Also, because "HLLW.frmLogin.bytePeriod" is a field that references the marshaling class, accessing the above members may cause runtime exceptions.
What caused this warning to occur?
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list