textbox and size.

  • Thread starter Thread starter Per W.
  • Start date Start date
P

Per W.

Is it possible to have dynamic size on a textbox? If the context is only one
line then the box is only one line, but if the context is 10 lines then the
box adjust to this?

/Per W.
 
Kevin Spencer said:
Set the Multiline property to true.

i have done that, but i want the height of the box to adjust to the number
of lines in the box, i dont want to use scroll bars. Maybe i can count the
number of lines and set the height after how many lines there is.

/Per W.
 
i have done that, but i want the height of the box to adjust to the number
of lines in the box, i dont want to use scroll bars. Maybe i can count the
number of lines and set the height after how many lines there is.

/Per W.

Well, the textbox is actually a native component, so the only way I can
think of is to use the SendMessageLong API call with something like
EM_GETLINECOUNT, and use that to calculate the height you need the control
to be.
A bit messy, but while Win32API is being used for control rendering we have
very little choice in the matter.

Cheers,
Gadget
 
You can use the System.Drawing.Graphics.MeasureString() method to measure
the rectangle that the text will occupy. The method takes a Font as an
argument, and an overload that takes a SizeF, so that, for example, if you
want to limit the width, you can pass a SizeFto the method. See:

http://msdn2.microsoft.com/en-us/library/system.drawing.graphics.measurestring.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Ministry of Software Development
http://unclechutney.blogspot.com

Any experience you can walk away from
is a good one.
 
As I just posted (later than yours), I have successfully used the
System.Drawing.Graphics.MeasureString method to to this. Of course, using
the Win32 API is always available, and I use it occasionally for tasks that
aren't available through Managed code.

--
HTH,

Kevin Spencer
Microsoft MVP
Ministry of Software Development
http://unclechutney.blogspot.com

Any experience you can walk away from
is a good one.
 
Gadget said:
Well, the textbox is actually a native component, so the only way I can
think of is to use the SendMessageLong API call with something like
EM_GETLINECOUNT, and use that to calculate the height you need the control
to be.
A bit messy, but while Win32API is being used for control rendering we
have
very little choice in the matter.

Thanx, but do you now about som step by step to do this? All i can find is
only working on VB 6.0 and earlier.

/Per W.
 
As I just posted (later than yours), I have successfully used the
System.Drawing.Graphics.MeasureString method to to this. Of course, using
the Win32 API is always available, and I use it occasionally for tasks that
aren't available through Managed code.

I'd be very intersted to see this working. I know that I tried to use this
some time ago for the same requirements as Per, but I found the
MeasureString results never seemed to match what the textbox rendered, and
I had to add cludges to multiply heights by 1.05, etc, and still often
ended up with the bottom line missing (or a blank line appended).
It would be a very useful feature to have available on a listbox, so
perhaps someone should knock up a custom control? :)

Cheers,
Gadget
 
Hi Gadget,

The following example code is abbreviated from some code I use to build a
table grid. I removed the extraneous looping through rows, etc, and left in
the basic measuring technique:

// 96 bpi bitmap (Same dpi as screen) used to get a System.Drawing.Graphics
instance for measuring.
private Bitmap GraphicsHelper;

protected void GetGraphicsHelper()
{
GraphicsHelper =
(Bitmap)global::DsiGlobal.FormControls.Properties.Resources.GraphicsHelper;
}


public void LoadTables()
{
int cellHeight = 0; // Row Height variable
int i;
SizeF textSize = new SizeF();
SizeF cellSize = new SizeF();
string cellValue;

// System.Drawing.StringFormat deals with String Formatting rules when drawn
StringFormat format = new StringFormat();
format.FormatFlags = StringFormatFlags.LineLimit;
format.Trimming = StringTrimming.EllipsisWord;
using (Graphics graphics = Graphics.FromImage(GraphicsHelper))
{
// Uses the column width of a column, and a Maximum column height
cellSize = new SizeF(TableManager.Columns.Width, (float)maxRowHeight);
// the text to be displayed
cellValue = Convert.ToString(row);
// returns a SizeF only large enough to hold the text, constrained by
width
textSize = graphics.MeasureString(cellValue, this.Font.GetFont(),
cellSize, format);
}
}

