views:

57

answers:

1

When a user selects a button a custom user control is added to the form. This user control provides the ability to enter in some values.

How do I wait for the user control to complete before changing the value on my main form?

I was thinking of something like this:

customControl ylc = new customControl();
ylc.Location = new Point(11, 381);
ylc.Parent = this;
ylc.BringToFront();

if(ylc.ShowDialog() == DialogResult.OK)
{
   this.lblSomeText.Text = ylc.PublicPropertyValue
}

UPDATE1

The user control can't be added to its own form. On some forms it is 'embedded-in' and on other forms it is dynamically created as needed.

UPDATE2

This SO link was helpful.

My final solution looks like (I hide the usercontrol when 'completed'):

customControl ylc = new customControl();
ylc.Location = new Point(11, 381);
ylc.Parent = this;
ylc.BringToFront();
ylc.VisibleChanged += new EventHandler(ylc_VisibleChanged);    
ylc.Show();

Then this code goes in the 'Visiblechanged' event:

if(ylc.ShowDialog() == DialogResult.OK)
{
   this.lblSomeText.Text = ylc.PublicPropertyValue
}
+4  A: 

A user control does not really complete does it? I think what you're trying to do might be better served by putting the user control on its own form and calling ShowDialog on that.

msergeant
Yes, depending on what exactly you are trying to do with your control, you should think about placing it in its own form, or instead of inheriting `Control` inherit `Form` instead.
Daniel Joseph
I thought about that but the control is used in different ways on different forms. The one form in question only needs to use the control occasionally. Once the user has entered the value required they can 'close' the usercontrol by hiding it.
John M
Couldn't you just attach to whatever mechanism you're 'closing' the user control with to set your value?
msergeant
@msergeant - I could do that but then my control would need to know which form it was on to find the right label to update.
John M