Class Button C# | VN-Zoom | Cộng đồng Chia Sẻ Kiến Thức Công Nghệ và Phần Mềm Máy Tính

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We need money to operate the site, and almost all of it comes from our online advertising.

If possible, please support us by clicking on the advertisements.

Please add vn-z.vn to your ad blocking whitelist or disable your adblocking software.

×

Class Button C#

chào các bạn dưới đây là class tạo button cố định, giờ mình muốn truyền giá trị theo ý mình như borderRadius=số mình gõ vào, size kích thước button mình cũng gõ số vào. mỗi lần thay đổi giá trị nhập vào button cũng thay đổi theo giá trị đó mà mình không biết khai báo để truyền giá trị vào chỗ nào các bạn hướng dẫn mình với:
public class VBButton : Button
{
//Fields
private int borderSize = 0;
private int borderRadius = 18;
private Color borderColor = Color.BurlyWood;

//Properties
[Category("Custom Props")]
public int BorderSize
{
get { return borderSize; }
set
{
borderSize = value;
this.Invalidate();
}
}

[Category("Custom Props")]
public int BorderRadius
{
get { return borderRadius; }
set
{
borderRadius = value;
this.Invalidate();
}
}

[Category("Custom Props")]
public Color BorderColor
{
get { return borderColor; }
set
{
borderColor = value;
this.Invalidate();
}
}
[Category("Custom Props")]
public Color BackgroundColor
{
get { return this.BackColor; }
set { this.BackColor = value; }
}

[Category("Custom Props")]
public Color TextColor
{
get { return this.ForeColor; }
set { this.ForeColor = value; }
}

//Constructor
public VBButton9()
{
this.FlatStyle = FlatStyle.Flat;

this.FlatAppearance.BorderSize = 0;
this.Size = new Size(100, 36);
//this.BackColor = Color.Coral;cach lay mau 1
this.BackColor = Color.FromArgb(33, 37, 41); ;//cach ly mau 2
this.ForeColor = Color.White;
this.Resize += new EventHandler(Button_Resize);
}

//Methods
private GraphicsPath GetFigurePath(Rectangle rect, float radius)
{
GraphicsPath path = new GraphicsPath();
float curveSize = radius * 2F;

path.StartFigure();
path.AddArc(rect.X, rect.Y, curveSize, curveSize, 180, 90);
path.AddArc(rect.Right - curveSize, rect.Y, curveSize, curveSize, 270, 90);
path.AddArc(rect.Right - curveSize, rect.Bottom - curveSize, curveSize, curveSize, 0, 90);
path.AddArc(rect.X, rect.Bottom - curveSize, curveSize, curveSize, 90, 90);
path.CloseFigure();
return path;
}

protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

Rectangle rectSurface = new Rectangle(0, 0, this.Width, this.Height);
Rectangle rectBorder = new Rectangle(1, 1, (int)(this.Width - 0.8f), this.Height - 1);
int smoothSize = 2;
if (borderSize > 0)
smoothSize = borderSize;

if (borderRadius > 2) //Rounded button
{
using (GraphicsPath pathSurface = GetFigurePath(rectSurface, borderRadius))
using (GraphicsPath pathBorder = GetFigurePath(rectBorder, borderRadius - borderSize))
using (Pen penSurface = new Pen(this.Parent.BackColor, smoothSize))
using (Pen penBorder = new Pen(borderColor, borderSize))
{
//Button surface
this.Region = new Region(pathSurface);
//Draw surface border for HD result
pevent.Graphics.DrawPath(penSurface, pathSurface);

//Button border
if (borderSize >= 1)
//Draw control border
pevent.Graphics.DrawPath(penBorder, pathBorder);
}
}
else //Normal button
{
//Button surface
this.Region = new Region(rectSurface);
//Button border
if (borderSize >= 1)
{
using (Pen penBorder = new Pen(borderColor, borderSize))
{
penBorder.Alignment = PenAlignment.Inset;
pevent.Graphics.DrawRectangle(penBorder, 0, 0, this.Width - 1, this.Height - 1);
}
}
}


}
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
this.Parent.BackColorChanged += new EventHandler(Container_BackColorChanged);
}

