So, I am creating a Windows Forms Application in Visual C++ 2010, and I want to add an event to a text box. When the program loads, a letter A is printed onto the screen. When you enter the text box, the letter is supposed to turn red.
The name of the textbox is AngleA, and this is the code I have so far:
this->AngleA->Enter += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::AngleA_Enter);
//many lines later
this->Controls->Add(this->AngleA);
//many lines later
public: System::Void Form1::AngleA_Enter(System::Object^ sender, PaintEventArgs^ e)
{
System::Drawing::Font^ textFontA = gcnew System::Drawing::Font("Arial", 16);
System::Drawing::SolidBrush^ textBrushA = gcnew System::Drawing::SolidBrush(Color::Red);
e->Graphics->DrawString("A", textFontA, 开发者_如何学运维textBrushA, 300, 120);
}
The original drawing of the letter happens in a separate function, here:
public: virtual Void Form1::OnPaint(PaintEventArgs^ pe ) override
{
Graphics^ g = pe->Graphics;
System::Drawing::Font^ textFont = gcnew System::Drawing::Font("Times New Roman", 16);
SolidBrush^ textBrushA = gcnew SolidBrush(Color::Black);
g->DrawString("A", textFont, textBrushA, 300, 120);
}
So, the drawing of the original letter works great, but every time I try to build the program with the Enter event, I get the following error:
error C2664: 'System::Windows::Forms::Control::Enter::add' : cannot convert parameter 1 from 'System::Windows::Forms::PaintEventHandler ^' to 'System::EventHandler ^'
1> No user-defined-conversion operator available, or
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
It seems to me that the form1 object (default name for class in windows forms apps) will only accept an EventHandler parameter for the "this->AngleA->Enter += gcnew " and not PaintEventHandler, but I dont understand why. Is there any way to create an Enter event function that will allow me to paint after the program has already loaded, based on an event?
Thanks for the help, I hope I was clear in my question :)
You can only add a PaintEventHandler
to the Paint
event; not to the Enter
event.
You probably want to add a normal EventHandler
to the Enter
event and call Invalidate()
in the handler.
精彩评论