This is reaching way back in the memory vault. Legacy application, VB6, standard TextBox control on a Form.
When you start typing text, it is right against the left edge. I want to set a left margin so that the first character appears inset by some amount.
There are a dozen examples online, all the same, but none of which change the left margin for me. What could I possibly be doing wrong?
Here is the standard example and the one I am using:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const EM_SETMARGINS = &HD3
Private Const EC_LEFTMARGIN = &H1
Private Const EC_RIGHTMARGIN = &H2
' Set the TextBox's margins.
Private Sub SetMargin(nLeft As Integer, nRight As Integer, lhWnd As Long)
Dim lLongValue As Long
'nRight needs to be in the hi-word, so we multiply by 65536
lLongValue = (&H10000 * nRight) + nLeft
SendMessage lhWnd, EM_SETMARGINS, EC_LEFTMARGIN Or EC_RIGHTMARGIN, lLongValue
End Sub
This absolutely does not change the left margin of the text in my TextBox. I have tried values of 10, 100, 1000, and 10000 with no changes. The TextBox Alignment property is set to LeftJustify. I have tried Multiline set to True or False. (One example indicated it must be set to True) I have tried calling SetMargin() in Form_Load and other locations.
I must be missing something silly. But have looked and spent a couple hours Googl'ng on this and can't seem to find the missing piece. Any ideas from anyone?
Adding my test situation. An empty Form with only one TextBox in it and the following code:
Option Explicit
Private Sub Form_Load()
SetMargin 100, 100, txtMargin.hwnd
End Sub
I don't know why, but I figured out a scenario that allows this to work on my legacy VB6 project.
Prior to this, when it wouldn't work, I had Declared the SendMessage function in a standard VB module (.bas). And I put the Public SetMargin() method in that module as well. Then called it from my Form. When I did this, it didn't work.
Now, I put the declaration, defined the constants, and the SetMargin() method in my Form's local code. And voila! It works.
Below is a pic of it with the left margin set to 30. And you can see why I wanted this. I wanted to overlay a Picture control on the TextBox, and have the typing start to the right of that.