Solved

How to validate textbox in Visual C++?

Posted on 2004-10-18
5
677 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 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