Set Text on Textbox w/o changing cursor pos

W

WALDO

I have a .Net TextBox (TextBoxBase, really) in which I am appending about 20
lines of text per second. I use the AppendText() method to accomplish this.
This is a great substitute for taking the Text property and concatenating
it...

Me.tb.Text &= newText
' Instead use
Me.tb.AppendText(newText)

....ultimately setting the SelectionStart to the end of TextBox, the
SelectionLength to the length of the new text and replacing the SelectedText
(via the SendMessage API function, determined by using Lutz Roeder's
Reflector).

This keeps the cursor at the end of the TextBox as text is appended to it
without flicker. I wish to perform this same function, but keep the cursor
position where it is (if it is not at the end of the TextBox). If I persist
the selection position and length before I append the text and restore them
aftrer, it creates AWFUL flicker.

Is there a message I can send, maybe using the SendMessage API, that will
append text but not change the position or the selection?

Picture the Output Window in Visual Studio as you are debugging and calling
Debug.WriteLine(newText) at the same rate. If your cursor is set at the end
of the TextBox, it stays at the end as lines are written. If you select some
text, your selection stays where it is, but text is still appended. That's
exactly what I'm trying to do.

Dim i As Integer
For i = 0 To 10000
Debug.WriteLine("I am writing line " & i)
Next

Any help is appreciated.
Thanks in advance.

WALDO
 
B

