如何在C#中创建自定义按钮控件? [英] How can i create custom button control in C#?

查看:101
本文介绍了如何在C#中创建自定义按钮控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建用户定义的按钮控件以在表单中使用.

解决方案

您需要创建一个继承自Button的类,以将所有样式保留在一个地方.要进行悬停和按下状态,可以覆盖鼠标的输入/离开按钮事件并更改样式.

我认为最简单的方法是设置按钮的某些属性,例如以下和

 .button1.FlatStyle= System.Windows.Forms.FlatStyle.Flat;
 .button1.Image = " 
 .button1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
 .button1.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBefor 




CodeProject文章具有颜色和形状的自定义按钮 [ 公共 部分 class UserControl1:UserControl { 公共 UserControl1() { InitializeComponent(); } }


更改为UserControl以按类似的按钮

 公共 部分  class  UserControl1:按钮
   {
       公共 UserControl1()
       {
           InitializeComponent();
       }
   } 


您还可以在表单应用程序中使用wpf用户控件,如果您想让用户控件达到范围,我建议您使用WPF控件,如果您对XAML语言有所了解的话,可以在WPF中使用应用程序.


I need to create user defined button control to use in my form.

You need to create a class that inherits from Button to keep all your styling in one place. To do the hover and pressed states you can override the mouse enter / leave events of the button and change style.

I think the simplest way is set some properties of the button like below and

this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Image = "Any Image"
this.button1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.button1.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBefor




CodeProject article
Custom Button with Color and Shape[^] is a good reference.


you need to create user control for for button
it is depend on how you want to Chang your button like style of you button of add costume property of button etc

fist of all
you need to add new user control in you form application project
then go to code behind file then

here

public partial class UserControl1 : UserControl
   {
       public UserControl1()
       {
           InitializeComponent();
       }
   }


to Chang to UserControl to button something like that

public partial class UserControl1 : Button
   {
       public UserControl1()
       {
           InitializeComponent();
       }
   }


you can also user wpf user control in to your form application i suggest to you if you want to make you user control reach then use wpf control if possibl than make you application in WPF if you little bit know about XAML language


这篇关于如何在C#中创建自定义按钮控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