Is there a way to implement a flat TextBox in C #?

I have a flat style GUI for buttons. I would like to use TextBox controls with the same appearance, but I cannot find where I can configure the external line. Is there any control in WinForms that can be assigned FlatStyle? Thanks!


Change 1

Thanks for the FixedSingle border style information, but then, how can I change the line properties?


Edit 2

I implemented a solution with a small amount of both. I would like if you could help improve this class, since I am not an expert in C #, and I find this code somewhat dirty. 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

ReadOnly. OnClick . OnClick :

class BorderTextBox : UserControl
{
    ...

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

    ...
}

, . ? ?

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

BorderStyle FixedSingle.

: TextBox, , UserControl. , BackColor UserControl SystemColors.WindowFrame, TextBox BorderStyle None. Resize TextBox , ( UserControl, ) 2 5 , .

2: UserControl, "ThextBox" ( Th, 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();
    }
}

, UserControl "ThextBox" , . , " " , . , , , ..

, TextBox (, MaxLength RightToLeft), PasswordChar . TextBox UserControl, , TextBox. .

+8

1:

Hack: ( , )

TextBox , x , TextBox. ( TextBox ), TextBox.

2:

, . TextBox. TextBox Border None, . , , (, ..), , TextBox . :

public class BorderedTextBox: TextBox
{
    protected override void OnPaint( PaintEventArgs pe )
    {
        base.OnPaint(pe);

        // Draw your border here
    }
}
+2

# . "" .

0

BorderStyle BorderStyle.FixedSingle BorderStyle.None?

0

Source: https://habr.com/ru/post/1720692/


All Articles