Click here to Skip to main content
15,740,620 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using VB.Net (Win Forms) and would like to know how to set Insert / Overwrite Mode programmatically in a RichTextBox control.



What I have tried:

I have tried using both
My.Computer.Keyboard.SendKeys("{INSERT}", True)
but neither option works.
Sergey Alexandrovich Kryukov 4-Feb-16 14:17pm    
Using SendKeys in UI development is dirty. You don't really need it. What do you want to achieve?
Tino Fourie 4-Feb-16 14:27pm    
Hi SA. You are perfectly correct in your reply as most of the documentation I have studied advised against that and MS also warned about Timing issues with both Old and New methods implemented in SendKeys - I just could not find other way of achieving my goal.

I am trying to format text in two columns using a RTB for printing purposes (a DGV will not work unfortunately). I have Line Descriptions of a fixed length and then Barcodes of a variable length. First, I AppendText the Line Descriptions after which I move the caret (cursor) back and forth to Paste the Barcode from clipboard.

Needless to say, as you probably know, Insert mode is default which means every time I paste the Column 1 Barcode the Line Description of Column 2 will move forward depending on the width of the Barcode image.

Let me know if you need more info on what I am trying to achieve.

Sergey Alexandrovich Kryukov 4-Feb-16 14:59pm    
Unfortunately, RichTextBox is not comprehensive at all; it covers only most primitive and common formatting. Here is how people solved this problem: they used the property RichTextBox.Rtf to enter low-level formatting fully described in RTF format documentation. This format is considered as badly obsolete and barely usable, suitable only for the minimalistic formatting. Alternatives are WFP FlowDocument, HTML, and more...

However, I cannot understand how Insert/Override mode can be related to text in columns. How do you picture those columns? The only support is using tables:

Tino Fourie 4-Feb-16 15:10pm    
In most part I completely agree with you, however I still find it a useful tool when it comes to printing. Sadly, as you already mentioned, it is severely handicapped in functionality and the RTF code is almost not from this world, thus not easily learned or implemented.

Thanks for your reply and alternative options provided.


Edit: Here is the code I use to format the text - hope it sheds some light on what I am trying to do ;)

'Description Lines
'Patient Information and Contact Information Columns
Col1 = "Patient ID :" ' Patient Information
Col2 = "Mobile Number :" ' Patient Contact Information
'Insert Line
Dim DescriptionLine As String = String.Format("{0, -70} {1, -24}", Col1, Col2)
'We now have to move the cursor back to the first Col in order for us to paste the first Barcode. To do this
'we need to (1) Store the current cursor postion and (2) subtract 70 + 24 from the current position to paste
'Col1 Barcode, (3) move the cursor to the stored cursor postion to paste Col2 Barcode
CursorPosition = rtfMain.SelectionStart
'Move cursor back to Col1 position + 4 chars from ":"
rtfMain.SelectionStart = CursorPosition - 70 + 4
'Paste Barcode for Col1
If Not frmMain.ucPatientInformation.txtPatID.Text = "" Then
'We need to put the RichTextBox in Overwrite mode before pasting the Barcode
'My.Computer.Keyboard.SendKeys("{INSERT}", True)

'Paste the Barcode

'Enable Insert mode again
'My.Computer.Keyboard.SendKeys("{INSERT}", True)
Sergey Alexandrovich Kryukov 4-Feb-16 15:20pm    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900