如何在C#中验证文本框 [英] How to validate textbox in C#

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

问题描述

大家好,

我有五个文本框,我希望用户在单击保存"按钮之前先进行填充.

我需要一个代码来告诉他们这些文本框是否为空.

Hi All,

I have five textboxes that i want users to fill before clicking the save button.

I need a code that will tell them if any of those textbox are empty.

推荐答案

使用string.IsNullOrEmpty或使用验证器.这是一项微不足道的任务,您应该考虑在Winforms上购买一本书.
Use string.IsNullOrEmpty or use a validator. This is a trivial task, you should consider buying a book on Winforms.


请不要按"answer"来添加评论.编辑您的帖子.如果您不想使用验证器,那么编写的代码显然是微不足道的,这就是为什么我建议您还购买一本书以帮助您了解winforms工作原理的原因.有一些事件,例如由您的保存"按钮触发的事件.您可以编写代码来运行,也可以访问和检查文本框的Text属性.验证程序会为您完成所有这些操作,但是编写您自己并不难.
PLease don''t push ''answer'' to add comments. Edit your post. If you don''t want to use a validator, the code to write instead is obviously trivial, which is why I suggest you also buy a book to help you understand the basics of how winforms works. There are events, such as the event fired by your save button. You can write code to run, and you can access and check the Text property of your textboxes. A validator does all this for you, but it''s not hard to write yourself.


只需将代码添加到保存按钮的按钮单击事件中即可.如果任何一个框都不包含(有效)条目,则弹出一条消息以提醒用户.我不是C#专家(我的PC上没有编译器要检查),但是我可以肯定地肯定有些事件链接到文本框,从而使您可以禁用Save按钮,直到所有框中包含有效的条目.
Just add the code to your button click event for the save button. If any of the boxes do not contain (valid) entries then pop a message to alert the user. I''m not a C# expert (and I don''t have a compiler on this PC to check) but I am reasonably sure there are also some events linked to the text boxes that would allow you to disable the Save button until all boxes contain valid entries.


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

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