2017-02-17 151 views
0

我的英语不是很好如何调用Resize事件在C#

嗨,我是在C#中,我想用2按钮创建开关新的I事件,我想打电话给调整大小,但我可以”吨。我想让自己调整大小。

public abstract class SwitchBase : Control 
    { 
    private Button first; 
    private Button second; 


    public SwitchBase() 
    { 
     InitializeMySwitch(); 
    } 

    private void InitializeMySwitch() 
    { 

     Controls.Add(first = new Button()); 
     Controls.Add(second = new Button()); 

     //first 
     first.Text = "first"; 

     //second 
     second.Text = "second"; 
     second.Location = new System.Drawing.Point(first.Location.X + first.Width, first.Location.Y); 

    } 


    public delegate void ChangedEventHandler(object source, EventArgs args); 

    public event ChangedEventHandler Changed; 

    protected virtual void OnSwitchChanged() 
    { 
     if (Changed != null) 
      Changed(this, EventArgs.Empty); 
    } 

    public delegate void ResizeEventHandler(object source, EventArgs args); 

    public event ResizeEventHandler Resize; 

    protected virtual void OnResize() 
    { 
      Resize(this, EventArgs.Empty); 
    } 

} 



public class Switch : SwitchBase 
{ 
    public Switch() 
    { 

    } 

    protected override void OnSwitchChanged() 
    { 
     base.OnSwitchChanged(); 
    } 

    protected override void OnResize() 
    { 
     base.OnResize(); 
    } 
} 

在另一个按钮更改我的开关

回答

0

的大小从阅读你的代码,据我了解,通过“呼调整大小”你的意思是引发事件。你在做什么是正确的......虽然应该指出的是,通过缺省事件实现,如果没有订户,它将为空...

实际上,另一个线程可能会退订你的背后。因为这个建议是要复制一份。

你能做到这一点,如下所示:

var resize = Resize; 
if (resize != null) 
{ 
    resize(this, EventArgs.Empty) 
} 

应当注意的是,上面的代码将调用用户的事件,但不会造成cotrol来调整。如果你想要的是改变你的控件的大小,那么这样做:

this.Size = new Size(400, 200); 

或者:

this.Width = 400; 
this.Height = 200; 

注意:我不知道是什么Control类你使用。特别是,如果它是System.Windows.Forms.Control它已经有Resize事件,因此您将不会定义您自己的。有可能您正在使用的课程甚至没有SizeWidthHeight

编辑:System.Web.UI.Control没有ResizeSizeWidthHeight。但System.Windows.Controls.ControlWidthHeight甚至认为它没有Resize

+0

好的,我想学习这个事件,因为我想让ChangeSelectEvent类似。 –

+0

@elektronator我有[在我的另一个答案]事件的解释(http://stackoverflow.com/questions/20734477/how-to-change-the-name-of-an-existing-event-handler/20734571 #20734571)可以帮助您处理自定义事件。 – Theraot