Connecting Tech Pros Worldwide Help | Site Map

C#-App: Textbox scroll bar showing double byte characters

Member
 
Join Date: Oct 2008
Posts: 47
#1: May 28 '09
Hi everyone,

I have a textbox which is multiline and wordwrap is true.
I did not set any scrollbar because I want to show it manually (using different buttons)
The textbox will sometimes have double byte strings (chinese characters)

Question:
1) How can I know if I need to show the scrollbar base on the text inside the textbox?

Options:
1) is there a way to manually predict the number of characters that my textbox can show within its size? Getting the length of the string just show the number of characters in the string, not the actual space it can occupy inside the textbox


TIA

dantz
Newbie
 
Join Date: Jun 2009
Posts: 2
#2: Jun 29 '09

re: C#-App: Textbox scroll bar showing double byte characters


Quote:

Originally Posted by dantz View Post

Hi everyone,

I have a textbox which is multiline and wordwrap is true.
I did not set any scrollbar because I want to show it manually (using different buttons)
The textbox will sometimes have double byte strings (chinese characters)

Question:
1) How can I know if I need to show the scrollbar base on the text inside the textbox?

Options:
1) is there a way to manually predict the number of characters that my textbox can show within its size? Getting the length of the string just show the number of characters in the string, not the actual space it can occupy inside the textbox


TIA

dantz

Hi dantz!

This might be a moot point - not sure I understood your situation correctly. But at least with non-chinese (or other multi-byte) characters this should work....

You can measure the amount of pixels it takes to draw a string by using the MeasureString-method from the System.Drawing.Graphics - object.

Expand|Select|Wrap|Line Numbers
  1. SizeF size = g.MeasureString("string to measure here", this.Font);
You will want to use the Font from your Textbox object. Now you only need to get a hold of Graphics object. One way is to override the OnPaint method like t his:
Expand|Select|Wrap|Line Numbers
  1. protected override void OnPaint(PaintEventArgs e)
  2. {
  3. // e.Graphics has reference to Graphics used
  4. }
Or if that wont do, you can do it once in your form's OnLoad like this:
Expand|Select|Wrap|Line Numbers
  1. System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd(this.Handle);
That should get you started solving your problem.

You will have some troubles with "borders" of the text box. I mean that the size of the textbox object is likely to be different from the area where you can actually draw strings...

HTH,
salmenmikko
Reply