日期:2014-05-18 浏览次数:20903 次
private void showI() { textBox1.Text=i.ToString(); }
public partial class Form1 : Form { private UserControl1 uc1 = null; private int i = 0; public Form1() { InitializeComponent(); //初始化用户控件 uc1 = new UserControl1(); //监听按钮单击引发的事件 uc1.AddEvent += new AddEventHandler(uc1_AddEvent); uc1.MinusEvent += new MinusEventHandler(uc1_MinusEvent); } private void ShowI() { textBox1.Text = i.ToString(); } void uc1_MinusEvent() { i--; ShowI(); } void uc1_AddEvent() { i++; ShowI(); } private void Form1_Load(object sender, EventArgs e) { groupBox1.Controls.Add(uc1); } } //以下是UC //声明加1和减1委托 public delegate void AddEventHandler(); public delegate void MinusEventHandler(); public partial class UserControl1 : UserControl { //声明加1和减1事件,即对应委托的实例 public event AddEventHandler AddEvent = null; public event MinusEventHandler MinusEvent = null; public UserControl1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (AddEvent != null) { AddEvent();//触发加1事件 } } private void button2_Click(object sender, EventArgs e) { if (MinusEvent != null) { MinusEvent();//触发减1事件 } } }
------解决方案--------------------
2 楼说的可以
------解决方案--------------------
可不可以将i设为静态的,
public static int i;
public static bool str;
然后在页面放个timer,在click里写:
if(str == true)
{
str == false;
showI();
}
而在用户控件里只需:str = true;
------解决方案--------------------
设置自定义控件属性,赋值给属性
通过委托调用主页面方法,操作属性值
参考