John M
John M

Reputation: 14668

Add something like ShowDialog to a custom User Control?

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
}

Upvotes: 5

Views: 5994

Answers (1)

msergeant
msergeant

Reputation: 4801

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.

Upvotes: 6

Related Questions