views:

52

answers:

5

I want a form containing:

+----------------------------------------+
| Dialog Title                          X|
+----------------------------------------+
+----------------------------------------+
|icon   |                                |
|32x32px| One-line label (Heading)       |
|       +--------------------------------+
|       |                                |
|       | Message label with auto-wrap   |
|       | text according to any given    |
|       | string.                        |
+-------+--------------------------------+
| row for dialog buttons...              |
+----------------------------------------+

I'll gladly answer any questions; the basic idea is still simple (though I cannot get it to work): Given any message string containing possible newlines the dialog (a Form) should keep its width but grow vertically depending on the message.

Any way how this can be done?

A: 

Create your own new Form and show it as a dialog box. You can put whatever/however you want on that form.

Here you have a tutorial that will show you how to do the hardest part.

thelost
+1  A: 

I think the component that you will find most useful is TableLayoutPanel. Find it under “Containers” in the Toolbox. Set the TableLayoutPanel’s Dock = Fill.

You can use it to lay out the controls in columns and rows. Once a control is inside the TableLayoutPanel, you can use the ColumnSpan property on such a control to span it across multiple columns; I’d use this for the button row at the bottom, i.e. make a new panel for the button row and put the buttons inside that. For the icon, of course, use RowSpan instead.

Experiment with various values of Anchor, AutoSize and AutoSizeMode for some of the controls, especially the message label that you want to grow automatically. If you set the TableLayoutPanel and the Form to AutoSize = true, then the window will grow automatically with the text contents.

Timwi
A: 

Try a TableLayoutPanel for the layout and set its Dock property to Fill to occupy the entire Form. Then plop your "one-line" and "message" labels into their respective cells and set their Dock properties to Fill to occupy the entire cell.

If you really want to resize the entire Form to fit any message at runtime, you may have to use Graphics.MeasureString to determine the area you need to contain the string and then resize the form to contain that area.

flipdoubt
A: 

You might try to ask for the position of the last character

TextBox box = new TextBox();
box.Text = "...";
var positionOfLastCharacter = box.GetPositionFromCharIndex(box.TextLength);

The you can calculate the necessary height of the textbox and the form.

Edit: That will give you the top left corner of the last character, you should add 10px or so to make the last line fit.

Albin Sunnanbo
May I have an explanation of the downvote?
Albin Sunnanbo
+1  A: 

You could try handling the TextChanged event of the label and measure the size of the string using something like this:

Graphics g = Graphics.FromHwnd(this.Handle);
SizeF s = g.MeasureString(yourLabel.Text, yourLabel.Font, yourLabel.Width);

After this, knowing the sizes of the other controls you can modify the size of the window accordingly. I am assuming that you only want to resize the window vertically.

devnull
Rather than use `Graphics.MeasureString`, use `TextRenderer.MeasureText()` - Text renderer provides a more accurate result (and no need to create a Graphics object yourself too).
Pondidum