C# 如何在 ToolBox 上放置扩展的 WinForms 控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1116311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:35:09  来源:igfitidea点击:

How to put an extended WinForms Control on ToolBox

c#winforms

提问by david.healed

I plan to add functionalities to TextBox with the following:

我计划为 TextBox 添加以下功能:

   public class TextBoxExt : TextBox  
    {
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);
        }

    }

The question is how can we use this TextBoxExt? Is there anyway to get this class onto the ToolBox so that we can just drag and drop it onto the form? If not, what is the best way to use the TextBoxExt?

问题是我们如何使用这个TextBoxExt?有没有办法把这个类放到工具箱上,这样我们就可以把它拖放到表单上?如果没有,使用 TextBoxExt 的最佳方法是什么?

采纳答案by Calanus

  1. Build you project with TextBoxExt, make sure it compiles ok.
  2. With the form that you want TextBoxExt on, open the toolbox, right click and select "choose items"
  3. Browse to you .exe or dll that you compiled in 1)
  4. make sure that TextBoxExt has a tick next to it, press ok
  5. TextBoxExt should appear in the toolbox, drag it onto your form
  1. 使用 TextBoxExt 构建您的项目,确保它编译正常。
  2. 使用您想要 TextBoxExt 的表单,打开工具箱,右键单击并选择“选择项目”
  3. 浏览到您在 1) 中编译的 .exe 或 dll
  4. 确保 TextBoxExt 旁边有一个勾号,按确定
  5. TextBoxExt 应出现在工具箱中,将其拖到您的表单上

(There is another way of doing this, opening the designer file and renaming the instances of TextBox to TextBoxExt but manual editing of designer files can be considered hazardous by some)

(还有另一种方法,打开设计器文件并将 TextBox 的实例重命名为 TextBoxExt 但手动编辑设计器文件可能被某些人认为是危险的)

回答by Rob Windsor

Any custom control in your project should show up in the Toolbox automatically. I have found that sometimes the controls won't show until you close a re-open Visual Studio. I assume the issue has something to do with caching of the contents of the Toolbox.

项目中的任何自定义控件都应自动显示在工具箱中。我发现有时在关闭重新打开的 Visual Studio 之前控件不会显示。我认为这个问题与工具箱内容的缓存有关。

回答by James

You need to add a constructor to your derived class.

您需要向派生类添加构造函数。

public class TextBoxExt : TextBox      
{        
    public TextBoxExt()
    {
    }

    protected override void OnKeyPress(KeyPressEventArgs e)        
    {            
         base.OnKeyPress(e);        
    }    
}

回答by Eric

Your control should appear in the toolbox for your solution automatically. To have it appear for other projects, you have to do Choose Toolbox items, as others have said.

您的控件应自动出现在您的解决方案的工具箱中。要让它出现在其他项目中,您必须像其他人所说的那样选择工具箱项目。

If you want to provide special design-time functionality, then you will also need to provide some additional designer related attributes and probably your own class derived from ControlDesigner.

如果您想提供特殊的设计时功能,那么您还需要提供一些额外的与设计器相关的属性以及您自己的从 ControlDesigner 派生的类。

回答by Ferdinando Santacroce

I fell into this trap just a couple of hours ago.
I've got a .NET 2.0 Windows Application project with some custom UserControls; it worked fine. So I decided to order my files in subfolders, to make my project a little bit cleaner.
After that, Visual Studio 2010 designer stopped loading my forms, and ToolBox won't show my controls anymore.
I freaked out, moving back source files in project root, resetting ToolBox, but nothing seemed to work. After that, I remembered I used ReSharper "Remove Unused References", so I tried to put back unused reference, in particular System.Data: problem solved! :O
I can't say you why, but this worked for me.
Hope my experience can help someone else. :)
Bye, Nando

就在几个小时前,我掉进了这个陷阱。
我有一个带有一些自定义用户控件的 .NET 2.0 Windows 应用程序项目;它工作正常。所以我决定在子文件夹中排序我的文件,让我的项目更简洁一些。
之后,Visual Studio 2010 设计器停止加载我的表单,并且 ToolBox 将不再显示我的控件。
我吓坏了,将项目根目录中的源文件移回,重置工具箱,但似乎没有任何效果。之后,我想起我使用了 ReSharper“删除未使用的引用”,所以我尝试放回未使用的引用,特别是System.Data:问题解决了!:O
我不能说你为什么,但这对我有用。
希望我的经验可以帮助别人。:)
再见,南多

