Setting the text area of a textbox

  • Thread starter Thread starter Aaron Smith
  • Start date Start date
A

Aaron Smith

I am creating a subclassed text box. I want to have a button inside the
tetbox. That was the easy part. The part I can't figure out, is how you
set the area for the text? If you type a string it will go underneath
the button instead of stopping at the button and doing it's normal wrap
functionality where it pushes the text off the one side of the
textbox... How do you tell it to only use a certain area for text?

Aaron
 
Aaron Smith said:
I am creating a subclassed text box. I want to have a button inside the
tetbox. That was the easy part. The part I can't figure out, is how you set
the area for the text? If you type a string it will go underneath the
button instead of stopping at the button and doing it's normal wrap
functionality where it pushes the text off the one side of the textbox...
How do you tell it to only use a certain area for text?

You can use p/invoke to set a horizontal indentation. A VB6 sample can be
found here:

SetMargin
<URL:http://dotnet.mvps.org/vb/samples/controls/SetMargin.zip>

If you don't want a border around the whole control, you can create a
usercontrol that contains a textbox and the button control.
 
Herfried said:
You can use p/invoke to set a horizontal indentation. A VB6 sample can
be found here:

SetMargin
<URL:http://dotnet.mvps.org/vb/samples/controls/SetMargin.zip>

If you don't want a border around the whole control, you can create a
usercontrol that contains a textbox and the button control.

I'll have to see if I can get that to work in VB.Net ... I do want a
border around the whole thing. I actually want the button to be part of
the text box, not outside of it...

Here is what I have:

Public Class PromptTextBox
Inherits System.Windows.Forms.TextBox
Dim Prompt As New PromptButton

Public Class PromptButton
Inherits System.Windows.Forms.Button

Public Sub New()
Dim sz As System.Drawing.Size
Me.Text = "..."
Me.TextAlign = ContentAlignment.BottomCenter
sz.Height = 17
sz.Width = 18
Me.Size() = sz
Me.FlatStyle = FlatStyle.System
Me.BackColor =
System.Drawing.Color.FromKnownColor(KnownColor.Control)
Me.Cursor = System.Windows.Forms.Cursors.Arrow
End Sub
End Class

Public Sub New()
Me.Controls.Add(Prompt)
End Sub

Private Sub PromptTextBox_Resize(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Resize
Dim pt As System.Drawing.Point
pt.X = Me.Width - (Prompt.Size.Width + 3)
pt.Y = 0
Prompt.Location = pt
End Sub

End Class
 
I can't seem to be able to get this to work with .Net...

Dim RightMargin As Long
RightMargin = Prompt.Size.Width + 3
Win32.SendMessage(Me.Handle, EM_SETMARGINS, EC_RIGHTMARGIN, RightMargin)

Public Module Win32
Private Declare Function SendMessageLong Lib "user32.dll" Alias
"SendMessageA" (ByVal hwnd As System.IntPtr, ByVal wMsg As Long, ByVal
wParam As Long, ByVal lParam As Long) As Long

Public Function SendMessage(ByVal hwnd As System.IntPtr, ByVal wMsg
As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim lRet = SendMessageLong(hwnd, wMsg, wParam, lParam)
Return lRet
End Function
End Module

Is it something i am doing or am I just barking up the wrong tree with
this method? No matter what I do, it doesn't make a difference. I even
just tried a static value for a left margin just to get it to work, and
it's not... Tried it with Int32, Int64, Long and then the Handle.ToInt64
... Nothin...
 
Aaron Smith said:
I can't seem to be able to get this to work with .Net...

Dim RightMargin As Long
RightMargin = Prompt.Size.Width + 3
Win32.SendMessage(Me.Handle, EM_SETMARGINS, EC_RIGHTMARGIN, RightMargin)

Public Module Win32
Private Declare Function SendMessageLong Lib "user32.dll" Alias
"SendMessageA" (ByVal hwnd As System.IntPtr, ByVal wMsg As Long, ByVal
wParam As Long, ByVal lParam As Long) As Long

Public Function SendMessage(ByVal hwnd As System.IntPtr, ByVal wMsg As
Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim lRet = SendMessageLong(hwnd, wMsg, wParam, lParam)
Return lRet
End Function
End Module

Replace the 'As Long' in the declarations with 'As Int32'. 'lRet' should be
32-bit too.
 

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

Back
Top