Form类using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;public class Form1:Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button button2; private int i=0; public Form1
{
this.button1 = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.button2 = new System.Windows.Forms.Button(); this.button1.Location = new System.Drawing.Point(95, 34);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
//
// panel1
//
this.panel1.Controls.Add(this.button2);
this.panel1.Location = new System.Drawing.Point(68, 93);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(184, 103);
//
// button2
//
this.button2.Location = new System.Drawing.Point(37, 28);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
//
// Form9
//
this.ClientSize = new System.Drawing.Size(292, 268);
this.Controls.Add(this.panel1);
this.Controls.Add(this.button1); //注册事件
this.button1.Click += new System.EventHandler(this.button_Click);
this.button2.Click += new System.EventHandler(this.button_Click);
} private void button_Click(object sender,EventArgs e)
{
i++;
} protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
button1.Text = i.ToString();
button2.Text = i.ToString();
}
}当点击button1时,然后鼠标离开button1,两个Button的text都会显示自增1后的i.
但同样点击button2,然后移开鼠标,两个button的text确没有立即发生改变。这是为什么?
可以想象,button1被点击后,引发了Form1的OnPaint(PaintEventArgs e)方法调用。
为什么button2不可以呢?它被放到Panel1里面就不可以引发Form1的Onpaint方法调用了吗?
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;public class Form1:Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button button2; private int i=0; public Form1
{
this.button1 = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.button2 = new System.Windows.Forms.Button(); this.button1.Location = new System.Drawing.Point(95, 34);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
//
// panel1
//
this.panel1.Controls.Add(this.button2);
this.panel1.Location = new System.Drawing.Point(68, 93);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(184, 103);
//
// button2
//
this.button2.Location = new System.Drawing.Point(37, 28);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
//
// Form9
//
this.ClientSize = new System.Drawing.Size(292, 268);
this.Controls.Add(this.panel1);
this.Controls.Add(this.button1); //注册事件
this.button1.Click += new System.EventHandler(this.button_Click);
this.button2.Click += new System.EventHandler(this.button_Click);
} private void button_Click(object sender,EventArgs e)
{
i++;
} protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
button1.Text = i.ToString();
button2.Text = i.ToString();
}
}当点击button1时,然后鼠标离开button1,两个Button的text都会显示自增1后的i.
但同样点击button2,然后移开鼠标,两个button的text确没有立即发生改变。这是为什么?
可以想象,button1被点击后,引发了Form1的OnPaint(PaintEventArgs e)方法调用。
为什么button2不可以呢?它被放到Panel1里面就不可以引发Form1的Onpaint方法调用了吗?
当然,我只是猜测,是不是点击button1时,它会引发它的Parent(在这儿就是Form1)的OnPaint()方法。
而点击button2时,它引发的是Parent(在这儿是panel1)的OnPaint()方法。因此,button1的text属性得不到立即改变!
{
i++;
}
当你点击按钮的时候。I+1了。当你在点击第二个按钮的时候。执行的值是i+1+1;
没有出现你说的那种情况,
而是点击2个按钮都没反应,
最小化后重绘才显示i的值!所以Paint事件,只有在被最小化,或者窗体被遮盖后,系统才会隐式调用,
click事件是不会调用整个窗体的paint事件,如果把 button1.Text = i.ToString();
button2.Text = i.ToString();
放到button click里面的话,则会显示变化,,这也是个paint事件,不过这个paint事件的绘图区域只是button1,2。而你重写的paint是整个窗体的,这只是我的猜测,
而怎样才会使得显示区域变得无效呢?1,最初建立窗口时,
2,改变窗体大小时,
3,最小化时,
4,窗口被另一窗口遮盖时,还有就是现实调用Invalidate();而Click事件则不会默认产生paint消息