Access components on modal form from mainform
-
Hello, In my project I have two forms: frmMainForm (Mdi parent) frmLogon (dialog) - components on frmLogon: (edUserName, edPassword, edDatabase) - TextEdit (btnOk, btnCancel) - Buttons In frmMainForm Load event i wrote this code; { frmLogon Logon = new frmLogon(); Logon.ShowDidalog(); if (Logon.DialogResult == DialogResult.OK) { this is my problem, I don't know how can I get values from edUserName, edPassword... to build connectstring. I mean something like that
string username = Logon.edUserName.Text;
} } I hope you understand me Thank you for answers Anze /*sorry about my bad English*/ -
Hello, In my project I have two forms: frmMainForm (Mdi parent) frmLogon (dialog) - components on frmLogon: (edUserName, edPassword, edDatabase) - TextEdit (btnOk, btnCancel) - Buttons In frmMainForm Load event i wrote this code; { frmLogon Logon = new frmLogon(); Logon.ShowDidalog(); if (Logon.DialogResult == DialogResult.OK) { this is my problem, I don't know how can I get values from edUserName, edPassword... to build connectstring. I mean something like that
string username = Logon.edUserName.Text;
} } I hope you understand me Thank you for answers Anze /*sorry about my bad English*/The best thing to do is to implement your dialog results through properties of the frmLogon, i.e. In your frmLogon, you begin by declaring some private fields
private string userName; private string password; private string databaseName;
Then, you create the properties (also in fmrLogon)public string UserName { get { return userName; } set { userName=value } } public string Password { get { return password; } set { password=value } } public string DatabaseName { get { return databaseName; } set { databaseName=value } }
After this, don't forget to update your variables when the user presses the OK button. Something likeprivate void button1_Click(object sender, System.EventArgs e) { this.UserName=textBox1.Text; this.Password=textBox2.Text; this.DatabaseName=textBox3.Text; }
Having done this, you are now able to access your dialog results in the main form through the properties. For example...frmLogon Logon=new frmLogon; if(Logon.ShowDialog()==DialogResult.Ok) { //Displays the results of the dialog in a textbox in the main form this.myTextBox=Logon.UserName +Logon.Password +Logon.DataBaseName }
:)