private void Container_BackColorChanged(object sender, EventArgs e)
{
this.Invalidate();
}

private void Button_Resize(object sender, EventArgs e)
{
if (borderRadius > this.Height)
borderRadius = this.Height;
}
}
 

dammage

Rìu Chiến
bạn nên bỏ code vô trong thẻ code như ở dưới cho dễ nhìn
C#:
public class VBButton : Button
{
//Fields
private int borderSize = 0;
private int borderRadius = 18;
private Color borderColor = Color.BurlyWood;

//Properties
[Category("Custom Props")]
public int BorderSize
{
    get { return borderSize; }
    set
    {
        borderSize = value;
        this.Invalidate();
    }
}

[Category("Custom Props")]
public int BorderRadius
{
    get { return borderRadius; }
    set
    {
        borderRadius = value;
        this.Invalidate();
    }
}

[Category("Custom Props")]
public Color BorderColor
{
    get { return borderColor; }
    set
    {
        borderColor = value;
        this.Invalidate();
    }
}

[Category("Custom Props")]
public Color BackgroundColor
{
    get { return this.BackColor; }
    set { this.BackColor = value; }
}

[Category("Custom Props")]
public Color TextColor
{
    get { return this.ForeColor; }
    set { this.ForeColor = value; }
}

//Constructor
public VBButton9()
{
    this.FlatStyle = FlatStyle.Flat;

    this.FlatAppearance.BorderSize = 0;
    this.Size = new Size(100, 36);
    //this.BackColor = Color.Coral;cach lay mau 1
    this.BackColor = Color.FromArgb(33, 37, 41); ;//cach ly mau 2
    this.ForeColor = Color.White;
    this.Resize += new EventHandler(Button_Resize);
}

//Methods
private GraphicsPath GetFigurePath(Rectangle rect, float radius)
{
    GraphicsPath path = new GraphicsPath();
    float curveSize = radius * 2F;

    path.StartFigure();
    path.AddArc(rect.X, rect.Y, curveSize, curveSize, 180, 90);
    path.AddArc(rect.Right - curveSize, rect.Y, curveSize, curveSize, 270, 90);
    path.AddArc(rect.Right - curveSize, rect.Bottom - curveSize, curveSize, curveSize, 0, 90);
    path.AddArc(rect.X, rect.Bottom - curveSize, curveSize, curveSize, 90, 90);
    path.CloseFigure();
    return path;
}

protected override void OnPaint(PaintEventArgs pevent)
{
    base.OnPaint(pevent);
    pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

    Rectangle rectSurface = new Rectangle(0, 0, this.Width, this.Height);
    Rectangle rectBorder = new Rectangle(1, 1, (int)(this.Width - 0.8f), this.Height - 1);
    int smoothSize = 2;
    if (borderSize > 0)
        smoothSize = borderSize;

    if (borderRadius > 2) //Rounded button
    {
        using (GraphicsPath pathSurface = GetFigurePath(rectSurface, borderRadius))
        using (GraphicsPath pathBorder = GetFigurePath(rectBorder, borderRadius - borderSize))
        using (Pen penSurface = new Pen(this.Parent.BackColor, smoothSize))
        using (Pen penBorder = new Pen(borderColor, borderSize))
        {
            //Button surface
            this.Region = new Region(pathSurface);
            //Draw surface border for HD result
            pevent.Graphics.DrawPath(penSurface, pathSurface);

            //Button border
            if (borderSize >= 1)
                //Draw control border
                pevent.Graphics.DrawPath(penBorder, pathBorder);
        }
    }
    else //Normal button
    {
        //Button surface
        this.Region = new Region(rectSurface);
        //Button border
        if (borderSize >= 1)
        {
            using (Pen penBorder = new Pen(borderColor, borderSize))
            {
                penBorder.Alignment = PenAlignment.Inset;
                pevent.Graphics.DrawRectangle(penBorder, 0, 0, this.Width - 1, this.Height - 1);
            }
        }
    }
}

protected override void OnHandleCreated(EventArgs e)
{
    base.OnHandleCreated(e);
    this.Parent.BackColorChanged += new EventHandler(Container_BackColorChanged);
}

private void Container_BackColorChanged(object sender, EventArgs e)
{
    this.Invalidate();
}

private void Button_Resize(object sender, EventArgs e)
{
    if (borderRadius > this.Height)
    borderRadius = this.Height;
}
}

