Text Box Question

  • Thread starter Thread starter Patrick De Ridder
  • Start date Start date
P

Patrick De Ridder

Re displaying text in a multi-line text box:

Is it possible to have a margin on the left hand side, or on both
sides, so the text doesn't come right up to the border(s)?

Many thanks.
 
Yes it is. This should do it for you:

[DllImport("User32.dll", EntryPoint = "SendMessage", CharSet =
CharSet.Auto)]
private static extern int SendMessageRefRect(IntPtr hWnd, UInt32 Msg, int
wParam, ref RECT rect);

private const int EM_SETRECT = 0xB3;

public static void SetTextBoxFormatingRectangle(TextBoxBase textbox,
Rectangle rect)
{
RECT rc = new RECT(rect);
SendMessageRefRect(textbox.Handle, EM_SETRECT, 0, ref rc);
}

You could also use the EM_SETMARGINS message, but that doesn't give you
control over the top and bottom.

Tom Clement
Apptero, Inc.
 
Tom Clement said:
Yes it is. This should do it for you:

[DllImport("User32.dll", EntryPoint = "SendMessage", CharSet =
CharSet.Auto)]
private static extern int SendMessageRefRect(IntPtr hWnd, UInt32 Msg, int
wParam, ref RECT rect);

private const int EM_SETRECT = 0xB3;

public static void SetTextBoxFormatingRectangle(TextBoxBase textbox,
Rectangle rect)
{
RECT rc = new RECT(rect);
SendMessageRefRect(textbox.Handle, EM_SETRECT, 0, ref rc);
}

You could also use the EM_SETMARGINS message, but that doesn't give you
control over the top and bottom.

Tom Clement
Apptero, Inc.

Thanks a lot. I am impressed!

I have pasted in your code above #region Windows Form Designer generated
code
and I get this message upon compilation:
The type or namespace name 'RECT' could not be found (are you missing a
using directive or an assembly reference?)
 
Darn it, I forgot to include that definition. Here is it...

public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;

public RECT(Rectangle rc) { Left = rc.Left; Top = rc.Top; Right =
rc.Right; Bottom = rc.Bottom; }
public RECT(int l, int t, int r, int b) { Left = l; Top = t; Right = r;
Bottom = b; }
public override string ToString() { return String.Format("RECT
({0},{1})-({2},{3})", Left, Top, Right, Bottom); }
}

Tom Clement
Apptero, Inc.

Patrick De Ridder said:
Tom Clement said:
Yes it is. This should do it for you:

[DllImport("User32.dll", EntryPoint = "SendMessage", CharSet =
CharSet.Auto)]
private static extern int SendMessageRefRect(IntPtr hWnd, UInt32 Msg, int
wParam, ref RECT rect);

private const int EM_SETRECT = 0xB3;

public static void SetTextBoxFormatingRectangle(TextBoxBase textbox,
Rectangle rect)
{
RECT rc = new RECT(rect);
SendMessageRefRect(textbox.Handle, EM_SETRECT, 0, ref rc);
}

You could also use the EM_SETMARGINS message, but that doesn't give you
control over the top and bottom.

Tom Clement
Apptero, Inc.

Thanks a lot. I am impressed!

I have pasted in your code above #region Windows Form Designer generated
code
and I get this message upon compilation:
The type or namespace name 'RECT' could not be found (are you missing a
using directive or an assembly reference?)
 
Tom Clement said:
Darn it, I forgot to include that definition. Here is it...

public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;

public RECT(Rectangle rc) { Left = rc.Left; Top = rc.Top; Right =
rc.Right; Bottom = rc.Bottom; }
public RECT(int l, int t, int r, int b) { Left = l; Top = t; Right = r;
Bottom = b; }
public override string ToString() { return String.Format("RECT
({0},{1})-({2},{3})", Left, Top, Right, Bottom); }
}
Thanks you for the addition!
There seems to be one last hurdle
The type or namespace name 'DllImport' could not be found (are you missing a
using directive or an assembly reference?)
 
Thanks you for the addition!
There seems to be one last hurdle
The type or namespace name 'DllImport' could not be found (are you missing a
using directive or an assembly reference?)

It would appear that this is the required reference
using System.Runtime.InteropServices;
I am left with the question how to apply it.
If anyone knows how, I would be very obliged with an example.
 
It would appear that this is the required reference:

using System.Runtime.InteropServices;

**************

Presumably this should do the trick:
SetTextBoxFormatingRectangle(textBox1, 10)

It doesn't work.

The second parameter generates problems.

Any suggestions?

If this isn't the way to implement the code, what is the right way?
 
Darn it, I forgot to include that definition. Here is it...

public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;

public RECT(Rectangle rc) { Left = rc.Left; Top = rc.Top; Right =
rc.Right; Bottom = rc.Bottom; }
public RECT(int l, int t, int r, int b) { Left = l; Top = t; Right = r;
Bottom = b; }
public override string ToString() { return String.Format("RECT
({0},{1})-({2},{3})", Left, Top, Right, Bottom); }
}

Tom Clement
Apptero, Inc.

(I have solved the reference problem.)

Presumably this is how your code should be invoked:
SetTextBoxFormatingRectangle(textBox1,10);

But it doesn't work. The second parameter gives problems.
 
Hi Patrick,
In the code I provided, the second parameter is a Rectangle. That needs to
be the case, since the call provides for offsets on the Top, Right, Bottom
and Left. Here's how I call the method:

API.SetTextBoxFormatingRectangle(txtBody, new Rectangle(10,16,
txtBody.ClientSize.Width - 12, txtBody.ClientSize.Height - 20));

But to be honest with you, Patrick, this is the sort of thing you should be
able to figure out on your own. I'd recommend that you take a course at a
community college if something is available, or buy a good book on
programming. Once you get going, things turn out to be pretty
straightforward in C#, but it can be a bit of a hurdle initially.

Tom
 
But to be honest with you, Patrick, this is the sort of thing you should be
able to figure out on your own. I'd recommend that you take a course at a
community college if something is available, or buy a good book on
programming. Once you get going, things turn out to be pretty
straightforward in C#, but it can be a bit of a hurdle initially.

First of all thank you. Secondly, sure I still have a hell of a lot to
learn. However, I have been reading and will go on doing so.
 
Back
Top