我无法将UserControl添加到窗体

时间:2019-10-04 14:06:22

标签: c# visual-studio user-controls

我有问题。我无法将UserControl添加到我的主窗体中。我只能添加1个名为“昵称”的UserControl,但不能添加第二个名为“ usermain”的UserControl。当我尝试添加“ usermain” UserControl时,出现此错误:

Unable to create component 'usermain'. Error message: 'System.NullReferenceException: Object reference not set to an object instance in IMP View.usermain.Usermain _Load(Object sender, EventArgs e) in C: \ Users \ Robert\ source \ repos \TMP View\ usermain.cs:line 29 in System.Windows.Forms.UserControl.OnLoad(EventArgs e) in System.Windows.Forms.UserControl.OnCreateControl0 in System.Windows.Forms.Control.CreateControl(Boolean flgnoreVisible) in System.Windows.Forms.Control.CreateControl0 in System.Windows.Forms.Control.ControlCollection.Add(Contro I value) in System.Windows.Forms.Design.ParentControlDesigner.AddCh ildControl(Control newChild) in System.Windows.Forms.Design.ParentControlDesigner.AddCo ntrol(Control newChild, (Dictionary defaultValues) in System.Windows.Forms.Design.ControlDesigner.lnitializeNew Component(IDictionaty defaultValues) in System.Drawing.Design.ToolboxItem.CreateComponentsCore(1 DesignerHost host, (Dictionary defaultValues) in System.Drawing.Design.ToolboxItem.CreateComponents(IDesi gnerHost ho...' 

我不知道该怎么办...

在“ usermain”中,我只有以下代码:

public partial class usermain : UserControl
    {
        WebClient wc = new WebClient();

        public usermain()
        {
            InitializeComponent();
        }

        private void Usermain_Load(object sender, EventArgs e)
        {
            var json = wc.DownloadString("https://api.truckersmp.com/v2/player/" + Properties.Settings.Default.SteamID);
            var root = JsonConvert.DeserializeObject<RootObject>(json);
            var response = root.response;
            var error = root.error;
            profile_img.ImageLocation = response.avatar;
        }
    }

1 个答案:

答案 0 :(得分:-1)

使用

包围您的Load事件
if(!DesignMode)
{
...
}

if(DesignMode) return;

…