Hi somepenguin,
From my experience, we need to call ShowDialog method to pop up a edit form. This the detail reason:
· If we call ShowDialog method, it would return only when the DialogResult property of the form is set or the form is closed. It allows us to do some operations on the form, such as check a check box, then set the DialogResult property to return to the main method and executes the code after the ShowDialog calling. In other words, the main code where we call the ShowDialog method and the code executed in the shown form is in a same UI thread.
· If we call Show method, it would return just after the edit form is shown. It would not wait for us to do some UI operations. For example, if we check a check box on the edit form, it would affect the Area property of the edit form, but it would not affect the Area value we fetched when the Show method returned. This is the code snippet, in which I point out the key issue:
private void GetFootingData(int w, int l, int row, int column)
{
frmFooting ftg = new frmFooting(w, l);
//This would return just after the form is shown.
ftg.Show();
//The value we get here is calculated just after the form is shows.
//The value is indeed calculated only in constructor of frmFooting.
dgvTakeOff.Rows[row].Cells[column].Value = ftg.Area;
}
In other words, the code executed in GetFootingData function and the code executed in frmFooting are not in the same thread.
This is the modified code snippet in which we call ShowDialog method to pop up the edit Form:
Get data method:
private void GetFootingData(int w, int l, int row, int column)
{
frmFooting ftg = new frmFooting(w, l);
//Please call ShowDialog here. This method can return only when you set the DialogResult of the form.
//If you call Show method. It would return rapidly. So whther the checkbox is checked cannot affect the result.
if (ftg.ShowDialog() == DialogResult.OK)
{
dgvTakeOff.Rows[row].Cells[column].Value = ftg.Area;
}
}
The frmFooting form:
public partial class frmFooting : Form
{
int width = 0;
int length = 0;
int area = 0;
public frmFooting(int width, int length)
{
InitializeComponent();
this.width = width;
this.length = length;
}
public int Area
{
get { return area; }
set { area = value; }
}
private void Calculation()
{
int top, bottom;
if (chkTop.Checked == true)
{
top = width * length;
}
else top = 0;
if (chkBottom.Checked == true)
{
bottom = width * length;
}
else bottom = 0;
area = top + bottom;
label1.Text = area.ToString();
}
private void btnOK_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
private void chkTop_CheckedChanged(object sender, EventArgs e)
{
Calculation();
}
private void chkBottom_CheckedChanged(object sender, EventArgs e)
{
Calculation();
}
}
Let me know if this helps.
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.