用户控件无效()方法。如何正确地使 UserControl 中的子控件无效
UserControl Invalidate() method. How to properly invalidate the child controls in a UserControl
我已经创建了我的 UserControl class 并且一切正常。只是想知道 Invalidate 的方法是什么。它实际上并没有使我的 class 中的控件失效,而且我在调用它时也没有发现任何效果。谁能解释这个方法在 UserControl classes.
中的作用
要创建一个 UserControl
Invalidate
所有 嵌套的 控件,您需要调用 Invalidate(invalidateChildren)
:
的特殊重载
yourUserControl.Invalidate(true);
Invalidates a specific region of the control and causes a paint
message to be sent to the control. Optionally, invalidates the child
controls assigned to the control.
注意备注:
Remarks
Calling the Invalidate method does not force a synchronous paint; to
force a synchronous paint, call the Update method after calling the
Invalidate method. When this method is called with no parameters, the
entire client area is added to the update region.
更新
如果你想从 UI 线程以外的线程调用它,你需要使用 Invoke
,可能像这样:
public delegate void InvalidateUC();
public InvalidateUC myInvalidateDelegate;
public void InvalidateMethod()
{
yourUserControl1.Invalidate(true);
}
现在,做完之后
myInvalidateDelegate = new InvalidateUC(InvalidateMethod);
您可以从另一个线程调用它
yourForm.yourUserControl1.Invoke(myInvalidateDelegate);
请注意,如果您不确定 ui 调用是否确实来自非 ui 线程,您可以(并且应该)添加条件
if (yourForm.yourUserControl1.InvokeRequired)..
我已经创建了我的 UserControl class 并且一切正常。只是想知道 Invalidate 的方法是什么。它实际上并没有使我的 class 中的控件失效,而且我在调用它时也没有发现任何效果。谁能解释这个方法在 UserControl classes.
中的作用要创建一个 UserControl
Invalidate
所有 嵌套的 控件,您需要调用 Invalidate(invalidateChildren)
:
yourUserControl.Invalidate(true);
Invalidates a specific region of the control and causes a paint message to be sent to the control. Optionally, invalidates the child controls assigned to the control.
注意备注:
Remarks
Calling the Invalidate method does not force a synchronous paint; to force a synchronous paint, call the Update method after calling the Invalidate method. When this method is called with no parameters, the entire client area is added to the update region.
更新
如果你想从 UI 线程以外的线程调用它,你需要使用 Invoke
,可能像这样:
public delegate void InvalidateUC();
public InvalidateUC myInvalidateDelegate;
public void InvalidateMethod()
{
yourUserControl1.Invalidate(true);
}
现在,做完之后
myInvalidateDelegate = new InvalidateUC(InvalidateMethod);
您可以从另一个线程调用它
yourForm.yourUserControl1.Invoke(myInvalidateDelegate);
请注意,如果您不确定 ui 调用是否确实来自非 ui 线程,您可以(并且应该)添加条件
if (yourForm.yourUserControl1.InvokeRequired)..