如何在C#中更改文本框的边框颜色? [英] How do I change the border colour of a textbox in C#?

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

问题描述

您好,



我有5个TextBox,我想更改空文本框的边框颜色。我有一个按钮来保存所有TextBox的数据,现在我想在同一个按钮中进行验证,如果TextBox为空,它会将相应空TextBox的边框颜色更改为红色。



请帮忙。



我的尝试:



我无法编写代码,因为我不知道这个概念。

解决方案

认为这是Windows Forms没有直接的方法改变边框的颜色。但是,您可以在边框上方绘制一个矩形,使控件看起来像颜色已更改。



查看以下讨论:

- c# - 更改TextBox的borderColor - Stack Overflow [ ^ ]

- 如何在C#中更改TextBox边框颜色? [ ^ ]

1。将Panel放在Form上,将其BackgroundColor属性设置为您想要的边框颜色。将Panel的Padding属性设置为所需边框的大小。



2.将TextBox粘贴到Panel中。将其Dock属性设置为Fill。



您可以操作Panel和TextBox的BorderStyle属性来实现不同的视觉效果。



验证时(快速草图):

 EmptyTbxBackColor = Color.Red; 

foreach (Textbox tbx in MyListOfTextBoxesToBeValidared)
{
if (tbx.Text == String .Empty)tbx.Parent.BackColor = EmptyTbxBackColor;
}


Hello,

I have 5 TextBoxes and I want to change the border colour of the TextBoxes that are empty. I have a button to save the data of all the TextBoxes and now I want to put a validation in the same button that if a TextBox is empty, it will change the border colour of the respective empty TextBox to Red.

Please help.

What I have tried:

I am unable to write the code as I am unaware of the concept.

解决方案

Taken this is Windows Forms there's no direct way to change the colour of the border. However, you can draw a rectangle above the border to make the control look like the colour has changed.

Have a look at the following discussions:
- c# - Change the borderColor of the TextBox - Stack Overflow[^]
- How to change TextBox border color in C# ?[^]


1. Put a Panel on your Form, set its BackgroundColor property to the border color you want. Set the Panel's Padding property to the size of the border you want.

2. Paste the TextBox inside the Panel. Set its Dock property to Fill.

You can manipulate the BorderStyle property of both Panel and TextBox to achieve different visual effects.

When you validate (quick sketch):

EmptyTbxBackColor = Color.Red;

foreach (Textbox tbx in MyListOfTextBoxesToBeValidared)
{
   if(tbx.Text == String.Empty) tbx.Parent.BackColor = EmptyTbxBackColor;
}


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

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