Basically, Im making a paint application very similar to MSPaint.
The idea is that, that the the user clicks anywhere on the form and should be able to write text in a control. And then following that, that text should be displayed in g.drawstring graphic method.
Basically, Im making a paint application very similar to MSPaint. The idea is that,
Share
I don’t want to do the whole thing for you, but here is a basic outline of one way to accomplish the goals you outline. This is not necessarily the best way, but it should get you started and will introduce you to a number of WinForms concepts.
Writing the text
Create a
Formand add aTextBoxcontrol to it. Make sure it is hidden by default. Override theOnMouseClickmethod of yourFormand add code that checks if theTextBoxis visible and if not, shows it and puts focus to it for the user to enter their text. If theTextBoxis already visible, the code should hide it and create a newUserControlin its place that shows the text (see below for details of thatUserControl).Also add an event handler to the
TextBoxso that if the user hits Esc, it cancels the edit and if they hit Enter, the text is accepted and theUserControlis created.Displaying the text
Create a
UserControland make sure that theUserPaintandOpaquestyles are set in its construction (seeSetStyle– you may also want to considerOptimizedDoubleBufferandAllPaintingInWmPaintas this can reduce flickering though it does require extra paint code).Override the
OnPaintmethod in yourUserControland implement the code for drawing the string (remember, you’ll also need a way to set the text on the control).Conclusion
If you hook all that up, you should have something that appears to meet your requirements. For further experimentation, consider how you could remove the need for the
UserControl. Good luck and have fun!