如何在C#中更改Textbox BorderColor? [英] How to change Textbox BorderColor in C#?
问题描述
大家好,
我需要在单击文本框时更改文本框边框颜色".
如果有人知道该怎么做,请告诉我.
我在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 innerTextBox
, directly from yourUserControl
, 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
大一点.使用Label
的BackColor
属性为TextBox
提供不同的边框颜色.
2)创建一个包含无边框TextBox
的用户控件,并使用 ^ ].请参阅此 [
You have some options:
1) A quick and dirty approach: put aLabel
behind theTextBox
and make the label a little bigger than theTextBox
. Use theBackColor
property of theLabel
to give theTextBox
a different border color.
2) Create a user control that contains a border-lessTextBox
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屋!