积极答复者
C#窗体设计中UserControl设计使用问题!!!!!!!!!!

问题
-
我在设计自己的软件界面时要用到画布,于是考虑使用Usercontrol来实现棋盘效果的画布,当完成画布代码设计后,我想把它作为控件放入到我的软件中的ToolStripContainer中,如何实现?
我试了网上的方法,都不行!!其一是:编译生成项目后自定义控件就可以自动加载到工具箱里,可是我的项目工程还有部分代码未完成,无法正确生成解决方案。另一种方法是:在工具箱中-》选择-》找到自定义控件生成的dll(可能是上面项目没生成的缘故,所以根本找不到dll文件,后来查找网上的说dll应该是外部的,可是我的usercontrol与winform在同一个项目中)。
请问还有其他简单方法吗?因为第一次使用usercontrol,请各位务必详细说明!
答案
-
我通常是这么动态加载用户控件的。
public void LoadUserControl(UserControl UCLoader) { UCLoader.Location = new Point(125, 0); this.Controls.Add(UCLoader); } public void UnloadUserControl(UserControl UCLoader) { this.Controls.Remove(UCLoader); UCLoader.Dispose(); } //载入控件 TestUserControl UC = new TestUserControl(); LoadUserControl(UC); //卸掉控件 UnloadUserControl(UC);
- 已标记为答案 EricQing 2011年7月1日 5:41
-
你好
我想你是把用户控件和自定义控件弄混淆了。
用户控件是添加一些vs存在的控件,然后可以像Rocky说的那样,直接new一个对象,然后add到你的ToolStripContainer里面。
而自定义控件一般是继承一些vs控件,然后重写一些新的功能,或者完全自己写一个控件,这样的控件需要像你做的那样,引如工具箱。
所以你不需要生成dll,直接在当前项目里,new一个那个控件类,然后add到ToolStripContainer里面。
希望对你有帮助~~
Cookie Luo[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 EricQing 2011年7月1日 5:40
-
从你的描述看,你自己已经在实现那个自定义控件了,但是不知道怎么用。是吧。
1.你首先在解决方案里建立一个WindowsFormsControlLibrary的项目。
2.然后删除里面的UserControl。
3.然后自己添加一个自定义控件(CustomControl),把代码复制进去,或者直接把你已经写好的自定义控件复制进去。
4.然后编译这个项目。
5。 在主项目中,工具箱中-》选择-》找到自定义控件生成的dll。添加成功后你就会在工具箱里找到这个控件了。
如果你不想添加新的项目在你的solution里面,你可以把生成的Dll放在程序里就好,之前用来生成dll的项目可以删掉了。
希望对你有帮助~~
Cookie Luo[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 EricQing 2011年7月1日 5:40
-
如果你的dll名字是imageSeq的话,不需要using了。直接ImageSeg.Graphic。你先试试把dll重新build一下,然后重新添加到项目里面。
如果还不行能不能看看Graphic定义的一些代码,还有运行是报错前后的代码。
Cookie Luo[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 EricQing 2011年7月1日 5:41
-
嗯,using是针对命名空间的,你引用了ImageSeg之后,就可以用里面的类了,不需要再引用里面的类。如果命名空间下还有别的空间的话,还可以继续引用。代码可以运行了吗?
Cookie Luo[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 EricQing 2011年7月1日 5:41
全部回复
-
是啊!下面是我采用网络上的方法时的情况整理:
仔细做了一下,自定义控件可以引入工具箱。但会出现下列报错:
与“ImageSeg.Dialog.RegionDialog.RegionDialog(ImageSeg.Widget.Canvas)”最匹配的重载方法具有一些无效参数,参数“1”: 无法从“ControlLib.Canvas”转换为“ImageSeg.Widget.Canvas”
下面是我定位到得报错的语句: RegionDialog dlg = new RegionDialog(this.canvasMain);
//RegionDialog是我建的form窗体,他有一个构造函数 public RegionDialog(Canvas parent)
{
InitializeComponent();
this.canvas = parent;
this.srcImage = this.canvas.Image;
}
Canvas是我写的画布类,我为了生成画布这个自定义控件,就把原项目中Canvas类用到的其他类单独提取出来,新建了一个新命名控件的窗体控件项目,然后顺利生成了dll,再倒入我原项目的工具箱里应用到窗体上,就出现上面的问题了。请问各位,我这样做错误在哪里?谢谢!! -
我通常是这么动态加载用户控件的。
public void LoadUserControl(UserControl UCLoader) { UCLoader.Location = new Point(125, 0); this.Controls.Add(UCLoader); } public void UnloadUserControl(UserControl UCLoader) { this.Controls.Remove(UCLoader); UCLoader.Dispose(); } //载入控件 TestUserControl UC = new TestUserControl(); LoadUserControl(UC); //卸掉控件 UnloadUserControl(UC);
- 已标记为答案 EricQing 2011年7月1日 5:41
-
你好
我想你是把用户控件和自定义控件弄混淆了。
用户控件是添加一些vs存在的控件,然后可以像Rocky说的那样,直接new一个对象,然后add到你的ToolStripContainer里面。
而自定义控件一般是继承一些vs控件,然后重写一些新的功能,或者完全自己写一个控件,这样的控件需要像你做的那样,引如工具箱。
所以你不需要生成dll,直接在当前项目里,new一个那个控件类,然后add到ToolStripContainer里面。
希望对你有帮助~~
Cookie Luo[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 EricQing 2011年7月1日 5:40
-
你好
我想你是把用户控件和自定义控件弄混淆了。
用户控件是添加一些vs存在的控件,然后可以像Rocky说的那样,直接new一个对象,然后add到你的ToolStripContainer里面。
而自定义控件一般是继承一些vs控件,然后重写一些新的功能,或者完全自己写一个控件,这样的控件需要像你做的那样,引如工具箱。
所以你不需要生成dll,直接在当前项目里,new一个那个控件类,然后add到ToolStripContainer里面。
希望对你有帮助~~
Cookie Luo[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
非常感谢您的提醒,是我的表达出了问题。我需要实现的是自定义控件,能否举个例子说明一下,谢谢! -
从你的描述看,你自己已经在实现那个自定义控件了,但是不知道怎么用。是吧。
1.你首先在解决方案里建立一个WindowsFormsControlLibrary的项目。
2.然后删除里面的UserControl。
3.然后自己添加一个自定义控件(CustomControl),把代码复制进去,或者直接把你已经写好的自定义控件复制进去。
4.然后编译这个项目。
5。 在主项目中,工具箱中-》选择-》找到自定义控件生成的dll。添加成功后你就会在工具箱里找到这个控件了。
如果你不想添加新的项目在你的solution里面,你可以把生成的Dll放在程序里就好,之前用来生成dll的项目可以删掉了。
希望对你有帮助~~
Cookie Luo[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 EricQing 2011年7月1日 5:40
-
从你的描述看,你自己已经在实现那个自定义控件了,但是不知道怎么用。是吧。
1.你首先在解决方案里建立一个WindowsFormsControlLibrary的项目。
2.然后删除里面的UserControl。
3.然后自己添加一个自定义控件(CustomControl),把代码复制进去,或者直接把你已经写好的自定义控件复制进去。
4.然后编译这个项目。
5。 在主项目中,工具箱中-》选择-》找到自定义控件生成的dll。添加成功后你就会在工具箱里找到这个控件了。
如果你不想添加新的项目在你的solution里面,你可以把生成的Dll放在程序里就好,之前用来生成dll的项目可以删掉了。
希望对你有帮助~~
Cookie Luo[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
我找了一些源代码,发现里面的空间设计,用的是UserControl来设计我需要的画布这个类的,按照以上网友提供的方法,我做了一下实验:当你的用户控件是独立的,也就是没有用到项目中的其他类时,是可以的,但是我的情况是:我在这个项目中要用到这个控件才开始在这个工程里设计它,然后再将设计好的控件放到工程里应用,而问题就出在设计好控件的代码与界面后,生成不了(dll)工具,无法应用到项目里。我试过将控件用到的类从工程中抽取,单独生成控件dll,但当我引入到该工程时,又会出现,该控件实例化对象值为null(具体情况是:实例化引入的该控件时,项目中的其他用到该控件的代码会报出警告:引入的控件中的类与项目代码中的类重复,在debug时,发现该控件的实例化对象值为null,程序运行中断),麻烦大牛帮忙解答一下,谢谢了! -
你好
如果你创建的自定义控件的话,生成的dll,不是把这个dll引用到项目,而是要添加到工具箱~~
工具箱中-》选择-》找到自定义控件生成的dll。添加成功后你就会在工具箱里找到这个控件了。你再看看我上一个回复。
Cookie Luo[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
从错误看,你的程序集里面没有ImageSeg.Graphic这个类型,但是你代码里面用到了。最好提供些代码看看。报错那里前后。
Cookie Luo[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
一般不会的,是不是忘了Using了?Graphic是自己写的?还是系统的那个System.Drawing的?设个断点看看。如果有代码就比较好分析了。
Cookie Luo[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
如果你的dll名字是imageSeq的话,不需要using了。直接ImageSeg.Graphic。你先试试把dll重新build一下,然后重新添加到项目里面。
如果还不行能不能看看Graphic定义的一些代码,还有运行是报错前后的代码。
Cookie Luo[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 EricQing 2011年7月1日 5:41
-
嗯,using是针对命名空间的,你引用了ImageSeg之后,就可以用里面的类了,不需要再引用里面的类。如果命名空间下还有别的空间的话,还可以继续引用。代码可以运行了吗?
Cookie Luo[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 EricQing 2011年7月1日 5:41
-
你可以试着先建一个新的项目,引用那个dll,用那个类,如果还是出现这个问题的话。你就把这个项目发给我调试下。cookie.1987@hotmail.com
Cookie Luo[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
我已经回复你的yahoo邮箱了。请查看。
Cookie Luo[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.