如何修复损坏/消失的控件

时间:2017-03-20 22:37:20

标签: c# winforms custom-controls

我只能猜测问题在于以下代码,但我并非100%肯定。只有当我尝试在RoomDesigner表单中移动面板中的CtrlComputer对象时,才会出现此问题。代码的目的是让CtrlComputer对象在单击时移动为一个。不幸的是,如果没有foreach循环,会单独移动label1,但在拖动时仍会在其周围形成一个灰色框。有关更清晰的图片,请参阅here。我不确定为什么拖动对象会扩大自定义控件本身。

CtrlComputer

public CtrlComputer()
    {
        InitializeComponent();
        label1.MouseMove += new MouseEventHandler(textbox_MouseMove);
        CompButton.MouseMove += new MouseEventHandler(textbox_MouseMove);
        this.MouseMove += new MouseEventHandler(textbox_MouseMove);

    void textbox_MouseMove(object sender, MouseEventArgs e)
    {
        if (activeControl == null || activeControl != sender)
            return;
        foreach (Control control in this.Controls)
        {
            var location = control.Location;
            location.Offset(e.Location.X - previousLocation.X, e.Location.Y - previousLocation.Y);
            control.Location = location;
        }                    
    }

CtrlComputer.Designer

        private void InitializeComponent()
    {
        this.label1 = new System.Windows.Forms.Label();
        this.CompButton = new WindowsFormsApplication1.DoubleClickButton();
        this.SuspendLayout();
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.BackColor = System.Drawing.Color.Lime;
        this.label1.Location = new System.Drawing.Point(7, 20);
        this.label1.Margin = new System.Windows.Forms.Padding(0);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(35, 13);
        this.label1.TabIndex = 39;
        this.label1.Text = "label1";
        this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        // 
        // CompButton
        // 
        this.CompButton.BackColor = System.Drawing.Color.Transparent;
        this.CompButton.BackgroundImage = global::SchoolAdmin.Properties.Resources.memeio;
        this.CompButton.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
        this.CompButton.Location = new System.Drawing.Point(0, 0);
        this.CompButton.Name = "CompButton";
        this.CompButton.Size = new System.Drawing.Size(62, 57);
        this.CompButton.TabIndex = 40;
        this.CompButton.UseVisualStyleBackColor = false;
        // 
        // CtrlComputer
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.AutoSize = true;
        this.BackColor = System.Drawing.Color.Transparent;
        this.Controls.Add(this.label1);
        this.Controls.Add(this.CompButton);
        this.Name = "CtrlComputer";
        this.Size = new System.Drawing.Size(65, 60);
        this.ResumeLayout(false);
        this.PerformLayout();

    }

CtrlComputer - http://pastebin.com/GJ3aXJFb

0 个答案:

没有答案