Mez
Mez

Reputation: 2857

How to execute a method after usercontrol is fully visible

I have a usercontrol containing a textbox which i load dynamically onto a form. At the form's start up i initiate the usercontrol and set it's visibility tag to 'false'. I want to trigger a method automatically when the usercontrol becomes visible, since this method writes some output to the textbox this method should only start executing after the usercontrol and all it's inherited controls become visible to the user.

I thought the paintEventHandler should be the last event that gets triggered when a form and its inherited controls gets repainted after eg a control's visibility gets changed.

So subscribing to the paintEventHandler should trigger my subscribed method after the form is fully repainted, but apparently is does not, my method executes while my textbox is still hidden, turning only visible after the method finished executing.

Any thoughts on this?

private void processControl_SetActive(object sender, CancelEventArgs e)
        {

            this.BeginInvoke((MethodInvoker)delegate
           {
               this.Paint += new PaintEventHandler(processControl_Paint);
           });

        }

void processControl_Paint(object sender, PaintEventArgs e)
        {
            //Should only be called when everything is fully loaded and visible on the form.
            //Application.DoEvents()  ->probably bad idea??
            AddStuffToTextBox();
        }

Upvotes: 1

Views: 8993

Answers (5)

David
David

Reputation: 474

Applied solution written by Guillemaune at the first reply! Thanks! Just keep in mind not to throw the function when the object goes not visible (it depends on the case)

private void Control_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) {
    if (e.Property.ToString().Equals("IsVisible") && (bool)e.NewValue)
        doYourStuffHere();
}

Upvotes: 3

Eric
Eric

Reputation: 6425

Since what you really want to do is fill the text box line by line, I'd suggest starting a timer when the VisibleChanged event is fired. In the timer's Tick event, you can add a line of text. If you need to wait longer before the first timer tick, then set the timer's delay longer and then reduce it the first time through the Tick event handler.

Upvotes: 0

Michael A. McCloskey
Michael A. McCloskey

Reputation: 2411

You could try adding the text box initialization code to a handler for the Enter event of the user control, which fires when the control gains input focus and then in your code that activates the control make sure you call the Focus method on the control to set it active. You would probably want to keep a flag so this was done only on the first occurrence of the control gaining input focus.

Upvotes: 2

arbiter
arbiter

Reputation: 9575

You can subscribe to VisibleChanged event of your control or override OnVisibleChanged. To invoke your code only when user control is shown, post method invoke into control message queue. You can also try it with Paint event if VisibleChanged fails in your case, but I do not think so. BTW, why you add event via BeginInvoke?

void OnVisibleChanged(EventArgs e)
{
    //Should only be called when everything is fully loaded and visible on the form.
    //Application.DoEvents()  -> actually bad idea!!
    if (IsHandleCreated)
        BeginInvoke(new MethodInvoker(AddStuffToTextBox));
    base.OnVisibleChanged(e);
}

Upvotes: 0

Guillaume
Guillaume

Reputation: 13138

See VisibleChanged event.

Upvotes: 1

Related Questions