Leen15
Leen15

Reputation: 113

Execute a function after the caller function finished in C#

I've a TextBox, on LostFocus event of this i call a function, and here i need to call my function that in some situations have to delete the TextBox, so when the code come back to the LostFocus event, it give me a NullReferenceException.

So how can i do?

Is possible to call my Function only when the LostFocus function finished?

Thanks.

Hi! thanks for your answer.. below you can see a simple of my problem:

void senseMessage_LostFocus(object sender, EventArgs e)
{
 ...

 MyFunction();

}



void MyFunction()
{
 ...

 senseList.RemoveItem(senseMessage);

 ... add some other items to senseList...

 senseMessage = new StedySoft.SenseSDK.SensePanelTextboxItem();
 senseMessage.Text = "test";
 senseList.AddItem(senseMessage);

}

senseList is a List of items, i need to have the senseMessage always at the end of the list. So when the senseMessage Lost the focus (and is ready for add the text in a new item of the list) i need to delete senseMessage, add the new item, and reAdd the senseMessage.

i hope you can help me..

Upvotes: 1

Views: 751

Answers (2)

Alex F
Alex F

Reputation: 43311


delegate void VoidDelegate();


void senseMessage_LostFocus(object sender, EventArgs e)
{
    BeginInvoke(new VoidDelegate(MyFunction), new object[]{});
}

Upvotes: 2

tia
tia

Reputation: 9698

LostFocus event is quite low-level and bound closely to WIN32 api. Try to use Leave event instead.

If that still won't work, try to use WindowsFormSynchronizationContext to delay invoke your function like this:

WindowsFormSynchronizationContext.Post(obj => { MyFunction(); }, nil);

or something like that. Sorry if the syntax might be inaccurate because I'm running OS X now so I have no VS.

Upvotes: 0

Related Questions