Solved

How to validate textbox in Visual C++?

Posted on 2004-10-18
5
655 Views
Last Modified: 2010-04-24
Can anyone please tell me how to go about validating textboxes in visual C++? I notice there's an "addValidating" event that we can call, but how should I use it? I basically want to make sure that the user does not leave a textbox blank, if they do I want to highlight that textbox a different color and tell them to fill it in...
0
Comment
Question by:jade03
  • 3
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
Svetlin_Panayotov earned 50 total points
Comment Utility
You can have some code like this:
in your InitializeComponent() method:
this->textBox1->Validating += new System::ComponentModel::CancelEventHandler(this, textBox1_Validating);

and add this method:

private: System::Void textBox1_Validating(System::Object *  sender, System::ComponentModel::CancelEventArgs *  e)
             {
                   System::Boolean isValid = false;
                   //Do your validation here...
                   if (isValid == false)
                   {
                         textBox1->set_BackColor(System::Drawing::Color::Red);
                         e->set_Cancel(true);
                   }
                   else
                   {
                         textBox1->set_BackColor(System::Drawing::SystemColors::Window);
                   }
             }

Svetlin
0
 

Author Comment

by:jade03
Comment Utility
Svetlin,

I tried that...for one thing, visual c++ doesn't seem to have the "Validating" method...it has an add_validating method instead....when I use the add_validating method, it complains of the way I call textBox1_Validating (exactly like how you called it above, with no params). It says since textBox1_Validating is defined to take 2 params, I couldn't call it w/ no args...
0
 
LVL 6

Expert Comment

by:Svetlin_Panayotov
Comment Utility
The code I posted works fine for me - but with VS 2003.Can't test it on 2002 right now.
What version of VS are you using?
0
 

Author Comment

by:jade03
Comment Utility
Svetlin,

Thanx! my version is also 2003. I guess when I typed in this->textBox1-> and didn't see the "Validating" method in the list I thought it wasnt' available, so I assumed it's the "add_Validating" method instead...but when I tried calling "Validating" anyways the way you did it, it compiles and is good.

Thanx again! :)

jade
0
 
LVL 6

Expert Comment

by:Svetlin_Panayotov
Comment Utility
It happens :)
Good luck with your project and happy coding :)

Svetlin
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now