我正在尝试在C#中设计一个用户界面,我的一个子课程的事件遇到了一些问题。
基本上,我有一个类(" WidgetWindow")继承自Form
,并且在方案中,是主表单的 mdiChild 。
接下来,我已经定义了一个" IWidget "继承自UserControl
并从中构建实际小部件的基类(它们继承此类)。
我有三种类型的小部件,对于其中两种小部件,每当父级" WidgetWindow "时,都会正确调用resize事件。 class被修改,但没有为第三个调用事件,除了Load事件。
您对我如何调查原因有什么建议吗?我对.NET
很新,所以我可能做错了。
public partial class WidgetWindow : Form
{
IWidget ctl;
private Point desiredLocation;
private Size desiredSize;
public WidgetWindow()
{
this.SuspendLayout();
InitializeComponent();
this.ClientSize = new System.Drawing.Size(284, 262);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.ResumeLayout();
}
public WidgetWindow(IWidget ctl, Point location, Size size)
: this()
{
this.desiredLocation = location;
this.desiredSize = size;
this.Location = location;
this.Size = size;
mainContainer.Size = new Size(this.Size.Width - 10, this.Size.Height - 10);
mainContainer.Location = new Point(5, 5);
if (ctl != null)
{
this.ctl = ctl;
ctl.Size = new Size(this.Size.Width - 10, this.Size.Height - 10);
ClientSize = ctl.Size;
ctl.Dock = DockStyle.Fill;
mainContainer.Controls.Add(ctl);
ctl.refreshMy();
}
}
}
public partial class IWidget : UserControl
{
// TODO Add it in the future
public virtual event EventHandler<TableDataChangedEventArgs> DataChanged;
protected TableModel data;
protected bool initialized = false;
public IWidget()
{
InitializeComponent();
}
public virtual void refreshMy()
{
;
}
}
public class DigitalReadOut : IWidget
{
internal System.Windows.Forms.Label lblValue;
internal System.Windows.Forms.Label lblCaption;
private int MDID;
private int SDID;
private System.ComponentModel.IContainer components;
private void InitializeComponent()
{
this.SuspendLayout();
//
// DigitalReadOut
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.Name = "DigitalReadOut";
this.ResumeLayout(false);
}
public DigitalReadOut(int MDID, int SDID)
{
this.MDID = MDID;
this.SDID = SDID;
}
public override void refreshMy()
{
this.lblCaption = new System.Windows.Forms.Label();
this.lblValue = new System.Windows.Forms.Label();
//this.SuspendLayout();
//
//lblCaption
//
this.lblCaption.AutoSize = false;
this.lblCaption.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblCaption.Name = "lblCaption";
this.lblCaption.TabIndex = 0;
this.lblCaption.TextAlign = ContentAlignment.MiddleCenter;
this.lblCaption.ForeColor = Color.GreenYellow;
this.lblCaption.BackColor = Color.Black;
//
//lblValue
//
this.lblValue.AutoSize = false;
this.lblValue.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblValue.Name = "lblValue";
this.lblValue.TextAlign = ContentAlignment.MiddleCenter;
this.lblValue.TabIndex = 1;
this.lblValue.Text = "###";
this.lblValue.BackColor = Color.Black;
this.lblValue.ForeColor = Color.GreenYellow;
//
//DigitalReadOut
//
this.Controls.Add(this.lblValue);
this.Controls.Add(this.lblCaption);
this.Name = "DigitalReadOut";
this.BackColor = System.Drawing.Color.Black;
Redraw();
}
private void Redraw()
{
Control c = this;
Graphics g = c.CreateGraphics();
//if we are going to do autosizing of a text box font do it here...
lblValue.Left = 0;
lblValue.Top = 0;
lblValue.Height = (int)(0.66 * (float)c.Height);
lblValue.Width = c.Width - 3;
this.lblCaption.Left = 0;
this.lblCaption.Top = lblValue.Bottom;
//Makes the top of the Caption under the value readout
this.lblCaption.Width = c.Width - 3;
this.lblCaption.Height = (int)(0.33 * (float)c.Height - 3);
if (string.IsNullOrEmpty(lblCaption.Text))
return;
StretchTextValue();
StretchText();
}
PS:为 IWidget 类添加Resize
事件处理程序可以正常工作,对于继承它的其他两个类也是如此。