You can use a third-party control for this (maybe), but here are a few reasons why this is a potentially bad idea:
- third-party controls spend money
- third-party controls tend to suck
- third-party controls require you to distribute at least an additional DLL that you usually don't have control over
- even if a third-party control can ultimately do what you need, it will take you some time and effort to figure out how to use it and how to drive it into the form you need.
For something unusual (like what you are doing), writing your own UserControl is the best way for these reasons (among others):
- Writing your own UserControl is the most interesting thing you can do in .Net
- you need to learn some things to do it well, but the knowledge gained is immediately transferred to other problems and projects (whereas everything you learn about using Initrode Infinite Wonderfulness Gridifier will only help you in using this particular control)
- there will be nothing extra to distribute with your application
- complete source control
- free forever (with an apology to Avoni)
- the sky is the limit - with any third-party control, you will end up with something that it simply cannot do, but if you do it yourself, you can literally do whatever you want.
Your specific problem (well described in your question, thanks to the graphics) is quite easy to do, as a mostly user-defined UserControl (with a TextBox or two thrown in the mix). The only methods you will need from the System.Drawing.Graphics object are DrawRectangle, FillRectangle, MeasureString and DrawString. You don’t even need documentation, as Intellisense will provide you with everything you need.
If you have any problems, I will write you this for cookies with a chocolate chip. :)
Update : since you need the text to be selectable, this makes this approach more complicated. Implementing your own functionality like Textbox is a huge pain in firecrackers, but a relatively simple solution is to add a real multi-line text block on top of any text rectangle when the user clicks on it, and place the rectangle text (previously selected) in the text box. When this temporary text field loses focus (LostFocus), you draw the edited text into a rectangle and delete the text field. This way, you only have one text block at a time in your UserControl.
This version will cost you two cookies.
Update 2 . Below is a simple application that shows how to use one text block to create the whole control is selected and edited. And here is the source code .
source share