使用自定义类 .NET Winforms 替换现有控件
我有一个现有的 .NET 应用程序,其中已经创建了大量控件。
现在我有一个新的自定义控件类,例如按钮(继承自“Button”),具有额外的属性、方法等。 编译得很好,它显示在我的工具栏中。
我如何告诉 VS 我想用新控件替换现有控件?
我的意思是,如果我打开设计器代码并手动更改控件的类型,VS 会非常不高兴,并且设计会被破坏并出现令人讨厌的错误消息。
有没有办法在不破坏 VS 中设计的情况下更改设计器生成的代码?
或者除了手动删除每个控件并创建一个新控件之外,还有其他方法吗?这将是一个真正的麻烦,因为当我删除控件时,所有控件的处理程序都将在表单代码中被删除。例如:
处理 myControl.Click ...处理 myControl.MouseHover ...等。
欢迎任何提示!
I have an existing .NET application with tons of controls already created.
Now I have a new custom control class for - say - buttons (which inherits from "Button"), with extra properties, methods etc.
Compiled fine, and it shows in my toolbar.
How can I tell VS that I want to REPLACE existing controls with the new one?
I mean, if I open the designer code and manually change the control's type, VS is really unhappy and the design is broken and down with a nasty error message.
Is there a way to change the designer generated code without breaking the design in VS?
Or Is there a way to do it other than manually delete each control, and create a new one? This would be a real hassle since all control's handlers will be erased in the form's code when I delete the control. Like:
handles myControl.Click ... handles myControl.MouseHover ... etc.
Any tip welcome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
完整的程序如下。
进行备份!
转到解决方案资源管理器窗口(Ctrl Alt L 是快捷方式),然后单击“显示所有文件”按钮。
选择您想要进行控件替换的表单。
单击小三角形以显示“designer.vb”文件。
打开它。
搜索您要替换的控件实例。
检查是否出现以下情况:
替换您的控制定义。
对每次出现的情况执行相同操作:
构建您的解决方案
Complete procedure follows.
Make a backup!
Go to solution explorer window (Ctrl Alt L is the shortcut) and click "Show all files" button.
Select the form you want to do control replacement.
Clic on the small triangle to show the "designer.vb" file.
Open it.
Search for the control instance you want to replace.
Check for the occurrences of:
Substitute your definition of control.
Do the same for every occurrence of:
Build your solution
您可以在 VS 中使用“当前项目”范围进行搜索和替换。确保您已选中“搜索隐藏文本”框(这将包括设计者生成的代码)。然后尝试编译该项目,如果有任何错误(例如缺少属性),可以用相同的方式纠正这些错误。
You can do a search and replace in VS with "Current Project" scope. Make sure you've checked the "Search hidden text" box (this will include designer-generated code). Then try to compile the project, if there are any errors (e.g. missing properties), those errors can be corrected the same way.