trên window form thì nó có cái textbox, bạn tạo 1 cái textbox để cho người dùng nhập dữ liệu, xong lấy thuộc tính text của nó, convert thành số rồi gán cho button thôi
https://www.geeksforgeeks.org/c-sharp-textbox-controls/
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.textbox?view=windowsdesktop-6.0
 
bạn nên bỏ code vô trong thẻ code như ở dưới cho dễ nhìn
C#:
public class VBButton : Button
{
//Fields
private int borderSize = 0;
private int borderRadius = 18;
private Color borderColor = Color.BurlyWood;

//Properties
[Category("Custom Props")]
public int BorderSize
{
    get { return borderSize; }
    set
    {
        borderSize = value;
        this.Invalidate();
    }
}

[Category("Custom Props")]
public int BorderRadius
{
    get { return borderRadius; }
    set
    {
        borderRadius = value;
        this.Invalidate();
    }
}

[Category("Custom Props")]
public Color BorderColor
{
    get { return borderColor; }
    set
    {
        borderColor = value;
        this.Invalidate();
    }
}

[Category("Custom Props")]
public Color BackgroundColor
{
    get { return this.BackColor; }
    set { this.BackColor = value; }
}

[Category("Custom Props")]
public Color TextColor
{
    get { return this.ForeColor; }
    set { this.ForeColor = value; }
}

//Constructor
public VBButton9()
{
    this.FlatStyle = FlatStyle.Flat;

    this.FlatAppearance.BorderSize = 0;
    this.Size = new Size(100, 36);
    //this.BackColor = Color.Coral;cach lay mau 1
    this.BackColor = Color.FromArgb(33, 37, 41); ;//cach ly mau 2
    this.ForeColor = Color.White;
    this.Resize += new EventHandler(Button_Resize);
}

//Methods
private GraphicsPath GetFigurePath(Rectangle rect, float radius)
{
    GraphicsPath path = new GraphicsPath();
    float curveSize = radius * 2F;

    path.StartFigure();
    path.AddArc(rect.X, rect.Y, curveSize, curveSize, 180, 90);
    path.AddArc(rect.Right - curveSize, rect.Y, curveSize, curveSize, 270, 90);
    path.AddArc(rect.Right - curveSize, rect.Bottom - curveSize, curveSize, curveSize, 0, 90);
    path.AddArc(rect.X, rect.Bottom - curveSize, curveSize, curveSize, 90, 90);
    path.CloseFigure();
    return path;
}

protected override void OnPaint(PaintEventArgs pevent)
{
    base.OnPaint(pevent);
    pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

    Rectangle rectSurface = new Rectangle(0, 0, this.Width, this.Height);
    Rectangle rectBorder = new Rectangle(1, 1, (int)(this.Width - 0.8f), this.Height - 1);
    int smoothSize = 2;
    if (borderSize > 0)
        smoothSize = borderSize;

    if (borderRadius > 2) //Rounded button
    {
        using (GraphicsPath pathSurface = GetFigurePath(rectSurface, borderRadius))
        using (GraphicsPath pathBorder = GetFigurePath(rectBorder, borderRadius - borderSize))
        using (Pen penSurface = new Pen(this.Parent.BackColor, smoothSize))
        using (Pen penBorder = new Pen(borderColor, borderSize))
        {
            //Button surface
            this.Region = new Region(pathSurface);
            //Draw surface border for HD result
            pevent.Graphics.DrawPath(penSurface, pathSurface);

            //Button border
            if (borderSize >= 1)
                //Draw control border
                pevent.Graphics.DrawPath(penBorder, pathBorder);
        }
    }
    else //Normal button
    {
        //Button surface
        this.Region = new Region(rectSurface);
        //Button border
        if (borderSize >= 1)
        {
            using (Pen penBorder = new Pen(borderColor, borderSize))
            {
                penBorder.Alignment = PenAlignment.Inset;
                pevent.Graphics.DrawRectangle(penBorder, 0, 0, this.Width - 1, this.Height - 1);
            }
        }
    }
}

protected override void OnHandleCreated(EventArgs e)
{
    base.OnHandleCreated(e);
    this.Parent.BackColorChanged += new EventHandler(Container_BackColorChanged);
}

private void Container_BackColorChanged(object sender, EventArgs e)
{
    this.Invalidate();
}

private void Button_Resize(object sender, EventArgs e)
{
    if (borderRadius > this.Height)
    borderRadius = this.Height;
}
}

trên window form thì nó có cái textbox, bạn tạo 1 cái textbox để cho người dùng nhập dữ liệu, xong lấy thuộc tính text của nó, convert thành số rồi gán cho button thôi
https://www.geeksforgeeks.org/c-sharp-textbox-controls/
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.textbox?view=windowsdesktop-6.0
Button trên form thì e làm đc còn đây là tạo 1 class library để project khác add vào thư viện .dll nên không biết làm
 

dammage

Rìu Chiến
Button trên form thì e làm đc còn đây là tạo 1 class library để project khác add vào thư viện .dll nên không biết làm
đọc kĩ lại thì hình như ý của bạn là muốn nhập dữ liệu trực tiếp cho button luôn thì phải, ý là muốn button có luôn các chức năng của textbox đúng không, bạn có thể gọi hàm inputbox để lấy user text input
https://www.csharp-examples.net/inputbox-class/

lấy xong convert thành số rồi gán cho mấy properties của button
 
Sửa lần cuối:
Thêm hàm khởi tạo có chứa tham số đầu vào, điều này cơ bản của OOP
thay đổi giá trị _height với _width bằng giá trị khác sao nó không thay đổi bác

private readonly int _height = 40;
private readonly int _width = 100
//Constructor
public VBButton()
{
this.FlatStyle = FlatStyle.Flat;
this.FlatAppearance.BorderSize = 0;
this.Size =new Size(_width, _height);
this.BackColor = Color.FromArgb(13, 110, 252);
this.ForeColor = Color.White;
this.Resize += new EventHandler(Button_Resize);

}
 

HLongIT

Rìu Bạc
thay đổi giá trị _height với _width bằng giá trị khác sao nó không thay đổi bác

private readonly int _height = 40;
private readonly int _width = 100
//Constructor
public VBButton()
{
this.FlatStyle = FlatStyle.Flat;
this.FlatAppearance.BorderSize = 0;
this.Size =new Size(_width, _height);
this.BackColor = Color.FromArgb(13, 110, 252);
this.ForeColor = Color.White;
this.Resize += new EventHandler(Button_Resize);

}
Bạn không biết hàm khởi tạo với tham số đầu vào à, bạn là tay ngang sang code hay sao vậy
 
Bạn không biết hàm khởi tạo với tham số đầu vào à, bạn là tay ngang sang code hay sao vậy
private int height = 40;
private int width = 100
//Constructor
public VBButton(int h,int w)
{
height=h;width=w;
}
//nhưng làm sao nó hiểu được chiều dài button=width, chiều cao button=height được đại ca
 


Top