autscrolling textbox on modeless userform

E

Erich Neuwirth

I am using a modeless form with a text box to display
some debugging information while some routines are running.
it works like this:

Sub PrintDebug(Msg As String)
ErrorForm.ErrorTextBox = ErrorForm.ErrorTextBox & _
"Debugging: " & Msg & vbLf
End Sub

I would like the text box to scroll down when it has more text than can
be displayed at once.
With the code above, always the text from the start is displayed.
What do I need to do to make the displayed text
scroll automatically without giving the textbox (or the userform)
focus?


I found some VB code which is supposed to d this in VB, not in VBA.
But I have no idea who I can get the handle of a Textbox
(or of any forms element) in VBA.

Option Explicit

Private Declare Function PostMessage Lib "user32" Alias "PostMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal
lParam As Long) As Long
Private Const EM_LINESCROLL = &HB6


Private Sub Command1_Click()

Text1.Text = Text1 & "test" & vbCrLf
PostMessage Text1.hwnd, EM_LINESCROLL, 0&, 2& 'scrolls down 2 lines, or
to bottom.
End Sub
</PRE></CODE>
 
K

keepITcool

you could use a listbox rather thn a textbox..
example needs a Userform2 with 1 listbox and 1 textbox

Option Explicit

Dim ufError As userform2


Private Sub CommandButton1_Click()
With ufError.Listbox1
.AddItem "Error" & Format(Now, "hh:mm:ss")
.ListIndex = .ListCount - 1
End With

Dim sAdd As String

sAdd = "Error" & Format(Now, "hh:mm:ss") & vbNewLine
With ufError.TextBox1

.Text = .Text & sAdd
.SelStart = Len(Replace(.Text, vbNewLine, Space$(1))) - _
Len(Replace(sAdd, vbNewLine, Space$(1)))
.SelLength = Len(sAdd)

End With
End Sub


Private Sub UserForm_Initialize()
Set ufError = New userform2
With ufError
With .TextBox1
'These should be set in designer!
.WordWrap = False
.MultiLine = True
.ScrollBars = 2
.HideSelection = False
End With

.Show vbModeless
.Top = Me.Top
.Left = Me.Left + Me.Width + 5
.Caption = "DEBUGGING"
End With
End Sub



keepITcool

< email : keepitcool chello nl (with @ and .) >
< homepage: http://members.chello.nl/keepitcool >
 
E

Erich Neuwirth

Alas,
listboxes wont work because the messages I get are multiline.
Or is there a way of having multiline items in listboxes?
 
E

Erich Neuwirth

I read it,
but the textbox also does not work the way I need it to work.

Here is my adaptation.

Userform1 has TextBox1 and CommandButton1
Userform1 is Modeless
TextBox1 has WordWrap and MultiLine = True
CommandButton1_Click runs display

Running RunIt and then clicking CommandButton1
behaves as expected, the textbox scrolls.

Running RunIt a second time or running display
adds the text, but does not scroll.

This can be done since UserForm1 is modeless, so one can switch to
the VBA IDE with the box still displayed and execute the commands
from there.

And that is what I really need.
The sub writing to the textbox will be called from some other code,
and not from a button on the modeless dialog box.
And that is what does not work as expected in the code.



Sub display()
Dim oldlen As Integer
Dim text2add As String
For i = 1 To 20
With UserForm1.TextBox1
oldlen = Len(.Text)
text2add = "hallo " & i & vbLf
.Text = .Text & text2add
.SelStart = oldlen
.SelLength = Len(text2add)
Ed With
Next i
UserForm1.TextBox1.SetFocus
End Sub


Sub RunIt()
If Not UserForm1.Visible Then UserForm1.Show
display
End Sub
 
K

keepITcool

Erich,

you must be sure you "write" to the current instance of the
loaded userform. That's where the object variable comes in.

dim uf as userform1
set uf = new userform1

uf.show
uf.textbox1.text etc

if you need access to uf from "various" places,
dim it as Public somewhere in a normal module.





keepITcool

< email : keepitcool chello nl (with @ and .) >
< homepage: http://members.chello.nl/keepitcool >
 

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