C# 中调用函数完成后执行函数
我有一个 TextBox,在 LostFocus 事件中我调用一个函数,在这里我需要调用我的函数,在某些情况下必须删除 TextBox,所以当代码返回到 LostFocus 事件时,它给我一个 NullReferenceException 。
那我该怎么办呢?
仅当 LostFocus 函数完成时才可以调用我的函数吗?
谢谢。
你好!感谢您的回答..下面您可以看到我的问题的一个简单的:
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 是一个项目列表,我需要始终将 senseMessage 放在列表的末尾。 因此,当 senseMessage 失去焦点(并准备好在列表的新项目中添加文本)时,我需要删除 senseMessage,添加新项目,然后重新添加 senseMessage。
我希望你能帮助我..
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..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
LostFocus
事件相当低级,并且与 WIN32 api 紧密结合。尝试改用Leave
事件。如果仍然不起作用,请尝试使用 WindowsFormSynchronizationContext 来延迟调用您的函数,如下所示:
或类似的内容。抱歉,如果语法可能不准确,因为我现在运行的是 OS X,所以我没有 VS。
LostFocus
event is quite low-level and bound closely to WIN32 api. Try to useLeave
event instead.If that still won't work, try to use
WindowsFormSynchronizationContext
to delay invoke your function like this:or something like that. Sorry if the syntax might be inaccurate because I'm running OS X now so I have no VS.