C#表单剪切控件并粘贴到面板,控件代码无效。

huangapple go评论62阅读模式
英文:

C# form Cut control and paste in panel,control code not work

问题

Step 1: 创建一个C# Windows Forms应用项目

Step 2: 向窗体添加一个按钮、标签控件和一个面板,将按钮和标签放在面板外部。

Step 3: 双击按钮控件,添加以下代码:

private void button1_Click(object sender, EventArgs e)
{
    if (label1.Visible == true)
    { 
        label1.Visible = false;  
    }
    else
    { 
        label1.Visible = true;   
    }
}

Step 4: 运行应用程序,单击button1 -> label1消失。

Step 5: 停止应用程序,返回到窗体设计。将按钮控件剪切并粘贴到面板中(不要拖放)。

Step 6: 再次运行应用程序,单击按钮,代码不起作用。

为什么按钮控件的代码不起作用?如何修复而无需重新编码?

英文:

Step 1: create a C# Windows Forms app project

Step 2: add a button, label control and one panel to the form, button and label outside the panel:

C#表单剪切控件并粘贴到面板,控件代码无效。

Step 3: double-click Button control and add this code:

private void button1_Click(object sender, EventArgs e)
{
    if (label1.Visible == true)
    { 
        label1.Visible = false;  
    }
    else
    { 
        label1.Visible = true;   
    }
}

Step 4: run the app, click button1 -> label1 disappears.

Step 5: stop app, back to form design. Cut button control and paste in panel (Not draw drop):

C#表单剪切控件并粘贴到面板,控件代码无效。

Step 6: run app again, click button, code does not work.

Why does the button control code not work? How to fix it without coding again?

答案1

得分: 2

在控件和事件处理程序方法之间创建了链接。当你剪切控件时,实际上是删除它,因此该链接被移除。当你粘贴控件时,链接不会自动重新创建,因为没有理由这样做。如果你想要恢复链接,需要自己重新创建。为此,请选择控件,打开属性窗口,点击事件按钮,然后从下拉列表中选择适当事件的现有方法。

更好的解决方案是一开始就不要剪切和粘贴,而是使用拖放。这样做时,控件永远不会被删除,因此事件处理程序的注册也永远不会被删除。请注意,你还可以使用文档大纲窗口将子控件移入和移出容器。不过,你可能仍需要调整它们的位置,可以通过拖放或在属性窗口中进行。

顺便说一下,如果你想在剪切和粘贴操作之前后看到差异,只需打开你的窗体的设计器代码文件。作为测试,向窗体添加两个Button控件,并双击每个以创建Click事件处理程序。剪切其中一个控件,然后再粘贴,然后查看每个的设计器代码。你将能够看到一个缺少事件处理程序注册的控件和另一个的区别。

英文:

There is a link created between the control and the event handler method. When you cut the control, you effectively delete it, so that link is removed. When you paste the control, the link is not recreated automatically because there's no reason to. If you want the link restored, you need to recreate it yourself. To do that, select the control, open the Properties window, click the Events button and then select the existing method from the drop-down list for the appropriate event.

A better solution is to not cut and paste in the first place. Instead, drag and drop. When you do that, the control is never deleted so the event handler registration is never deleted either. Note that you can also use the Document Outline window to move child controls into and out of containers. You may still have to adjust their position though, either by drag and drop or in the Properties window.

BTW, if you want to see the difference before and after a cut and paste operation, just open the designer code file for your form. As a test, add two Button controls to your form and double-click each to create Click event handlers. Cut one of the controls and then paste it again, then look at the designer code for each. You'll be able to see the event handler registration for one that is missing for the other.

答案2

得分: -1

也许你有一个额外的按钮,这样你的功能就无法正常工作,请分享代码。

英文:

Maybe you have one more button so that you function doesn't work please share code

huangapple
  • 本文由 发表于 2023年5月10日 13:20:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215128.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定