简体   繁体   English

C#Winform:Winform和用户控件之间的交流

[英]C# winform: communation between winform and usercontrol

I want to develop a custom button which contain a properity that can be changed dynamics according to the content. 我想开发一个自定义按钮,其中包含可以根据内容动态更改的属性。 This button would be used more that once in the same form. 以相同的形式使用此按钮的次数不止一次。

for example: the custom button contain a string: str. 例如:自定义按钮包含一个字符串:str。 if we want the button update the database, we just need to set the string in the mainform: str= "update mytable * *". 如果我们想要按钮更新数据库,我们只需要在主表单中设置字符串:str =“ update mytable * *”。 if we want the delete function, we just need to set the string into: str="delete from my table....." in the main form. 如果我们需要删除功能,则只需将字符串设置为:主窗体中的str =“ delete from my table .....”。

Can you give me an example? 你能举个例子吗? I am a newer in C#. 我是C#的新手。

Its quite easy to achieve this you just need to override button class of the .net framwork and need to write you own code 做到这一点非常容易,您只需要重写.net框架的按钮类,并编写自己的代码即可

you can check this blog post wheer i did the same thing for the textbox : Enhanced Textbox Control 如果我对文本框做了相同的操作,则可以查看此博客文章: 增强的文本框控件

You can create the public properties in the usercontrol and also public methods, that will be accessible from your form. 您可以在用户控件以及公共方法中创建公共属性,这些属性可以从表单中访问。 Then you can allocate the value to property and call the appropriate method to execute the database tranascation statement. 然后,您可以将值分配给property并调用适当的方法来执行数据库转换语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM