473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

how to validate the Textbox.text is not empty?

LittleDong
would somebody show me a convenient way to validate Textbox's value are not null or empty in Winform.
if-else condition is only good for fewer textboxs in a winform or solution,
if(Textbox1.text==null)
{
...
}
else
{
...
}

but if there are lots of textbox?
Mar 7 '12 #1

✓ answered by PsychoCoder

There's no need to write a method for this, just use String.IsNullOrEmpty like this

Expand|Select|Wrap|Line Numbers
  1. if(string.IsNullOrEmpty(TextBox1.text))
  2.     //do work here
  3. }
You could also use string.IsNullOrWhiteSpace

10 45063
may this code would help you..
after converting textbox value into string
if(textbox1.text=="")
Mar 7 '12 #2
if there are six textforms in a winform
I think it's not a good way to write
if((Textbox1.text=="")&&(Textbox1.text==null))
.
.
if((Textbox2.text=="")&&(Textbox2.text==null))
.
.
if((Textbox3.text=="")&&(Textbox3.text==null))
.
.
if((Textbox4.text=="")&&(Textbox4.text==null))
.
.
.
.

@mohit2k8
Mar 7 '12 #3
r035198x
13,262 8TB
You have many options, IMO, worst is to create a helper method that does this on a list of text components, much better is to learn the decorator pattern:http://en.wikipedia.org/wiki/Decorator_pattern
You decorate the text boxes with ability to validate their text. You are going to need to learn to learn this pattern if you want to write maintainable gui programs anyway.
Mar 7 '12 #4
hello.. you can create a function to check if the textbox is empty.

sample (vb.net)

Expand|Select|Wrap|Line Numbers
  1. function txtIsEmpty(frm as form)
  2.     dim isempty as boolean=true
  3.         For Each crl As Control In frm.Controls
  4.             If TypeOf crl Is TextBox Then
  5.                 if trim(trl.text)="" then
  6.                     isempty=true
  7.                     exit for
  8.                 else
  9.                     isempty=true
  10.                 end if
  11.             ElseIf TypeOf crl Is GroupBox Then
  12.                 'if the control is groupbox, its contents will not be included.
  13.                 For Each crl1 As Control In crl.Controls
  14.                     If crl1.GetType Is GetType(TextBox) Then
  15.                         if trim(crl1.text)="" then
  16.                             isempty=true
  17.                             exit for
  18.                         else
  19.                             isempty=false
  20.                         end if
  21.                     End If
  22.                 Next
  23.             End If
  24.         Next
  25.     txtIsEmpty=isempty
  26. end function
the above function will check all of the textbox on your form as well as inside the group box

to call the function:

if txtIsEmpty(Form1) then
msgbox "Other texts are empty."
end if
Mar 7 '12 #5
for VB6

Expand|Select|Wrap|Line Numbers
  1. ************
  2. Public function cHecker(frm as form)as Boolean
  3.   dim obj as string
  4.   For each obj in frm
  5.     If typeof obj is textbox then
  6.       If obj.text="" then
  7.          checker=false
  8.          msgbox "Textbox is empty"
  9.          exit function
  10.       End if
  11.     End if
  12.   Next obj
  13. End Function
  14.  
  15. ***********syntax
  16. if checker= true then
  17.    msgbox "Textbox is not empty"
  18. end if
Mar 8 '12 #6
For simply checking to see if a text string is null or empty I would use this:
http://msdn.microsoft.com/en-us/libr...llorempty.aspx
Mar 22 '12 #7
For simply checking to see if a text string is null or empty I would use this:
http://msdn.microsoft.com/en-us/libr...llorempty.aspx
Mar 22 '12 #8
PsychoCoder
465 Expert Mod 256MB
There's no need to write a method for this, just use String.IsNullOrEmpty like this

Expand|Select|Wrap|Line Numbers
  1. if(string.IsNullOrEmpty(TextBox1.text))
  2.     //do work here
  3. }
You could also use string.IsNullOrWhiteSpace
Mar 23 '12 #9
r035198x
13,262 8TB
Did you mean
Expand|Select|Wrap|Line Numbers
  1. if(string.IsNullOrEmpty(Textbox1.text)){
instead?

Mod edit
For line #1 instead of :
Expand|Select|Wrap|Line Numbers
  1. if(string.IsNullOrEmpty(TextBox1))
Mar 23 '12 #10
PsychoCoder
465 Expert Mod 256MB
Yes I did, thanks for seeing that
Mar 23 '12 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: rodger | last post by:
How do i validate text box entry, in VB. net In the event that user does not make a text entry, simply pop-up a warning, thanks rodger
2
by: TattyMane bigpond.net.au> | last post by:
asp.net, visual studio 2003, IE6 I have a sample web page that is linked to another page. On the first page, I have a text box, on the second, the first page's text box text is displayed using a...
5
by: Yuelin | last post by:
Hi there What's the best way of checking whether a textbox is empty? Thanks. Yuelin
5
by: Steve S | last post by:
Heres what I want to do...User types into a texbox, clicks a button, the button saves that text to a file. The problem is that when I click the submit button, any changes made to the textbox are...
2
by: Andrea | last post by:
If anyone has a simple routine to validate that text entered in a text box control contains only numbers (zip code) or only alpha characters (name), can you please send me? I'm new to this, and...
5
by: Agnes | last post by:
Except trim(Me.txtTextBox.text) = "" ?? for vfp, there is empty(), for c++ there is isblank() . any function call can check it in .net ??? Thanks From Agnes
9
by: B-Dog | last post by:
I have a form that has about 10 text boxes on it, they all have to be filled out before submitting is there a quick way to make sure that none are null or do I have to call out each textbox? Say...
1
by: ravipatil | last post by:
hi i am adding two numbers in C#.net & i want to check wheather first textbox is empty or not. if empty, then it must display "enter the first number" otherwise value is stored in first textbox1....
6
by: krishnaneeraja | last post by:
Hi, Iam developing windows application using vb.net with c#.net.In this i want to validate textbox accept only negative float numbers like -2.3 etc.... please help me.
4
by: sbandalli | last post by:
How to check if the text box is empty in C#?? I have to display an error box if the textbox is empty. I am trying this way,but no results. Thank you for the help. private void Save_Click(object...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.