I have a GUI with a flat style for the buttons. I would like to use TextBox controls with the same appearance, but I can’t find where can I configure the outer line with. Is there any control in WinForms which can be given FlatStyle? Thanks!
Edit 1
Thanks for the information about FixedSingle border style, but then, how can I change the line properties?
Edit 2
I’ve implemented a solution with a little bit of both. I would like if you could help improving this class, as I’m not an expert in C# and I find this code somewhat messy. Here is the code:
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();
}
}
}
Edit 3
I’m having some problems in implementing the ReadOnly property. I was trying to prevent the edit box to process the OnClick event and show the intermitent cursor inside. When I define the OnClick method inside this class:
class BorderTextBox : UserControl
{
...
protected override void OnClick(EventArgs e)
{
if (!ReadOnly)
base.OnClick(e);
}
...
}
This method only gets the clicks on the border, but not inside the textBox. Is there a way to catch that events? or how can you remove the event handlers of an element inside your component?
m_textBox.Click -= //the EventHandler we don't want
Set the
BorderStyleof the TextBox toFixedSingle.Update: there isn’t an easy way to control the border width of a TextBox, but you can easily create this effect yourself by creating your own UserControl. Basically, you would just set the
BackColorof the UserControl toSystemColors.WindowFrame, and then put a TextBox on the control with aBorderStyleofNone. In the Resize event of the control, you can then reposition the TextBox so that it leaves a border (which is just the UserControl itself showing through around the edges) of 2 pixels or 5 or whatever you want.Update 2: I’ve written a sample UserControl called “ThextBox” ( for Thick-bordered T extBox ) that has an adjustable border:
To use this code in your project, add a UserControl named “ThextBox” to your project and paste this code over what the designer added. This control has the adjustable border, and it also “plays nicely” with the designer, allowing you to set all its relevant properties in design mode. It also automatically persists things like the text entered, the border width, the password character etc.
If you need to extend this by including additional TextBox-specific properties (such as
MaxLengthandRightToLeft, just follow the example in thePasswordCharproperty of this control. One of the advantages of inheriting directly from TextBox instead of UserControl like this would be that your control would automatically have all of the TextBox properties. However, you couldn’t do the border that way.