Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否未调用TextBox OnPaint方法?_C#_.net_Winforms_Textbox_Onpaint - Fatal编程技术网

C# 是否未调用TextBox OnPaint方法?

C# 是否未调用TextBox OnPaint方法?,c#,.net,winforms,textbox,onpaint,C#,.net,Winforms,Textbox,Onpaint,我已经使用以下代码创建了一个textbox,但是在textbox的任何情况下都不会触发paint方法。您能否建议一种触发OnPaint()的解决方案 您需要在OnPaint protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); ControlPaint.DrawBorder(e.Graphics, this.Bounds, Color.Red, ButtonBorderStyle.Solid);

我已经使用以下代码创建了一个textbox,但是在textbox的任何情况下都不会触发paint方法。您能否建议一种触发OnPaint()的解决方案


您需要在
OnPaint

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    ControlPaint.DrawBorder(e.Graphics, this.Bounds, Color.Red, ButtonBorderStyle.Solid);
}
base.OnPaint()
像往常一样绘制
文本框。如果在调用
base
之前调用
DrawBorder
,则基本实现会再次对其进行超额支付


但是根据,
文本框
不支持
绘制
事件:

此API支持产品基础结构,不打算直接从代码中使用。
在重新绘制控件时发生。此事件与此类无关


因此,Ben Jackon的回答应该可以解决这个问题。

默认情况下不会在文本框上调用OnPaint,除非您通过调用以下内容将其注册为自绘制控件:

SetStyle(ControlStyles.UserPaint, true);

e、 g.从MyTextBox构造函数。

您调试过它没有命中吗?您的
DrawBorder
调用可能没有用,因为在此之后您调用了
base.OnPaint()
。因此,
TextBox
会在您之前绘制的内容上再次绘制自己。这没关系。我要求点击OnPaint方法。我要求触发OnPaint方法。因为这个方法没有被触发。@VenkateshKs好的,Ben得到了答案。我离开这篇文章是因为调用顺序在您的代码中仍然是一个相关的问题。@VenkateshKs如果这回答了您的问题,请将其标记为帮助有类似问题的未来用户的答案。这修复了触发事件问题,但弄乱了textbox的操作方式。这可能调用OnPaint,但现在它由系统以小字体绘制只需一个小小的插入符号,我自己的OnPaint方法就被透支了。切换到RichTextbox允许我自己绘制,但插入符号仍然由系统绘制。
SetStyle(ControlStyles.UserPaint, true);