Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting cursor position in a richtextbox

Status
Not open for further replies.

dbrb2

Instructor
Jul 19, 2004
121
GB
Hi...

I am writing to a richtextbox, and currently once I have
finished entering data, to updfate it I set the contents to "" then append the new data with a loop.

This causes an annoying flickering - ideally I would just set the cursor position to the start of the text box and let it overwrite the current contents with each new append statement, but this has so far escaped me.

Is there a solution? Since I can append text, there must be a variable somewhere keeping track of the cursor position, so if I could just set that to zero....

Cheers,

Ben
 
Try
Code:
RichTextBox.SelectionStart = 0
You may also need to set SelectionLength to zero if you want to insert text rather than replace the selected text
 
Hi Techsmith,

I think I tried that, and it seemed to just append forever rather than going back to the beginning, but I'll give it another go.

I'm thinking of trying to bind the array from which the data is stored directly to the display - possibly using a data grid - to speed things up, but for now if there is a way to do this it would be great.

I'll let you know what happend when I try this again...

Cheers,

Ben
 
Hi - no joy I'm afraid. I am trying to print out the contets of my array as shown below. The commented out line works, but makes the display flash horribly as the loop executes. The two lines following it fail to have any effect, and each loop merely appends data to that left by its predeccesor...

Private Sub PrintArray()
Dim r, c
Dim character As Char
'receiveBox.Text = ""
receiveBox.SelectionLength = 0
receiveBox.SelectedText = 0

For r = 0 To 23
For c = 0 To 79
character = vt100(r, c)
receiveBox.AppendText(character)
Next
receiveBox.AppendText(Chr(13))
Next
End Sub
 
...perhaps however it would work in a textbox (as opposed to a richtextbox)

I assume that when I set the position of the caret the entire text box is numbered left to right, top to bottom? There is no direct way to set X,Y, but if I know the dimensions of the box I ca work it out?

Cheers,

Ben
 
ah yes - having set multiline to true I can now use a textbox and the commands, which failed in a richtextbox, now work :)

Ben
 
Hmmm.... but having set the caret position, if I then append text it appends it to the end of the text box, not to the new caret position. How annoying...Is there a solution?
 
Having followed this thread, I must admit to not understanding what you are trying to achieve.

With both a TextBox and a RichTextBox you can INSERT text, REPLACE text and APPEND text (as well as DELETE text). techsmith pointed you in the right direction for handling any of these actions.

[vampire][bat]
 
Oh dear - I must have garbled things a bit.

Basically in both cases it would appear that I can only append
text to the end of the box, and setting the position of the caret has no effect.

When I put in:

TextBox1.Size.Height.Equals(24)
TextBox1.Size.Width.Equals(80)

TextBox1.SelectionStart.Equals((80 * V) + (H + i) TextBox1.SelectionLength = 0
TextBox1.AppendText = data(i)

Here V is my Vertical coordinate, H my horizontal. nowing the size of the text box I can convert this to a caret position.

However, the final line always seems to append text to the end of th box, not to wherever I have placed the caret. I am trying to set its position so that I can edit text without redrawing the entire winow...

Cheers,

Ben



 
I think I see where you are going wrong.

If you want to insert text ie. data(i) in a certain position in the text use
Code:
TextBox1.Insert(StartIndex, Value)

The Selection... properties are more for the GUI so that the next keyed or pasted text will replace the selection.
 
Thanks for that. Can I just check the syntax above...

I've set my textbox to width=80,height=24:

TextBox1.Size.Height.Equals(24)
TextBox1.Size.Width.Equals(80)

I would assume that this sets the maximum index to 80*24=1920

But even putting

Textbox1.text.insert(1,"foobar")

gives an index out of range error. Any ideas?

Cheers,

Ben




 
If you try Textbox1.text.insert(1,"foobar") when text = "" then you will get an out of range error as there is no chr in position 1.

The StartIndex is zero-based, so to insert text at the start use Insert(0,"foobar")

I think you are getting confused with the height and width properties. The StartIndex is a character position in the string. You may be able to work out a character position from an X,Y co-ordinate, but I suspect you don't really want to do this.

If I want to change "Some text I came up with all by myself" to "Some brilliantly imaginative text I came up with all by myself" I would use Insert(5,"brilliantly imaginative ")

Hope this helps
 
ahahh...so I can't create a textbox with a given number of elements, I need to create them by adding data first. So to create my 80*24 elements I woul need to, for example, add 1920 spaces to the textbox using a loop.

Is there an "overwrite" rather than "insert" method? So that I could start with "Hello Cruel World" and end up with Hello Happy World"?

Cheers,

Ben

 
OK, I decided to completely re-read everything you've posted - realised that you just want to dump an array into a textbox - adapted your Sub
Code:
	Private Sub PrintArray()
		Dim str As String = ""
		Dim r, c
		Dim character As Char

		For r = 0 To 23
			For c = 0 To 79
				str += vt100(r, c)
			Next
			str += Chr(13)
		Next
		receiveBox.Text = str
	End Sub

You don't need to set the Text property of your TextBox until you've built up the string, so no need to insert at all.

Hope this does it...
 
Ah yes - thanks. I was doing it like that originally - I was trying to
use the index methid so that I could update the textbox without having to loop through the entire array if just one or two elements had changed...

Thanks for all of your help,

Ben

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top