简体   繁体   中英

What is run when you add a control to a form in design view?

I have an error displayed regarding onload event. The error is only displayed when I add the control to a form and try to view the form in design view. Looking at the control itself in design view it's fine. The error is

Object reference not set to an instance of an object.

Stack Trace

at BHCS.ECC.PhysDoc.UI.UserControls.TemplateBaseControl.SetControlVisibility() in C:\TFS10\ECC\Dev\BHCS\ECC\BHCS.ECC\BHCS.ECC\PhysDoc\UI\TemplateBaseControl.cs:line 98
at BHCS.ECC.PhysDoc.UI.UserControls.TemplateBaseControl.ucTemplateBase_Load(Object sender, EventArgs e) in C:\TFS10\ECC\Dev\BHCS\ECC\BHCS.ECC\BHCS.ECC\PhysDoc\UI\TemplateBaseControl.cs:line 476
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Windows.Forms.UserControl.OnLoad(EventArgs e)
at DevExpress.XtraEditors.XtraUserControl.OnLoad(EventArgs e)
at BHCS.ECC.PhysDoc.UI.UserControls.TemplateBaseControl.OnLoad(EventArgs e) in C:\TFS10\ECC\Dev\BHCS\ECC\BHCS.ECC\BHCS.ECC\PhysDoc\UI\TemplateBaseControl.cs:line 1029
at System.Windows.Forms.UserControl.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.ControlCollection.Add(Control value)
at System.Windows.Forms.Form.ControlCollection.Add(Control value)
at System.Windows.Forms.Design.ControlDesigner.DesignerControlCollection.Add(Control c) 

Having objects at design time doesn't make any sense. Why do I have code running in my Form? What is not allowed in the control that causes the error in the form design view?

The error is thrown here:

BHCS.ECC.PhysDoc.UI.UserControls.TemplateBaseControl.SetControlVisibility() in C:\TFS10\ECC\Dev\BHCS\ECC\BHCS.ECC\BHCS.ECC\PhysDoc\UI\TemplateBaseControl.cs:line 98

You need to look at that line to determine why you're getting Object reference not set to an instance of an object. .

The reason you're getting this error in design time when you try to view the form designer is that VS needs to execute your custom control to display it. If there is an error in the code for your custom control, VS can't show anything, and hence reports the error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM