В конструкторе я установил пустое свойство Text ""
this.Text = "";
Я также попробовал string.Empty вместо ""
но я продолжаю видеть кнопку после перетаскивания ее из панели инструментов в дизайнере form1, текст и также посмотрите в проводнике решений справа в свойстве Text текст.
Я тестировал, и если я изменю размер и перетащу одну из кнопок из панели инструментов, размер изменится, но свойство Text никогда не становится пустым.
то же самое, если я перетаскиваю из панели инструментов пользовательскую кнопку 2, 3 или 4.
< img alt="свойство text не пусто и показывает customButton1" src="https://i.sstatic.net/ckbZ7TgY.jpg" />
using System;
using System.Drawing;
using System.Windows.Forms;
namespace ImageTest
{
public class CustomButton : Button
{
public enum ShapeType
{
Shape1,
Shape2,
Shape3,
Shape4,
Shape5 // Add more shapes as needed
}
public ShapeType ButtonShape { get; set; }
public CustomButton()
{
this.Size = new Size(100, 100);
this.FlatStyle = FlatStyle.Flat;
this.FlatAppearance.BorderSize = 0;
this.BackColor = Color.White;
this.Text = "";
}
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
Graphics g = pevent.Graphics;
Pen pen = new Pen(Color.Black, 2);
switch (ButtonShape)
{
case ShapeType.Shape1:
DrawShape1(g, pen);
break;
case ShapeType.Shape2:
DrawShape2(g, pen);
break;
case ShapeType.Shape3:
DrawShape3(g, pen);
break;
case ShapeType.Shape4:
DrawShape4(g, pen);
break;
case ShapeType.Shape5:
DrawShape5(g, pen);
break;
}
}
private void DrawShape1(Graphics g, Pen pen)
{
// Large rectangle with two small rectangles inside
Rectangle mainRect = new Rectangle(10, 10, 80, 40);
g.DrawRectangle(pen, mainRect);
// Two inner rectangles (top and bottom)
Rectangle topRect = new Rectangle(mainRect.X, mainRect.Y, mainRect.Width / 2, mainRect.Height / 2);
Rectangle bottomRect = new Rectangle(mainRect.X + mainRect.Width / 2, mainRect.Y, mainRect.Width / 2, mainRect.Height);
g.DrawRectangle(pen, topRect);
g.DrawRectangle(pen, bottomRect);
}
private void DrawShape2(Graphics g, Pen pen)
{
// Large rectangle split in half horizontally, with one half filled
Rectangle mainRect = new Rectangle(10, 10, 80, 40);
g.DrawRectangle(pen, mainRect);
// Fill one side with color
Rectangle sideRect = new Rectangle(mainRect.X + mainRect.Width / 2, mainRect.Y, mainRect.Width / 2, mainRect.Height);
g.FillRectangle(Brushes.Black, sideRect);
}
private void DrawShape3(Graphics g, Pen pen)
{
// Large rectangle with two smaller rectangles in lower half
Rectangle mainRect = new Rectangle(10, 10, 80, 40);
g.DrawRectangle(pen, mainRect);
// Two inner rectangles (left and right on bottom)
Rectangle leftRect = new Rectangle(mainRect.X, mainRect.Y + mainRect.Height / 2, mainRect.Width / 2, mainRect.Height / 2);
Rectangle rightRect = new Rectangle(mainRect.X + mainRect.Width / 2, mainRect.Y + mainRect.Height / 2, mainRect.Width / 2, mainRect.Height / 2);
g.DrawRectangle(pen, leftRect);
g.DrawRectangle(pen, rightRect);
}
private void DrawShape4(Graphics g, Pen pen)
{
// Large rectangle with a smaller yellow square inside
Rectangle mainRect = new Rectangle(10, 10, 80, 40);
g.DrawRectangle(pen, mainRect);
// Small square in center
Rectangle smallSquare = new Rectangle(mainRect.X + mainRect.Width / 2, mainRect.Y + mainRect.Height / 2, mainRect.Width / 4, mainRect.Height / 4);
g.FillRectangle(Brushes.Yellow, smallSquare);
}
private void DrawShape5(Graphics g, Pen pen)
{
// Example of another shape layout - Adjust as needed
Rectangle mainRect = new Rectangle(10, 10, 80, 40);
g.DrawRectangle(pen, mainRect);
// Additional drawing for complex shapes
// Use specific coordinates to replicate the exact pattern
Rectangle topLeft = new Rectangle(mainRect.X, mainRect.Y, mainRect.Width / 2, mainRect.Height / 2);
Rectangle bottomRight = new Rectangle(mainRect.X + mainRect.Width / 2, mainRect.Y + mainRect.Height / 2, mainRect.Width / 2, mainRect.Height / 2);
g.DrawRectangle(pen, topLeft);
g.DrawRectangle(pen, bottomRight);
}
}
}
и класс, унаследованный от пользовательской кнопки
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace ImageTest
{
[ToolboxItem(true)]
public class ShapeButton : CustomButton
{
public ShapeButton()
{
this.ButtonShape = ShapeType.Shape1;
}
}
[ToolboxItem(true)]
public class Shape2Button : CustomButton
{
public Shape2Button()
{
this.ButtonShape = ShapeType.Shape2;
this.Text = string.Empty;
}
}
[ToolboxItem(true)]
public class Shape3Button : CustomButton
{
public Shape3Button()
{
this.ButtonShape = ShapeType.Shape3;
this.Text = string.Empty;
}
}
[ToolboxItem(true)]
public class Shape4Button : CustomButton
{
public Shape4Button()
{
this.ButtonShape = ShapeType.Shape4;
this.Text = string.Empty;
}
}
// Add additional shapes as needed
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... tton-class
Почему свойство Text никогда не бывает пустым в классе CustomButton? ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Как обновить свойство в родительском классе, когда вложенное свойство изменяется в
Anonymous » » в форуме C# - 0 Ответы
- 18 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Как обновить свойство в родительском классе, когда вложенное свойство изменяется в
Anonymous » » в форуме C# - 0 Ответы
- 13 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Как обновить свойство в родительском классе, когда вложенное свойство изменяется в
Anonymous » » в форуме C# - 0 Ответы
- 10 Просмотры
-
Последнее сообщение Anonymous
-