有没有办法在C#中实现平面TextBox?

我有一个带按钮的平面样式的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(); } } } 

编辑3

我在实现ReadOnly属性时遇到了一些问题。 我试图阻止编辑框处理OnClick事件并在内部显示intermitent游标。 当我在这个类中定义OnClick方法时:

 class BorderTextBox : UserControl { ... protected override void OnClick(EventArgs e) { if (!ReadOnly) base.OnClick(e); } ... } 

此方法仅获取边框上的单击,但不在textBox内。 有没有办法捕捉那些事件? 或者如何删除组件内元素的事件处理程序?

 m_textBox.Click -= //the EventHandler we don't want 

将TextBox的BorderStyle设置为FixedSingle

更新 :没有一种简单的方法来控制TextBox的边框宽度,但您可以通过创建自己的UserControl轻松地创建此效果。 基本上,您只需将UserControl的BackColor设置为SystemColors.WindowFrame ,然后将TextBox放在控件上, BorderStyleNone 。 在控件的Resize事件中,您可以重新定位TextBox,使其留下2像素或5或任何您想要的边框(只是UserControl本身显示的边缘)。

更新2:我已经编写了一个名为“ThextBox”的示例UserControl(对于Thickbordered T 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(); } } 

要在项目中使用此代码,请将名为“ThextBox”的UserControl添加到项目中,并将此代码粘贴到设计器添加的内容上。 此控件具有可调边框,并且还与设计器“很好地配合”,允许您在设计模式下设置其所有相关属性。 它还会自动保留输入的文本,边框宽度,密码字符等内容。

如果你需要通过包含其他特定于TextBox的属性(例如MaxLengthRightToLeft来扩展它,只需按照此控件的PasswordChar属性中的示例。直接从TextBoxinheritance而不是像这样inheritanceUserControl的一个好处就是你的控件会自动拥有所有TextBox属性。但是,你不能这样做边框。

选项1:

一个稍微丑陋的哈克:(除非绝对必要,否则不推荐)

您可以使用没有边框的TextBox,并将其添加到比TextBox大x像素的面板中。 通过更改Panel(并将TextBox保持在中间),您可以改变TextBox边框的外观。

选项2:

一个更强大的解决方案,但更多的是痛苦。 您可以创建自己的自定义控件并从TextBoxinheritance。 通过将TextBox的Border设置为None,您可以将自己的边框绘制到控件上。 这样做的好处是你可以完全控制边框的样子(例如圆角等)但是要注意,这可能会侵占TextBox中的文本并且看起来很奇怪。 你可以这样做:

 public class BorderedTextBox: TextBox { protected override void OnPaint( PaintEventArgs pe ) { base.OnPaint(pe); // Draw your border here } } 

您可以在c#中的文本框控件上设置边框样式。 在我的系统上,默认情况下它们已经设置为“平面”样式。

您是否尝试将BorderStyle属性设置为BorderStyle.FixedSingleBorderStyle.None