Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to validate textbox in Visual C++?

Posted on 2004-10-18
5
Medium Priority
?
724 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
Svetlin_Panayotov earned 200 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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 course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

670 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