不能添加用户控件“无法加载工具箱项” C#winforms

时间:2018-09-06 08:32:47

标签: c# winforms controls

在处理应用程序时,我决定为该项目添加我的第一个用户控件。我很好,但是当我从工具箱中将其拖到主窗体时,它弹出并显示错误消息:

enter image description here

无论我做什么,似乎都无法解决。我尝试通过代码添加它,但是根本不会显示出来。

在网上查找问题时,我找不到可行的解决方案,或者至少没有可以遵循和理解的解决方案。

我们将非常感谢您的帮助,如果需要更多信息,我很乐意添加。但是目前我不知道可以添加些什么。

该代码用于一种简单的恶作剧病毒(必须激发自己继续学习代码:))这是代码(请不要启动该文件,毕竟这是一种恶作剧病毒,唯一退出方式是alt + f4 ):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Simple_virus_V2
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        internal static extern IntPtr SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        public Form1()
        {
            InitializeComponent();
            timer1.Start();
            timer2.Start();

            
            Thread newthread = new Thread(progress);
            newthread.Start();
        }
        Random rnd = new Random();
        int noticewidth = 0;
        bool changecursor = false;
        
        private void progress() {

            Thread.Sleep(1000);
            changecursor = true;
            Thread.Sleep(1000);
            timer3.Start();
            Thread.Sleep(5000);
            noticewidth = Width;

        }
        int mouseflash = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (changecursor) {

                if (mouseflash < 1000)
                {
                    Bitmap cursor = new Bitmap(new Bitmap(pictureBox1.Image), 24, 24);
                    Cursor = new Cursor(cursor.GetHicon());
                
                
                } else if (mouseflash < 1700) {

                    Bitmap cursor = new Bitmap(new Bitmap(pictureBox2.Image), 30, 30);
                    Cursor = new Cursor(cursor.GetHicon());

                }
                else {

                    mouseflash = 0;
                }


                mouseflash = mouseflash + rnd.Next(3,10);
            }
            header.Left = MousePosition.X - (header.Width / 2);
            label2.Left = MousePosition.X - (label2.Width / 2);
            label3.Left = label2.Left + 25;

            panel1.Width = noticewidth;
            this.Location = new Point(0,0);
            panel1.Location = new Point(0, MousePosition.Y - 40);
            this.WindowState = FormWindowState.Maximized;
            TopMost = true;
            
            Process currentProcess = Process.GetCurrentProcess();
            IntPtr hWnd = currentProcess.MainWindowHandle;
            if (hWnd != IntPtr.Zero)
            {
                SetForegroundWindow(hWnd);
                ShowWindow(hWnd, int.Parse("9"));
            }
            
            Focus();
            this.Width = Screen.PrimaryScreen.Bounds.Width * 3;
            this.Height = Screen.PrimaryScreen.Bounds.Height * 2; 
            
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {

            
            
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            /*
            using (Form1 frm = new Form1()) {
                if (frm.ShowDialog() == DialogResult.OK) {

                }
            }
            */
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            
            
            PictureBox pic = new PictureBox();
            pic.Width = 1;
            pic.Height = 1;
            pic.BackColor = Color.Black;
            pic.Location = new Point(rnd.Next(0, this.Width), rnd.Next(0, this.Height));
            this.Controls.Add(pic);
            

        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            /*
            bartry bars = new bartry();
            bars.Location = new Point(0, rnd.Next(0, 500));
            Controls.Add(bars);


            timer3.Interval = rnd.Next(100, 5000);
            */
        }
    }
}

谢谢

2 个答案:

答案 0 :(得分:0)

在尝试了另一篇文章的答案很久以后,他们都没有工作。在打开一个新项目并尝试使用我的代码后,我确实找到了解决方案。

解决方案:确保用户控件和主窗体具有相同的“使用”引用!

抱歉,这是不言而喻的,但我不知道您必须这样做。

答案 1 :(得分:-2)

只需调试项目,然后尝试添加用户控件表单