Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 755
  • Last Modified:

How to validate textbox in Visual C++?

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
jade03
Asked:
jade03
  • 3
  • 2
1 Solution
 
Svetlin_PanayotovCommented:
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
 
jade03Author Commented:
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
 
Svetlin_PanayotovCommented:
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
 
jade03Author Commented:
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
 
Svetlin_PanayotovCommented:
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.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now