See http://msdn2.microsoft.com/en-gb/library/957webty.aspx for more
information about this overload of the System.Drawing.Graphics.MeasureString
method. The size passed represents the maximum size. The height will be
truncated to fit the text.

--
HTH,

Kevin Spencer
Microsoft MVP
Ministry of Software Development
http://unclechutney.blogspot.com

Any experience you can walk away from
is a good one.
 
// 96 bpi bitmap (Same dpi as screen) used to get a System.Drawing.Graphics
instance for measuring.
private Bitmap GraphicsHelper;

protected void GetGraphicsHelper()
{
GraphicsHelper =
(Bitmap)global::DsiGlobal.FormControls.Properties.Resources.GraphicsHelper;
}


public void LoadTables()
{
int cellHeight = 0; // Row Height variable
int i;
SizeF textSize = new SizeF();
SizeF cellSize = new SizeF();
string cellValue;

// System.Drawing.StringFormat deals with String Formatting rules when drawn
StringFormat format = new StringFormat();
format.FormatFlags = StringFormatFlags.LineLimit;
format.Trimming = StringTrimming.EllipsisWord;
using (Graphics graphics = Graphics.FromImage(GraphicsHelper))
{
// Uses the column width of a column, and a Maximum column height
cellSize = new SizeF(TableManager.Columns.Width, (float)maxRowHeight);
// the text to be displayed
cellValue = Convert.ToString(row);
// returns a SizeF only large enough to hold the text, constrained by
width
textSize = graphics.MeasureString(cellValue, this.Font.GetFont(),
cellSize, format);
}
}


Thanks Kevin, It'll be interesting to have an experiment with this,
although I'm not convinced this will work as well with a TextBox as they
have padding margins that seem to be designed to screw-up any auto-sizing
code :)

I think most grid controls, particularly third party ones, avoid using the
native textbox components in any way and draw directly to a graphics
context, avoiding all these issues.

Of course, now Per reveals he actually wants this for a web page, the whole
discussion rather loses its reason for existence :)

Cheers,
Gadget
 
If you were still working with a TextBox, you would use the ClientSize
rectangle of the TextBox to get the width, and include some padding around
the Text in your calculations.

--
HTH,

Kevin Spencer
Microsoft MVP
Ministry of Software Development
http://unclechutney.blogspot.com

Never trust a dunderhead with a blunderbuss.


Gadget said:
// 96 bpi bitmap (Same dpi as screen) used to get a
System.Drawing.Graphics
instance for measuring.
private Bitmap GraphicsHelper;

protected void GetGraphicsHelper()
{
GraphicsHelper =
(Bitmap)global::DsiGlobal.FormControls.Properties.Resources.GraphicsHelper;
}


public void LoadTables()
{
int cellHeight = 0; // Row Height variable
int i;
SizeF textSize = new SizeF();
SizeF cellSize = new SizeF();
string cellValue;

// System.Drawing.StringFormat deals with String Formatting rules when
drawn
StringFormat format = new StringFormat();
format.FormatFlags = StringFormatFlags.LineLimit;
format.Trimming = StringTrimming.EllipsisWord;
using (Graphics graphics = Graphics.FromImage(GraphicsHelper))
{
// Uses the column width of a column, and a Maximum column height
cellSize = new SizeF(TableManager.Columns.Width,
(float)maxRowHeight);
// the text to be displayed
cellValue = Convert.ToString(row);
// returns a SizeF only large enough to hold the text, constrained by
width
textSize = graphics.MeasureString(cellValue, this.Font.GetFont(),
cellSize, format);
}
}


Thanks Kevin, It'll be interesting to have an experiment with this,
although I'm not convinced this will work as well with a TextBox as they
have padding margins that seem to be designed to screw-up any auto-sizing
code :)

I think most grid controls, particularly third party ones, avoid using the
native textbox components in any way and draw directly to a graphics
context, avoiding all these issues.

Of course, now Per reveals he actually wants this for a web page, the
whole
discussion rather loses its reason for existence :)

Cheers,
Gadget
 
Back
Top