如何在C#中更改Textbox BorderColor? [英] How to change Textbox BorderColor in C#?

查看:126
本文介绍了如何在C#中更改Textbox BorderColor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要在单击文本框时更改文本框边框颜色".
如果有人知道该怎么做,请告诉我.

我在form_load事件中尝试过,但它适用于表单中的所有文本框.

我希望在一个特定的文本框中单击它.


问候,
Pawan.

Hi all,

I need to change the Textbox Border Color at the time of Textbox Click.
If any body knows how to do this then please let me know.

I tried it in form_load event but it applies for all textboxes in the form.

I want it in one particular Textbox click.


Regards,
Pawan.

推荐答案

为了直接从您的UserControl处理内部TextBox的事件,您可以公开所需的事件.这是此想法的示例实现:
In order to work with the events of the inner TextBox, directly from your UserControl, you can expose the events that you need. Here is an example implementation of this idea:
public partial class TextBoxEx : UserControl
{
    // The TextBox
    private TextBox textBox = new TextBox();

    // Border color of the textbox
    private Color borderColor = Color.Gray;

    // Ctor
    public TextBoxEx()
    {
        InitializeComponent();
        this.Paint += new PaintEventHandler(TextBoxEx_Paint);
        this.Resize += new EventHandler(TextBoxEx_Resize);
        textBox.Multiline = true;
        textBox.BorderStyle = BorderStyle.None;
        this.Controls.Add(textBox);

        InvalidateSize();
    }

    // Exposed properties of the textbox
    public override string Text
    {
        get { return textBox.Text; }
        set { textBox.Text = value; }
    }
    // ... Expose other properties you need...

    // The border color property
    public Color BorderColor
    {
        get { return borderColor; }
        set { borderColor = value; Invalidate(); }
    }

    // Expose the Click event for the texbox
    public event EventHandler TextBoxClick
    {
        add { textBox.Click += value; }
        remove { textBox.Click -= value; }
    }
    // ... Expose other events you need...

    private void TextBoxEx_Resize(object sender, EventArgs e)
    {
        InvalidateSize();
    }
    private void TextBoxEx_Paint(object sender, PaintEventArgs e)
    {
        ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, borderColor, ButtonBorderStyle.Solid);
    }
    private void InvalidateSize()
    {
        textBox.Size = new Size(this.Width - 2, this.Height - 2);
        textBox.Location = new Point(1, 1);
    }
}



现在,您甚至可以通过VS设计器使用此事件.这是暴露事件的事件处理程序示例:



Now you can use this event even through the VS designer. Here is an example event handler for the exposed event:

private void textBoxEx1_TextBoxClick(object sender, EventArgs e)
{
    // Change the border color on click
    textBoxEx1.BorderColor = Color.Red;
}




另一种选择是将整个TextBox公开为用户控件中的公共属性,并按照您希望的方式订阅它的事件.
示例:




An alternative is to expose the entire TextBox as a public property in user control and subscribe to it''s events the way you desire.
Example:

public partial class TextBoxEx : UserControl
{
    // The TextBox
    private TextBox textBox = new TextBox();

    // Expose the entire TextBox
    public TextBox InnerTextBox { get {return textBox ; } }
}



完成此操作后,您可以订阅其任何事件,例如(伪代码):
textBoxEx.InnerTextBox.EventOfYourChoice += ...

:)



After doing this you can subscribe on any of its events like (pseudo-code):
textBoxEx.InnerTextBox.EventOfYourChoice += ...

:)


您有一些选择:

1)一种快速而肮脏的方法:在TextBox后面放置一个Label,并使标签比TextBox大一点.使用LabelBackColor属性为TextBox提供不同的边框颜色.

2)创建一个包含无边框TextBox的用户控件,并使用 [
You have some options:

1) A quick and dirty approach: put a Label behind the TextBox and make the label a little bigger than the TextBox. Use the BackColor property of the Label to give the TextBox a different border color.

2) Create a user control that contains a border-less TextBox and draw the border rectangle (with desired color) using ControlPaint.DrawBorder[^]. See this[^] thread for a simple example implementation of this idea.


您可以在点击事件或任何其他事件上使用它.
Textbox1.Style.Add("Border","1px Solid");


您也可以使用CSS

input [type = text]:focus {
边框:1px纯色
}
you can use this on click event or any other.
Textbox1.Style.Add("Border", "1px Solid");


also you can use css

input[type=text]:focus {
border: 1px Solid
}


这篇关于如何在C#中更改Textbox BorderColor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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