回答by Matthew Radford

I created an empty constructor for my custom implementation of UltraGridBagLayoutPanel. Although david.healed is right it isn't necessary, it is quite useful to put a breakpoint in to check that when the form initialises it is using your class to implement your custom control.

我为 UltraGridBagLayoutPanel 的自定义实现创建了一个空的构造函数。尽管 david.healed 是对的,但它不是必需的,但放置断点以检查表单初始化时它是否正在使用您的类来实现您的自定义控件非常有用。

It would have been a lot easier to edit the designer file, but I tried it and changed both the field type for the control and also changed the assignment of the field to a new instance of my custom control.

编辑设计器文件会容易得多,但我尝试了它并更改了控件的字段类型,并将字段的分配更改为我的自定义控件的新实例。

private Infragistics.Win.Misc.UltraGridBagLayoutPanel ultraGridBagLayoutPanel1;
this.ultraGridBagLayoutPanel1 = new Infragistics.Win.Misc.UltraGridBagLayoutPanel();

to

private Athia.Reports.ultraGridBagLayoutPanel1 ultraGridBagLayoutPanel1;
this.ultraGridBagLayoutPanel1 = new Athia.Reports.ultraGridBagLayoutPanel1();

Doing this destroys Visual Studio every time, and to fix it requires using a text editor to put it back again. Therefore unless anyone can describe what is wrong with my implementation of this approach, perhaps calling the class the same as the control name isn't a great idea, I think the only safe and reliable way to achieve this is as Calanus describes in steps 1 to 5 or as an small deviation from that as Rob Windsor rightly points out restarting VS will bring the control into the Toolbox automatically. Unfortunately for me I then have to change all of the child controls over from the original class to my customised class :-(.

每次这样做都会破坏 Visual Studio,要修复它需要使用文本编辑器将其重新放回原处。因此,除非有人能描述我的这种方法的实现有什么问题,否则调用与控件名称相同的类并不是一个好主意,我认为实现这一点的唯一安全可靠的方法是 Calanus 在步骤 1 中描述的到 5 或与 Rob Windsor 正确指出的小偏差,重新启动 VS 将自动将控件带入工具箱。不幸的是,我必须将所有子控件从原始类更改为我的自定义类:-(。

回答by Adiono

I know this is super old question, but maybe still useful for someone else that has same problem like me - as it's still on the top Google :)

我知道这是一个非常老的问题,但对于像我一样有同样问题的其他人来说可能仍然有用 - 因为它仍然在谷歌的顶部:)

You might interest to use ToolboxItemAttribute (http://msdn.microsoft.com/en-us/library/system.componentmodel.toolboxitemattribute(v=vs.110).aspx).

您可能有兴趣使用 ToolboxItemAttribute ( http://msdn.microsoft.com/en-us/library/system.componentmodel.toolboxitemattribute(v=vs.110).aspx)。

I did this at my end to resolve the problem.

我最后这样做是为了解决这个问题。

[ToolboxItem(true)]
public class PanelTitle : LabelControl  {
   // Whatever code to override LabelControl here...
}

Rebuild the solution and the extended control should be shown in the toolbox.

重建解决方案,扩展控件应显示在工具箱中。

回答by anon

Within the same Solution this should work automatically. However, I have found that if the Target Framework aren't matching the Toolbox does not populate. ( I'm assuming really Reference needs to be of version same or lower than target of Reference. ) ( I did get a warning about non-matching Frameworks ) By making these the same Target Framework, Recompile, Restart VS. the control populated correctly. ( I also added the ToolboxItem(true) Attribute)

在同一个解决方案中,这应该自动工作。但是,我发现如果目标框架不匹配,则不会填充工具箱。(我假设确实 Reference 的版本需要与 Reference 的目标相同或更低。)(我确实收到了关于不匹配框架的警告)通过使这些成为相同的目标框架,重新编译,重新启动 VS。控件填充正确。(我还添加了 ToolboxItem(true) 属性)