Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer
This is the forum to discuss the Wrox book Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer by Rod Stephens; ISBN: 9780470596906
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old October 29th, 2012, 08:26 PM
Authorized User
Points: 130, Level: 2
Points: 130, Level: 2 Points: 130, Level: 2 Points: 130, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Location: Australia
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default Checking if a text box is empty

Hi,

I want to be able to check if a text box is empty, show a message if it is, otherwise it should continue to the else code.

When i run the program and try to convert when nothing is in the text box, it skips the message and goes straight to an error "Input string was not in a correct format."

I have noted where the program stops. I figure its because the empty text box isnt a double. It seems to disregard the empty textbox message. I hope i am not in over my head here.

Code:
private void btnLengthConvert_Click(object sender, EventArgs e)
        {
          //check if textbox is empty  
          if (tbxLengthToConvert == null)
            {
                MessageBox.Show("test");
            }
            else

            //CENTIMETRES
            //check if the centimetre radio button is checked and then run the math
            if (rbCM.Checked == true)
            {
                double length = double.Parse(tbxLengthToConvert.Text); ERROR
                double mm = length * 10;
                double cm = length * 1;
                double mt = length * 0.01;
                double km = length * 0.00001;
                double inches = length * 2.54;
                double feet = length * 0.032808399;

                tbxLenMm.Text = mm.ToString();
                tbxLenCm.Text = cm.ToString();
                tbxLenMetres.Text = mt.ToString();
                tbxLenKm.Text = km.ToString();
                tbxLenIn.Text = inches.ToString();
                tbxLenFt.Text = feet.ToString();

            }
__________________
"These pretzels are making me thirsty"
Reply With Quote
  #2 (permalink)  
Old October 30th, 2012, 10:15 AM
Rod Stephens's Avatar
Wrox Author
Points: 3,141, Level: 23
Points: 3,141, Level: 23 Points: 3,141, Level: 23 Points: 3,141, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 640
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Quote:
//check if textbox is empty
if (tbxLengthToConvert == null)
What this is really doing is comparing the TextBox itself to null. The TextBox exists so it isn't null and this will never be true.

Instead you need to compare the Text inside the TextBox. And don't compare it to null because blank isn't the same as null. For example, try:

Code:
if (tbxLengthToConvert.Text == "")
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
  #3 (permalink)  
Old October 30th, 2012, 06:36 PM
Authorized User
Points: 130, Level: 2
Points: 130, Level: 2 Points: 130, Level: 2 Points: 130, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Location: Australia
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default

It works, thanks so much. I did try == "" but i didnt have .text before that to associate the text box with the text inside it. Trial and error is a great learning tool. I wont forget that again
__________________
"These pretzels are making me thirsty"
Reply With Quote
  #4 (permalink)  
Old October 30th, 2012, 09:34 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,141, Level: 23
Points: 3,141, Level: 23 Points: 3,141, Level: 23 Points: 3,141, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 640
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Trial and error is a great learning tool only if it's painful enough!
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
  #5 (permalink)  
Old October 31st, 2012, 11:47 PM
Authorized User
Points: 130, Level: 2
Points: 130, Level: 2 Points: 130, Level: 2 Points: 130, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Location: Australia
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default

so true. All that pain and frustration is worth it in the end
__________________
"These pretzels are making me thirsty"
Reply With Quote
  #6 (permalink)  
Old November 19th, 2015, 08:10 PM
Registered User
Points: 9, Level: 1
Points: 9, Level: 1 Points: 9, Level: 1 Points: 9, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2015
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default please help

Hi everyone
now i'm using VS 2010 and i use " if" but it not working correctly

My code :
Code:
if (input == "a\r\n")
                {
                    
                    RFID.Text += (" Name       ------    \r\n ");
                    RFID.SelectionStart = RFID.Text.Length;
                    RFID.ScrollToCaret();
                    RFID.Refresh();


                   
                 }
input is data receive from serial port
RFID.text display only one time
i don't know why
please help me
thankyou so much
Reply With Quote
  #7 (permalink)  
Old November 20th, 2015, 04:07 AM
Rod Stephens's Avatar
Wrox Author
Points: 3,141, Level: 23
Points: 3,141, Level: 23 Points: 3,141, Level: 23 Points: 3,141, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 640
Thanks: 2
Thanked 96 Times in 95 Posts
Default

I know the parentheses shown in red below shouldn't be there. Is anything else going wrong with it?

Code:
if (input == "a\r\n")
{
    RFID.Text += (" Name       ------    \r\n ");
    RFID.SelectionStart = RFID.Text.Length;
    RFID.ScrollToCaret();
    RFID.Refresh();
}
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)

Last edited by Rod Stephens; November 20th, 2015 at 04:10 AM.
Reply With Quote
  #8 (permalink)  
Old November 20th, 2015, 10:53 AM
Registered User
Points: 9, Level: 1
Points: 9, Level: 1 Points: 9, Level: 1 Points: 9, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2015
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Code:
if (input == "a\r\n")
                {
                    
                    RFID.Text += " Name       ------    \r\n ";
                    RFID.SelectionStart = RFID.Text.Length;
                    RFID.ScrollToCaret();
                    RFID.Refresh();


                   
                 }
thanks Rod Stephens
I'm trying but it display only one time again
i can't understand
:(
Reply With Quote
  #9 (permalink)  
Old November 20th, 2015, 11:27 AM
Rod Stephens's Avatar
Wrox Author
Points: 3,141, Level: 23
Points: 3,141, Level: 23 Points: 3,141, Level: 23 Points: 3,141, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 640
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Well that code doesn't have any sort of looping code in it so it would only run once.

Is the code inside an event handler or something? If so, then it may be a problem with the RFID reader or library or something, but I doubt it's the C# code.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
  #10 (permalink)  
Old November 20th, 2015, 06:16 PM
Registered User
Points: 9, Level: 1
Points: 9, Level: 1 Points: 9, Level: 1 Points: 9, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2015
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

what should i do . hic
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
text box empty problem MunishBhatia ASP.NET 2.0 Professional 2 October 10th, 2007 07:39 AM
Checking to see if a label's text is empty mh JSP Basics 2 October 26th, 2005 11:05 AM
Checking to see if a label's text is empty mh ASP.NET 2.0 Basics 2 October 26th, 2005 10:38 AM
Don;t diplay text box if it is empty or null abdul_wasie Classic ASP Databases 5 May 20th, 2005 11:49 PM
How to insert default value if text box is empty akc Javascript How-To 2 November 7th, 2003 10:55 AM



All times are GMT -4. The time now is 12:12 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.