Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

c# - Need help creating control to display data

I am writing a .NET WinForms application that needs to display a list of results in a nice formatted user-friendly fashion. Here's how I'm hoping to display the data.

alt text http://img398.imageshack.us/img398/4336/imgdyu.png

The table must support a decent amount of data(>= 200 individual "data blocks"), and needs to be fairly fast. I'm unsure of the easiest way to accomplish this and I was hoping for some direction and advice. I created a quick prototype custom control that simply used a bunch a text boxes stacked on top of each other. It worked fairly well but Windows runs out of handles for the textboxes too quickly. I could create a custom textbox control, but it would be time consuming and I was wondering if there are any recommendations for alternative solutions? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could use a third-party control for this (maybe), but here are some reasons why this is potentially a bad idea:

  1. third-party controls cost money
  2. third-party controls tend to suck
  3. third-party controls require you to distribute at least an additional DLL, over which you usually have no control
  4. even if the 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 hammer it into the shape you require

For anything out of the ordinary (like what you're doing), writing your own UserControl is the best way to go, for these (among others) reasons:

  1. writing your own UserControl is far and away the most fun thing you can do in .Net
  2. you will have to learn some things to do this well, but the knowledge you gain is immediately transferable to other problems and projects (whereas whatever you learn about using Initrode's Infinite Wonderfulness Gridifier will only ever help you with using that particular control)
  3. there will be nothing additional to distribute with your application
  4. complete control over the source code
  5. free forever (with apologies to Evony)
  6. the sky is the limit - with any third-party control, you will ultimately run into something that it just can't do, but if you're doing it yourself, you can literally do anything you want

Your particular problem (well described in your question, thanks to the graphics) is quite easy to do as a mostly owner-drawn UserControl (with a TextBox or two thrown in the mix). The only methods you'll need from the System.Drawing.Graphics object are DrawRectangle, FillRectangle, MeasureString and DrawString. You won't even need any documentation, as Intellisense will give you everything you need.

If you run into trouble, I'll write it for you for a chocolate chip cookie. :)

Update: since you need the text to all be selectable, that makes this approach a bit more complicated. Implementing your own Textbox-type functionality is a gigantic pain in the petard, but a relatively simple solution is to add a real multi-line Textbox over top of any text-containing rectangle when the user clicks on it, and to put the rectangle's text (pre-selected) into the Textbox. When this temporary Textbox loses focus (LostFocus), you draw the edited text into the rectangle and delete the Textbox. This way you only have one Textbox at a time in your UserControl.

This version will cost you two cookies.

Update 2: Here is a simple application that demonstrates how to use a single TextBox to make your entire control selectable and editable. And here is the source code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...