views:

553

answers:

5

Hi All,

I have created two forms in my Windows Application.

One Form acts as a Form and the other form acts as a MODAL DIALOG BOX.

The Form Dialog Box contains a button and One textBox. When this button is clicked the MODAL DIALOGBOX should be displayed. This dialog box also contains One Textbox and Two Buttons(Ok and Cancel).

Now when this dialog box is displayed the TextBox of the dialog box should contain the value entered in the textbox of Form1.

I have used the following coding to accomplish this task. Form1 Coding:

public string UserName;
private void btnFn_Click(object sender, EventArgs e)
{
  UserName = txtUserName.Text;
  frmFnC objFnC = new frmFnC();
  objFnC.ShowDialog();
  objFnC.txtUserName.Text = UserName;
}

Code in MODAL DIALOGBOX OK button:

Please note that the Cancel button is enabled only when the OK button is clicked.

Coding:

private void btnOk_Click(object sender, EventArgs e)
{
    btnCancel.Enabled=true;
}
private void btnCancel_Click(object sender,EventArgs e)
{
  this.Close();
}

The problem I am facing is the value entered by the User in the USERNAME textbox is not displayed in the TEXTBOX in the MODAL DIALOG BOX. Instead it is displaying the textbox as empty.

What should I do to get the values entered by the user in the textbox to this modal dialog box?

Can anybody help me out in performing the desired task?

Thanks in advance!!!

+3  A: 

The problem you've got is that you're showing the dialog before you set the username.

//this shows your dialog
objFnC.ShowDialog(); 
//this won't happen until the dialog is closed
objFnC.txtUserName.Text = UserName;

Because the dialog is modal it won't go to the next line until the dialog is closed. To want to swap those lines round and it'll be fine.

//do this first
objFnC.txtUserName.Text = UserName;
//then show your dialog
objFnC.ShowDialog();

I'd like to point out that exposing the textbox publically isn't a really good idea though. You don't want other classes to have implementational knowledge of your dialog.

It would be better if you added a parameter to the form constructor and then set the textbox text from within that. Then you could do the following:

//get the username
string userName = txtUserName.Text;  
//create a new form passing in the username
frmFnC objFnC = new frmFnC(userName);  
//display the form
objFnC.ShowDialog();

This would be much better style.

DoctaJonez
Thanks for your prompt it is working now as I desired!
sheetal
+1  A: 

In order to be able to set (and get) the contents of the text box in the modal form, add this code to that form:

public string UserName
{
    get { return txtUserName.Text; }
    set { txtUserName.Text = value; }
}

Then, in the other form, you can set the user name:

frmFnC objFnC = new frmFnC();
objFnC.UserName = txtUserName.Text;
objFnC.ShowDialog();

I also need to ask you about the relation between the OK and Cancel buttons in the modal dialog form; it seems a bit wierd the user would need to first click OK, in order to get the Cancel button enabled, and then click Cancel to actually close the form.

I would suggest to not have any event handlers for the click events of these buttons, but instead set the appropriate values of their DialogResult property, and then set the modal form's AcceptButton and CancelButton properties. That way you can check how the dialog was closed:

frmFnC objFnC = new frmFnC();
objFnC.UserName = txtUserName.Text;
if (objFnC.ShowDialog() == DialogResult.OK)
{
    // the user clicked the OK button
}
else
{
    // the user clicked the Cancel button
}
Fredrik Mörk
Thanks for your prompt it is working now as I desired!
sheetal
+1  A: 

Set the text field in the dialogue before calling ShowDialog:

private void btnFn_Click(object sender, EventArgs e)
{
  UserName = txtUserName.Text;
  frmFnC objFnC = new frmFnC();
  objFnC.txtUserName.Text = UserName;
  objFnC.ShowDialog();
}
Richard
Thanks a lot it is working now as I desired!
sheetal
+1  A: 

You need to swap the setting of the text and the ShowDialog:

public string UserName;
private void btnFn_Click(object sender, EventArgs e)
{
  UserName = txtUserName.Text;
  frmFnC objFnC = new frmFnC();
  objFnC.txtUserName.Text = UserName;  // SET THE DATA BEFORE SHOWING THE DIALOG
  objFnC.ShowDialog();
}

or force a redraw of the dialog afterwards.

ChrisF
A: 

Change:

objFnC.ShowDialog();
objFnC.txtUserName.Text = UserName

To:

objFnC.txtUserName.Text = UserName
objFnC.ShowDialog();
Draco