well;
Create a C# windows form type application called “ModalModeless” in VS.Net. Add a button in the main form (Form1) with a name of “btnShowLogin” and a text property of “Show Login Dialog”. By right clicking on the project, add another form to the project (Add->Add New Item..). Name this form “LoginDlg.cs”. Put two labels, two text boxes and two buttons in this Login dialog, as shown below.
Name the textboxes as “txtUsername” and “txtPassword” respectively. Set the password character to * for the password text box. Name the first button as “btnLogin”, the second button as “btnCancel”. Since the parent form will need to know the user name and the password entered in the login dialog, you need to add two properties in the LoginDlg class (LoginDlg.cs file) as shown below. Note that, you only are writing the get method for the properties as the parent form will be only reading the data entered by the user.
public class LoginDlg : System.Windows.Forms.Form
{
private string uname;
public string Uname
{
get { return uname; }
}
private string pw;
public string PW
{
get { return pw; }
}
private System.Windows.Forms.TextBox txtUsername;
….
The code for the “Login” and “Cancel” button handlers in the LoginDlg is shown below.
private void btnLogin_Click(object sender, System.EventArgs e)
{
this.uname = txtUsername.Text;
this.pw = txtPawword.text;
this.DialogResult = DialogResult.OK; // also closes the dialog
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
this.DialogResult = DialogResult.Cancel; // also closes the dialog
}
Back to Form1. Write the following handler for the “Show Login Dialog” button.
private void btnShowLogin_Click(object sender, System.EventArgs e)
{
LoginDlg ldg = new LoginDlg();
if (ldg.ShowDialog() == DialogResult.OK)
{
string unm = ldg.Uname;
string pw = ldg.PW;
if ((unm == "windows") && (pw == "cs440"))
MessageBox.Show("Correct login");
else
MessageBox.Show("Incorrect login");
}
Build and test the application.
Modeless Forms:
Modeless forms coexist with the parent application. The call to display a modeless form is Show( ). When the user makes a change to the modeless form e.g., clicks a button, we need to invoke an event handler in the parent form. This can be easily handled by passing a delegate (that points to a member function in the parent form) to the constructor in the child form. The constructor’s code can then set it for a local button.
Example:
Add another form to the “ModalModeless” project. Name this form “MsgDlg.cs”. Design the user interface in this dialog as shown below.
Name the textbox as “txtMsg”. The two buttons are named as “btnSendMsg” and btnClose”. Add a property called msg in the MsgDlg class as shown below.
public string Msg
{
get { return txtMsg.Text; }
}
Add another constructor to the MsgDlg.cs class as shown below.
public MsgDlg(System.EventHandler del)
{
InitializeComponent();
this.btnSendMsg.Click += del;
// set the delegate to a func. in parent form
}
Write the following code for the “Close” button handler in the MsgDlg class.
private void btnClose_Click(object sender, System.EventArgs e)
{
this.Close();
}
The above function sets the click event handler for the “send Message” button to a f
Answered by
Kishore
at
7:53 AM on September 18, 2008