当前位置:网站首页>VS+C# 实现窗体输入框默认显示灰色文字
VS+C# 实现窗体输入框默认显示灰色文字
2022-04-23 00:58:00 【dontgotobed】
需求
- 一个查询输入框,默认显示灰色文字;
- 鼠标点击后默认文字消失,允许用户自己输入黑色文字;
- 一个确定按钮,点击后输入框变为只读,文字看起来“锁定”
图示
初始:

鼠标点击输入框后:

输入文字后:

点击查询按钮后:

实现
使用控件:
| 控件 | 名称 |
|---|---|
| TextBox | textBox1 |
| Button | button1 |
代码
private void textBox1_Click(object sender, EventArgs e)
{
this.textBox1.ReadOnly = false; //取消只读
this.textBox1.ForeColor = Color.Black; //设置文本颜色
this.textBox1.Text = ""; //清空默认文字
}
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.ReadOnly = true; //设置只读
}
其他默认设置
- textBox1 的属性:设置文字颜色和默认文字



- button1 的属性:设置显示文字


收获
- 输入框用 TextBox 控件
- TextBox 控件的 ForeColor 属性为文本颜色
- 最重要:ReadOnly 和 Enabled 的区别:
ReadOnly 是只读,即不可编辑文字; Enabled 是直接设置该输入框能否继续使用。如果 Enabled 被设置为 False,那么该输入框对象的 Click 功能等都将被禁用。
最开始想把输入框默认 Enabled 属性设置为 false,然后 textBox1_Click() 函数中再将 this.textBox1.Enabled = true,但根本不奏效。直到浏览 TextBox 的属性时发现了 ReadOnly 这个属性,豁然开朗。
仍旧存在的问题
用户必须一次性输入完成并点击确定;如果用户输入过程中(即本次输入已经开始但未完成)再次点击输入框,那么之前输入的内容都将被清空。
版权声明
本文为[dontgotobed]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_48335178/article/details/124329900
边栏推荐
- leetcode 134. 加油站
- Android international area code, registered mobile phone number code and list of common cities, Android memory optimization interview
- 2.56 - try running show with different sample values_ Bytes code.
- Research and application of power monitoring system in sports training center
- Change your posture to look at hooks, the combination of inspiration sources and the separation of new ideas from logical view in hoc mode
- BUUCTF 穿越时空的思念
- C#中的类型转换-自定义隐式转换和显式转换
- leetcode 396. Rotation function
- What is tooljet and how about it—— Evaluation of low code development platform
- [ACTF2020 新生赛]Include
猜你喜欢

Yunrong technology joined the dragon dragon dragon community to help the digital transformation of the financial industry

flask项目跨域拦截处理以及dbm数据库学习【包头文创网站开发】

The origin explanation and use example of image pre training model

Modify computer IP from the command line
![[HCTF 2018]admin](/img/08/8111f2f87796dda8dd6b0dcf72c42b.png)
[HCTF 2018]admin

BUUCTF 穿越时空的思念

DCB "first brother" xianruida, what does it rely on to raise the value curve?

Cross domain problem and solving cross domain problem by UMI proxy proxy

Amazon Aurora's ability to read and write: shardingsphere proxy

Lightly: a new generation of cloud IDE
随机推荐
leetcode 396. 旋转函数
Initial experience of talent plan learning camp: communication + adhering to the only way to learn open source collaborative courses
Software testing immortal documents, even Ali interviewers said it was too detailed. Understanding these directly is P7 level
Analysis and configuration process of epit periodic timer for imx6ull bare metal development
The origin explanation and use example of image pre training model
IMX6ULL裸机开发之硬件IIC分析及配置过程
Amazon Aurora's ability to read and write: shardingsphere proxy
Deloitte 2022 technology trend: it self subversion, technology cross-border integration and innovation
Get in the car, the era of intelligent database autonomy has come, and Tencent cloud database x AI has made a new breakthrough
L3-1 straight beat Huanglong (30 points) DJK + DFS
From construction to governance, the industry's first white paper on microservice governance technology was officially released (including a free download link)
Im instant messaging development how to design a database that can support millions of concurrent users
[Blue Bridge Cup real question 18] scratch addition multiple-choice question youth group scratch Blue Bridge Cup real question and answer explanation
Hardware IIC analysis and configuration process of imx6ull bare metal development
C#中的类型转换-自定义隐式转换和显式转换
C WPF UI framework mahapps switching theme
Design and application of intelligent lighting control system in hospital
DCB "first brother" xianruida, what does it rely on to raise the value curve?
Secret of 66% performance surge: AMD 25000 yuan 768mb 3D cache Xiaolong opened the cover for the first time
tdengine集群安装部署