RESOLVED: Turned out to be a visual studio problem. Closed visual studio, cleaned and rebuilt, and the value started showing. Thanks all for the help, sounds like I need to switch to VS2010.
This may not be the best, safest, or preferred way to pass values between forms, but this is the way I am attempting for the moment. So, please do help me to get this way working. After you provide an answer, you're more than welcome to add in some better ways of doing this.
The problem is, when the modal dialog box closes and I go back to the owner, the textbox value from the modal is an empty string rather than the actual value. I've read in several places this should not be the case, as the data should persist even after the modal box disposes. Here's my code.
public partial class PreferencesForm : Form
{
public PreferencesForm()
{
InitializeComponent();
}
private void okButton_Click(object sender, EventArgs e)
{
if (masterRadioButton.Checked == true)
{
if (password1TextBox.Text != password2TextBox.Text)
{
errorLabel.Text = "Passwords do not match, please re-enter both passwords and try again.";
this.Refresh();
}
else if (password1TextBox.Text == "" && password2TextBox.Text == "")
{
errorLabel.Text = "You must enter a password.";
}
else
{
okResultButton_Click(null, null);
}
}
else if (single开发者_开发知识库RadioButton.Checked == true)
{
okResultButton_Click(null, null);
}
}
private void cancelButton_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Dispose();
}
private void okResultButton_Click(object sender, EventArgs e)
{
// invisible button
this.DialogResult = DialogResult.OK;
this.Dispose();
}
And here is the code that calls the above form as a modal dialog box.
private void setPreferencesToolStripMenuItem_Click(object sender, EventArgs e)
{
PreferencesForm pf = new PreferencesForm();
DialogResult result = pf.ShowDialog();
if (result == DialogResult.OK)
{
if (pf.password1TextBox.Text != "")
{
masterPassword = pf.password1TextBox.Text;
}
else
{
masterPassword = null;
}
}
}
Thanks for any assistance. I'm getting pretty frustrated over here. >:(
Note: The ReadOnly property of the password1TextBox variable is correctly shown as true or false, depending on what I select in the modal form, but the text property will still not correctly display.
I'm guessing that Dispose
will also dispose the controls it contains. After the controls have been disposed, the text is likely no longer valid either. Try Close
rather than Dispose
and then Dispose
in the caller.
You should listen to the people answering your question. Dispose is supposed to clear out memory allocated, it doesn't matter if you can still get the ReadOnly property.
Don't call Dispose in the form, call dispose from the calling code, as in the example code from the ShowDialog method documentation (http://msdn.microsoft.com/en-us/library/c7ykbedk.aspx#Y851). Note that Dispose is called just before the testDialog variable goes out of scope.
public void ShowMyDialogBox()
{
Form2 testDialog = new Form2();
// Show testDialog as a modal dialog and determine if DialogResult = OK.
if (testDialog.ShowDialog(this) == DialogResult.OK)
{
// Read the contents of testDialog's TextBox.
this.txtResult.Text = testDialog.TextBox1.Text;
}
else
{
this.txtResult.Text = "Cancelled";
}
testDialog.Dispose();
}
I propose just save the string of the control of your Dialog into string property, and retrieve value of that class property and not control's property value after Dialog is closed, and stop worrying about Dispose
or not Dispose
, or whatever else.
Hope this helps
精彩评论