有没有办法在 C# 中实现平面 TextBox? [英] Is there a way to implement a flat TextBox in C#?

查看:33
本文介绍了有没有办法在 C# 中实现平面 TextBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个平面样式的 GUI 按钮.我想使用具有相同外观的 TextBox 控件,但我找不到在哪里可以配置外线.WinForms 中是否有任何可以赋予 FlatStyle 的控件?谢谢!

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!

编辑 1

感谢您提供有关 FixedSingle 边框样式的信息,但是,我该如何更改线条属性?

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

编辑 2

我已经实现了一个兼具两者的解决方案.我希望您能帮助改进这门课,因为我不是 C# 专家,而且我发现这段代码有些凌乱.代码如下:

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();
        }
    }
}

<小时>

编辑 3

我在实现 ReadOnly 属性时遇到了一些问题.我试图阻止编辑框处理 OnClick 事件并在里面显示间歇性光标.当我在这个类中定义 OnClick 方法时:

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

推荐答案

将TextBox的BorderStyle设置为FixedSingle.

Set the BorderStyle of the TextBox to FixedSingle.

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

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 BackColor of the UserControl to SystemColors.WindowFrame, and then put a TextBox on the control with a BorderStyle of None. 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.

更新 2: 我编写了一个名为ThextBox"的示例 UserControl(用于 Thick-bordered T extBox),它有一个可调整边框:

Update 2: I've written a sample UserControl called "ThextBox" ( for Thick-bordered T extBox ) that has an adjustable border:

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 添加到您的项目并将此代码粘贴到设计者添加的内容上.该控件具有可调整的边框,而且它也与设计器配合得很好",允许您在设计模式下设置其所有相关属性.它还会自动保留输入的文本、边框宽度、密码字符等内容.

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.

如果您需要通过包含额外的 TextBox 特定属性(例如 MaxLengthRightToLeft,请按照 PasswordChar 中的示例进行扩展)> 此控件的属性.直接从 TextBox 继承而不是像这样继承 UserControl 的优点之一是,您的控件将自动拥有所有 TextBox 属性.但是,您不能那样做边框.

If you need to extend this by including additional TextBox-specific properties (such as MaxLength and RightToLeft, just follow the example in the PasswordChar property 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.

这篇关于有没有办法在 C# 中实现平面 TextBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