AutoSize TextBox in Compact Framework

M

Manfred Denzer

Hello,

unfortunately, the property "AutoSize" doesn't work in Compact
Framework. So I have to set the size of my multiline TextBox manually.
I search a lot and found some solutions on the web. Often, I read the
hint to calculate the size of the text with Graphics.MeasureString:
http://www.eggheadcafe.com/software/aspnet/30758828/label-control-no-autosize.aspx

Now, I use this methode and calculate the lines:
SizeF textsize= g.MeasureString(control.Text, control.Font);
int lines = (int)textsize.Width / control.Width;
lines += (textsize.Width % control.Width) != 0 ? 1 : 0;
control.Height = lines * (int)textsize.Height;

But I'm sure that this solutions doesn't work in all cases! If there
is a long word in the TextBox and it doesn't fit completly in a line,
it will be displayed in the next line (like
JTextArea.setWrapStyleWord(true) in Java). So, in some cases it could
happen that the TextBox is to short.

What can I do the get the really number of lines of a TextBox?
Or is the a complete other solution for my problem? Maybe add the
TextBox in a Panel and use a LayoutManager like in Java? I'm new
in .NET CF and don't know if there are LayoutManagers.

Thank you very much!
 
B

Bjørn Brox

Manfred Denzer skrev:
Hello,

unfortunately, the property "AutoSize" doesn't work in Compact
Framework. So I have to set the size of my multiline TextBox manually.
I search a lot and found some solutions on the web. Often, I read the
hint to calculate the size of the text with Graphics.MeasureString:
http://www.eggheadcafe.com/software/aspnet/30758828/label-control-no-autosize.aspx

Now, I use this methode and calculate the lines:
SizeF textsize= g.MeasureString(control.Text, control.Font);
int lines = (int)textsize.Width / control.Width;
lines += (textsize.Width % control.Width) != 0 ? 1 : 0;
control.Height = lines * (int)textsize.Height;

But I'm sure that this solutions doesn't work in all cases! If there
is a long word in the TextBox and it doesn't fit completly in a line,
it will be displayed in the next line (like
JTextArea.setWrapStyleWord(true) in Java). So, in some cases it could
happen that the TextBox is to short.

What can I do the get the really number of lines of a TextBox?
Or is the a complete other solution for my problem? Maybe add the
TextBox in a Panel and use a LayoutManager like in Java? I'm new
in .NET CF and don't know if there are LayoutManagers.

Thank you very much!

Setting the size of a TextBox is also dependent on the surroundings, i.e
other elements and the size of the parent control.

Instead of trying to manually set the size you should consider using the
Anchor (or Dock) attribute and let windows automatically resize the textbox.

Remember that most devices can shift between landscape and portrait view
with a single key press, so you should avoid hard-coding anything.

It is better to fine tune elements position and size with the Anchor
attribute.
 
S

Simon Hart [MVP]

There are no layout managers under the CF. You have to lay stuff out yourself.

Why are you trying to set the height of a multi-line text box? why don't you
set it to a predefined size, then set the vertical scroll bar to true.

The complexity of trying to calculate the height on a framework control
based in lines in this case is rather ambitious as you have issues such as
line spacing etc.
 
M

Manfred Denzer

Bjørn, thank you very much for your answer :)

I tried using Anchor and Dock, but havn't success. On my Form are 2
TextBoxs among each other. If there is a lot of text in the TextBox,
the height of the TextBox should increase automatically.

With Anchor, I can define edges of the container to which a control is
bound. But I have to set the Size and Location still. Or?

Dock reminds me of a Java BorderLayout. I give both TextBoxs
DockStyle.Top, so the Location of the 2nd TextBox depends on the size
of the first one. But the size of the first one does not set
automatically on the basis of the text inside. I have still set the
size of the TextBox.

Please tell me if my usage of Anchor and Dock was incorrect.

Best regards
Manfred
 
M

Manfred Denzer

Simon, thank you very much for your answer, too :)

Yes, a predefined size with vertical scollbars would be a solution.
But in my special case, it is essential that you can see the whole
text on the display without scrolling.

I tried to improve my algorithm, now it understands word-wrap (\r\n):

String[] splits = text.Split(new char[] { '\r', '\n' });
int lines = 0;
foreach (String str in splits) {
SizeF size = g.MeasureString(str, contr.Font);
lines += (int)size.Width / (contr.Width - 5);
lines += (size.Width % contr.Width) != 0 ? 1 : 0;
}
contr.Height = lines * LINE_HEIGHT;


I would be glad, if you can help me furthermore.

Best regards
Manfred
 
M

Manfred Denzer

Hello,

if somebody has an idea for me, I would be glad :)

Thank you very much
Manfred
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top