Solved

CRichEditCtrl problem with backspace-char from network

Posted on 2003-10-31
8
669 Views
Last Modified: 2013-11-20
Hi,
I am writing a dialogbased MFC console/telnet application. My problem is that when a backspace character (ascii 8) is arriving from some external source (network, serial...not the keyboard) the CRichEditCtrl class won´t print it out correctly. Instead it prints out a garbage character. Is there an easy way to handle this? I don´t want to check every arriving character, that would slow the application down to much.


void CRichEditCtrlTerminal::OnDataArrival(char *buffer, int size)
{
      // Insert in rich edit
      // Without checking every incoming char
      .
      .
      .
      ReplaceSel(buffer)      // Generates garbage when backspace (ascii 8) arrives
      .
      .
      .
}

This problem is driving me nuts, coz I have a gut feeling that it´s not a difficult problem.

// Icco
0
Comment
Question by:Icco
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9656366
'I don´t want to check every arriving character, that would slow the application down to much'

I would be surprised that would happen.

Get your buffer
use _tcschr to search for an instance of ascii 8, if there is one (or more) then replace with what you want to display.
Now pass the buffer onto the RichEdit
0
 
LVL 3

Expert Comment

by:freewell
ID: 9656381
Just before you pass the result into edit control, do a little conversion. :)

CString strTemp(buffer);
while(strTemp.Replace(0x08,'*'));
ReplaceSel(strTemp);
0
 
LVL 8

Assisted Solution

by:martynjpearson
martynjpearson earned 250 total points
ID: 9656440
There is no option to disregard incoming characters like that, so you will have to do a cleanup. Performance shouldn't be affected that much, but if you are concerned, working with a raw character buffer rather than a CString etc is probably quicker.

If you want to replace the character with another one, then you can just go through your buffer and do a substitution. However, if you wish to remove the ascii 8 characters, then you'll have to create a new buffer and copy all valid characters over, and then pass that to ReplaceSel() thus :

char szWorking = new char[size];
char * pszDest = szWorking;

for (int nCount = 0; nCount < size; nCount++)
{
   if (buffer[nCount] != 8)
     *pszDest++ = buffer[nCount];
}
*pszDest = 0;

ReplaceSel(szWorking);

delete [] szWorking;


Hope this helps
Martyn
0
 

Author Comment

by:Icco
ID: 9657571
I'm sorry if i didn't give you enough information.
When receiving ascii character 0x08 from some external connection I would like the cursor to step one char back.

In this case, a backspace from the OS that I am trying to communicate with through a console port, is the sequence  0x08 0x32 0x08.
This means: go back one char (0x08), print space char(0x32), go back one char (0x08).  When trying to print the 0x08 char to the CRichEditCtrl, it prints out garbage where I want it to go back one char.

example:

When I send the backspacechar to the console, the shell echoes back the sequence 0x08, 0x32, 0x08.
This will be printed as: '',' ','' in the CRichEditCtrl instead of removing the last char.

The sequence 0x08 0x32 0x08 is nothing that I can change, that´s what is received from the console and I have to handle it correctly.

//Icco
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9657618
Do I understand correctly that you would want the 0x08 0x32 0x08 string to be changed to 0x32 ?
If so parse the buffer you have for incidences of this string. Copy the bit before to another buffer.  Move 3 chars forward then look for next instance.  None found, copy rest of string to second buffer, found an instance copy up to that point then forward 3 chars again, continue parsing.
0
 
LVL 8

Expert Comment

by:martynjpearson
ID: 9657619
OK, in that case there are two approaches - either you can process the text to remove characters that fall before the 0x08 (to simulate backspace), or you can break up the data top get the bits of text and the 0x08's, sending the text, then getting the rich edit control to delete the last character, then sending the next.

For example, say you have the data

H e l l l BS o ! ! ! BS

You could process the string so that the third 'l' and the third '!' are removed (along with the backspaces of course), or you could break the text up like this :

Send "Helll" to control
Get control to backspace
Send "o!!!" to control
Get control to backspace

It is probably easier and faster to process the string to deal with backspaces before you get to the control, using code like this :

The one complication arises when the stream starts with one or more backspace characters, as here you will need to instruct the rich edit control to do the backspace for you. However, you could be a little cunning and set the selection so that your text overwrites part of the content simulating a backspace.

Hope this helps
Martyn
0
 

Author Comment

by:Icco
ID: 9657888
So if I understand this right, there isn't a way to insert text (that contains BS-chars) arriving from a shell without processing it or simulating a BS.

martynjpearson writes: "...you will need to instruct the rich edit control to do the backspace for you."
I tried this to simulate a backspace:

      GetSel(start_pos, end_pos);
      start_pos--;
      SetSel(start_pos,end_pos);
      ReplaceSel(CString(""));

Is there an easier way to get the control to do a backspace? The way it works now is not really what I want.
I want it to go back without processing incoming data, but maybe it just isn´t possible...


Real-life example coming up =)
When pressing escape followed by 'k' the shell returns the last command followed by the equal amount of 0x08-chars. It does this so that the cursor will be set at the first position on that line (don´t ask me why, but it does it that way).

In my app the output will be:
-> version
the cursor is set at the last position.

But the output should be:
-> version
and the cursor set at the first position (before the 'v')


// Icco
0
 
LVL 8

Accepted Solution

by:
martynjpearson earned 250 total points
ID: 9657912
You could fire a message to your rich edit control to simulate the keypress, i.e.

SendMessage(WM_CHAR, VK_BACK);

It is probably best to use SendMessage rather than PostMessage so that the processing is done synchronously!

Martyn
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Question regarding Copy/Paste 16 91
isEverywhere  challenge 19 72
fix34  challenge 9 105
Excel file not created as expected 7 51
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now