Bob O`Bob

WALDO said:
I have a .Net TextBox

Good for you.
But please do not post about it in any of the classic VB newsgroups.

If it doesn't say "dotnet" (or "vsnet") then it isn't for dotnet.


thanks,


Bob
MS MVP - VB, 2000-2006
--
 
W

WALDO

It is posted to microsoft.public.vb.winapi because I am looking for a
SendMessage message. I think that qualifies.

Doesn't it expend less energy to help if you you have an answer or shut the
hell up if you don't than to just be an ass? Isn't it exhausting, Bob?

--

Any help is appreciated.
Thanks in advance.

WALDO
 
T

Thorsten Albers

WALDO said:
It is posted to microsoft.public.vb.winapi because I am looking for a
SendMessage message. I think that qualifies.

Why then don't you ask your question in a C(++) group as well where people
do know a lot about SendMessage? Right, because C(++) and VB.NET are
different languages. Know, let us ensure you that VB.NET and VB are
different languages as well.
Doesn't it expend less energy to help if you you have an answer or shut the
hell up if you don't than to just be an ass? Isn't it exhausting, Bob?

This for the future qualifies questions of yours as to be ignored. Good
bye!

--
 
R

Rick Rothstein \(MVP - VB\)

It is posted to microsoft.public.vb.winapi because I am looking for a
SendMessage message. I think that qualifies.

Doesn't it expend less energy to help if you you have an answer or shut
the hell up if you don't than to just be an ass? Isn't it exhausting, Bob?

The thing that you don't seem to realize is that almost everything in your
original post is meaningless to those who visit and/or answer questions here
in the microsoft.public.vb.winapi newsgroup which is what made it
inappropriate for this newsgroup. To list just a few of these... we have no
idea what a TextBoxBase (really) is; our TextBox'es do not have an
AppendText method, so while that may be a great substitute for you, it has
nothing to do with how we would be able to approach the problem; we have to
guess that your &= operator(?) means to concatenate newText onto Me.tb.Text
(that is a correct guess, right?); our TextBox'es do not have SelectionStart
and SelectionLength properties (although I would guess our SelStart and
SelLength properties would be equivalent); and another guess we have to make
is that your Debug.WriteLine(newText) statement is equivalent to a
Debug.Print newText statement in our world (although I have no idea if
WriteLine is perfectly equivalent to Print). Oh! And we have no idea who
Lutz Roeder is nor what his Reflector is or does (whatever it is, I'm
guessing it won't work with VB6 or earlier code). So, given we have to guess
at, or we outright don't know, what most of your post was about, it amounted
to nothing but noise here in this non VB.NET newsgroup. That was pretty much
the point of Bob's response to you.

Rick
 
C

Cor Ligthert [MVP]

Waldo,

Why do you want to use a VB6 method in VB.Net.

To set the position for a Textbox is just methods.
http://msdn.microsoft.com/library/d...rlrfsystemwindowsformstextboxmemberstopic.asp

AFAIK can you use the selection start.

In addition to the others, it is mostly better to describe what you want to
achieve than what you are doing, and than of course to the proper newsgroup.
The farmost largest newsgroup for VB is
microsoft.public.dotnet.languages.VB.

Cor
 
J

J French

It is posted to microsoft.public.vb.winapi because I am looking for a
SendMessage message. I think that qualifies.
Doesn't it expend less energy to help if you you have an answer or shut the
hell up if you don't than to just be an ass? Isn't it exhausting, Bob?

Actually he /was/ being helpful

We would have given you misleading answers

Also Bob saved the rest of us pointing you to where you can get real
help.
 
T

teslar91

Waldo,

I found this question interesting. It seems that many people have
asked, and no one has ever provided a workable answer other than to use
a 3rd-party control.

So I looked into it myself, and spent several hours researching and
experimenting, as this is something I can use also.

Here is the best I was able to manage with the stock textbox. I used a
multiline textbox with vertical scrollbar, a timer set to 50ms, and
this code:

Const WM_SETREDRAW = &HB
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal
hWnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam
As Int32) As Int32

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Dim i1 As Integer = TextBox1.SelectionStart
Dim i2 As Integer = TextBox1.SelectionLength
Dim b1 As Boolean = (i1 < TextBox1.Text.Length)
If b1 Then SendMessage(TextBox1.Handle, WM_SETREDRAW, 0, 0)
TextBox1.AppendText(Now & vbCrLf)
If b1 Then
TextBox1.SelectionStart = i1
TextBox1.SelectionLength = i2
SendMessage(TextBox1.Handle, WM_SETREDRAW, -1, 0)
End If
End Sub

Turning off redraw for the control while maintaining the cursor
position prevents any unwanted scrolling from occurring - a definite
improvement in behavior and flicker. However, it still flickers due to
the sometimes visible delay between the textbox's painting of the
background followed by the text.

Setting DoubleBuffer = True on the form means all painting is done in a
separate graphics buffer before being copied to the screen, which
should eliminate the flicker. However, it has no effect on the
textbox!

After exhausting quite a few ideas with no success, I determined that
the only feasible method was to create my own "DoubleBuffer/UserDraw"
scheme:
1) Suppress repainting of the textbox to the screen after the
..AppendText.
2) Paint to a bitmap instead.
3) Paint the bitmap onto the textbox.

I got it working, except:
1) The cursor flickered terribly during appends, as it wasn't painted
to the bitmap.
2) The scrollbars didn't paint properly either.

At this point, I decided getting this method to work properly was
non-trivial. Fortunately, someone else has already perfected it:

"AlphaBlendTextBox - A transparent/translucent textbox for .NET"
http://www.codeproject.com/cs/miscctrl/alphablendtextbox.asp

It's flicker-free, works great when used with the code above, and
provides some extra graphic options. Which can be turned off if you
don't need them.

There's still one small issue that I'm not sure how to solve - try
selecting text *in reverse*. Maybe you can figure it out. :)
 
T

teslar91

Bob said:
Good for you.
But please do not post about it in any of the classic VB newsgroups.

If it doesn't say "dotnet" (or "vsnet") then it isn't for dotnet.

That's interesting.

This is microsoft.public.VB.winapi. Not VB4, 5, or 6; just VB. Isn't
VB.NET is a version of VB?

But seriously, I understand what you're saying. This question should
go to a dotnet.winapi or vsnet.winapi newsgroup. Oh wait, *there isn't
one*.

So basically, the situation is this: if someone has a question
specifically about using the API in VB, and they *naturally* take it to
the ONLY Microsoft newsgroup that has API and VB in the name, you're
going to get cranky on them. Gotcha. Nice MVP.

If you don't want to have these questions here, why not redirect some
of that energy from being rude, into getting MS to set up a more
appropriate dotnet.vb.winapi newsgroup? That would only make sense...
 
K

Ken Halter

Cor Ligthert said:
Waldo,

Why do you want to use a VB6 method in VB.Net.

He obviously doesn't know what he wants (except non-standard behavior from a
control that's worked the same for decades)... and will probably never see
your answer because now, he's pouting. Can't expect too much from someone
that calls people an ass in a public group, then ends the post with 'any
help is appreciated'... obvously a lie, in itself.
 
C

Cor Ligthert [MVP]

Waldo,

Without words,

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim ar As New System.Text.StringBuilder("")
For i As Integer = 0 To 100
ar.Append("who state something without reading the replys first,
and is even accesing the UI direct while he can use a stringBuilder")
Next
TextBox1.Text &= ar.ToString
TextBox1.SelectionStart = 13
TextBox1.SelectionLength = 0
TextBox1.Focus()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "It is a fool, "
TextBox1.Multiline = True
End Sub

Cor
 
T

teslar91

Cor said:
Waldo,
Without words,

I'm laughing my arse off right now.

Not only is that the most clever way to insult someone I've seen in a
long time, I tried it out and it doesn't address Waldo's problem at
all. :)
 
W

WALDO

I don't care about the language. What you guys fail to realize is that I'm
not looking for a VB answer. I'm looking for a message I can send via
SendMessage, which last I checked was part of the Windows API, is it not? I
happen to be using VB.Net and a VB.Net textbox and explaining that so anyone
who DOES know may follow along.

You couldn't possibly tell me that my question is meaningless in the WinAPI
group because that's exactly what I'm looking for.

My point is that it took five additional posts for nobody to tell me
anything useful. So it was really a waste of both your and my times. If you
don't have anything to say that helps, don't reply. As for Thorsten, at
least he (I'm guessing it's a he) said something that helps, which was that
people in a C++ group might know more about SendMessage than in the generic
winapi group.
 
R

Rick Rothstein \(MVP - VB\)

I don't care about the language. What you guys fail to realize
is that I'm not looking for a VB answer.

It might be the case (I've seen it way too often) that the question you
asked may have been the wrong one. It is possible that VB.NET provides a
method that you are not aware of that would allow you to do what you asked.
We here (well, at least 99.9% of us) in the "vb" newsgroups would not know.
It is best to first ask your question in a VB.NET newsgroup and, if you
don't get an answer there, then post to other newsgroups which might
possibly be able to help (but if you do, mention that you first posted in to
the VB.NET community and they were not able to answer your question).
I'm looking for a message I can send via SendMessage, which
last I checked was part of the Windows API, is it not?

To the best of my knowledge, no such message exists to do what you asked
for.
I happen to be using VB.Net and a VB.Net textbox and
explaining that so anyone who DOES know may follow along.

The point of the majority of the responses you have gotten is that those
examples are, for the most part, meaningless to those in the "vb"
newsgroups. If you have a generic question about SendMessage, and you want
advice from those in the "vb.winapi" newsgroup, just pose the question with
a general description and leave out all the VB.NET mumbo-jumbo.
You couldn't possibly tell me that my question is meaningless
in the WinAPI group because that's exactly what I'm looking for.

Most of us will not read a VB.NET question in detail (we don't know the
language and it is different enough from the classic version of VB that we
are unlikely to be able to provide an answer, so we do not even waste our
time trying). See the last sentence of my first comment above as to a better
way to approach the "vb" newsgroups.
My point is that it took five additional posts for nobody
to tell me anything useful.

The type of responses that you have gotten might have something to do with
the tone of your initial response to Bob
So it was really a waste of both your and my times.

You think they are unhelpful; to us, they were just trying to keep down the
VB.NET postings here (to us, they are nothing but noise).
If you don't have anything to say that helps, don't reply.

From our standpoint, trying to educate VB.NET posters that their questions
are off topic in newsgroups with "vb" in their name is saying something that
helps.
As for Thorsten, at least he (I'm guessing it's a he) said
something that helps, which was that people in a C++ group
might know more about SendMessage than in the generic winapi group.

Just so we are completely clear, the microsoft.public.vb.winapi newsgroup is
**not** a generic winapi newsgroup.

Rick

Oh, by the way, here is how I would do what you originally asked for (note,
it is VB6 code and does not use any API functions)...

Dim i As Long
Dim Cursor As Long
With Text1
.Visible = False
Cursor = .SelStart
For i = 0 To 10000
.SelText = "I am writing line " & vbNewLine
Next
.SelStart = Cursor
.Visible = True
.SetFocus
End With

What the code does is first turn off the visibility of the TextBox, memorize
the cursor position, add the text using the SelText property (this looks
like it is mostly equivalent to VB.NET's AppendText method in
functionality), return the cursor to its initial location and then restore
the visibility of the TextBox. The TextBox will not actually disappear from
the screen; this will simply stop the system from trying to continually
update the screen.
 

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