Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CRichEditCtrl problem with backspace-char from network

Posted on 2003-10-31
8
Medium Priority
?
713 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 750 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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 750 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

664 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