我有一个带按钮的平面样式的GUI.我想使用具有相同外观的TextBox控件,但我找不到在哪里可以配置外线.WinForms中是否有任何控件可以给FlatStyle?谢谢!
编辑1
感谢有关FixedSingle边框样式的信息,但是,如何更改行属性?
编辑2
我已经实现了两者兼而有之的解决方案.我想如果你能帮助改进这门课程,因为我不是C#的专家,我发现这段代码有些混乱.这是代码:
class BorderTextBox : UserControl
{
private TextBox m_textBox;
private int m_borderSize;
private void ResizeComponent()
{
m_textBox.Size = new Size(Size.Width - 2 * m_borderSize, m_textBox.Size.Height);
Size = new Size(Size.Width, m_textBox.Size.Height + 2 * m_borderSize);
}
protected override void OnResize(EventArgs z_event)
{
base.OnResize(z_event);
ResizeComponent();
}
public BorderTextBox()
{
SuspendLayout();
// TextBox
m_textBox = new TextBox();
m_textBox.BorderStyle = BorderStyle.None;
m_textBox.Name = "textBox";
m_textBox.TabIndex = 0;
// Body
BackColor = Color.Black;
Name = "Body";
Controls.Add(m_textBox);
ResumeLayout(false);
PerformLayout();
}
public bool UsePasswordStyle
{
get { return m_textBox.UseSystemPasswordChar; }
set { m_textBox.UseSystemPasswordChar = value; }
}
public int BorderSize
{
get { return m_borderSize; }
set
{
m_borderSize = value;
m_textBox.Location = new Point(m_borderSize, m_borderSize);
ResizeComponent();
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑3
我在实现ReadOnly属性时遇到了一些问题.我试图阻止编辑框处理OnClick事件并在内部显示intermitent游标.当我在这个类中定义OnClick方法时:
class BorderTextBox : UserControl
{
...
protected override void OnClick(EventArgs e)
{
if (!ReadOnly)
base.OnClick(e);
}
...
}
Run Code Online (Sandbox Code Playgroud)
此方法仅获取边框上的单击,但不获取textBox内的单击.有没有办法捕捉那些事件?或者如何删除组件内元素的事件处理程序?
m_textBox.Click -= //the EventHandler we don't want
Run Code Online (Sandbox Code Playgroud)
将BorderStyleTextBox 设置为FixedSingle.
更新:没有一种简单的方法来控制TextBox的边框宽度,但您可以通过创建自己的UserControl轻松地创建此效果.基本上,你只需设置BackColor的用户控件来的SystemColors.WindowFrame,然后把一个文本框上有控制BorderStyle的None.在控件的Resize事件中,您可以重新定位TextBox,使其留下2像素或5或任何您想要的边框(只是UserControl本身显示的边缘).
更新2: 我已经写了名为"ThextBox"(对于样本用户控件的Th ICK-接壤牛逼extBox)具有可调边框:
public partial class ThextBox : UserControl
{
private TextBox _TextBox;
private int _BorderWidth = 1;
[Browsable(true)]
[DesignerSerializationVisibility
(DesignerSerializationVisibility.Visible)]
public override string Text
{
get
{
return _TextBox.Text;
}
set
{
_TextBox.Text = value;
}
}
[DesignerSerializationVisibility
(DesignerSerializationVisibility.Visible)]
public bool Multiline
{
get
{
return _TextBox.Multiline;
}
set
{
_TextBox.Multiline = value;
ResizeMe();
}
}
[DesignerSerializationVisibility
(DesignerSerializationVisibility.Visible)]
public bool UseSystemPasswordChar
{
get
{
return _TextBox.UseSystemPasswordChar;
}
set
{
_TextBox.UseSystemPasswordChar = value;
}
}
[DesignerSerializationVisibility
(DesignerSerializationVisibility.Visible)]
public char PasswordChar
{
get
{
return _TextBox.PasswordChar;
}
set
{
_TextBox.PasswordChar = value;
}
}
[DesignerSerializationVisibility
(DesignerSerializationVisibility.Visible)]
public int BorderWidth
{
get
{
return _BorderWidth;
}
set
{
_BorderWidth = value;
ResizeMe();
}
}
public ThextBox()
{
InitializeComponent();
this.BackColor = SystemColors.WindowFrame;
_TextBox = new TextBox();
_TextBox.Multiline = false;
_TextBox.BorderStyle = BorderStyle.None;
this.Controls.Add(_TextBox);
ResizeMe();
}
protected override void OnFontChanged(EventArgs e)
{
base.OnFontChanged(e);
ResizeMe();
}
private void ResizeMe()
{
if (this.Multiline)
{
_TextBox.Height = this.Height - (2 * _BorderWidth);
}
else
{
this.Height = _TextBox.Height + (2 * _BorderWidth);
}
_TextBox.Width = this.Width - (2 * _BorderWidth);
_TextBox.Location = new Point(_BorderWidth, _BorderWidth);
}
private void ThextBox_Resize(object sender, EventArgs e)
{
ResizeMe();
}
}
Run Code Online (Sandbox Code Playgroud)
要在项目中使用此代码,请将名为"ThextBox"的UserControl添加到项目中,并将此代码粘贴到设计器添加的内容中.该控件具有可调边框,并且与设计人员"很好地配合",允许您在设计模式下设置其所有相关属性.它还会自动保留输入的文本,边框宽度,密码字符等内容.
如果你需要通过包含额外的特定于TextBox的属性来扩展它(例如,MaxLength并且RightToLeft,只需按照PasswordChar此控件的属性中的示例.直接从TextBox继承而不是像这样继承UserControl的一个优点是你的控件会自动拥有所有TextBox属性.但是,你不能这样做边框.
| 归档时间: |
|
| 查看次数: |
8095 次 |
| 最近记录: |