Solved

How to validate textbox in Visual C++?

Posted on 2004-10-18
5
663 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
ID: 12344851
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
ID: 12348491
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
ID: 12349127
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
ID: 12350034
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
ID: 12350334
It happens :)
Good luck with your project and happy coding :)

Svetlin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
A short film showing how OnPage and Connectwise integration works.

937 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

11 Experts available now in Live!

Get 1:1 Help Now