find char position in edit box and append other text in dialog base application

hi,
 i'm writing  an dialog based application in mfc,
in this  application , i want to press any key from keyboard in edit box, and depending on that key append some msg in edit box,
for eg:
i press r in edit box then edit box display "red" in same edit box,
next i press enter key then  edit box append "hello" after previous msg red(i.e  redhello)
rpatnaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
ravineshConnect With a Mentor Commented:
take a sub class of CEdit and add message handler OnKeyDown and OnChar


void CHyperEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
         // comment this to read enter key
     //CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CHyperEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
        CString str,str1;
        switch (nChar)
        {
                case 'r':      
                                str="red ";
                                break;
                case 'R':

                                str="Red ";
                                break;
                case 'g':
                case 'G':

                                str="Green ";
                                break;
                case 'b':
                case 'B':

                                str="Blue ";
               
                                break;
                case ':' :
                                str=": ";
                                break;
                case 13:
                                str="NewLine ";
                                break;
                case 27:
                                str="Esc ";
                                break;
                default :
                                str = nChar;
        }
                this->GetWindowText(str1);    
                str1 += str;
                this->SetSel(-1,-1);
                this->SetWindowText(str1);

                int x,y;
                x = strlen(str1);
                y = strlen(str1);
                this->SetSel(x,y);
         
          // comment this to not display char press by user
          //CEdit::OnChar(nChar, nRepCnt, nFlags);
}
0
 
kirthirCommented:
1)Subclass the CEdit class
2)Override PreTranslatemessage of CEdit subclass, incorporate message to capture
 KEYDOWN message
3)In the switch case statements of KEYDOWN message, do what ever u want for
whatever key pressings.

0
 
GEliyahuCommented:
Well this is my solution.
Capture the message WM_KEYDOWN;
than capture with a switch case and insert the folow code.

char Press;// Press is the input of the WM_KEYDOWN function
CString Str;
switch (Press)
{
case 'r':
case 'R':

m_CEdit.GetWindowText(Str);
Str+=" Red";
m_CEdit.SetWindowText(Str);

break;
}

Good luck;

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
rpatnaAuthor Commented:
hi GEliyahu

this is not too easy.
in DilogBase Application U don't have
WM_KEYDOWN message
 
kirthir  solution is in the right way but
how to know the char position in edit box
or
how to append string in the same edit box.
SetWindowText(Str) is not work in case of
Subclass of the CEdit class

can U give me the full code?

 

0
 
GEliyahuCommented:
Ok i suggest another way.
Capture the message EN_CHANGE;

CString Str;
m_CEdit.GetWindowText(Str);
char Press = Str.GetAt(Str.GetLength()-1);
//This will give you the last charachter that you user input

switch (Press)
{
case 'r':
case 'R':

Str+=" Red";
m_CEdit.SetWindowText(Str);

break;
}

I check it and it's working perfectly.
Hope it will work for you to..


0
 
rpatnaAuthor Commented:
hi GEliyahu
thanks for quick comment,
i'll check later
0
 
rpatnaAuthor Commented:
Hi GEliyahu ,

in EN_CHANGE message, not check the enter key, tab key.

actually I'm making an edit box which will work same as hyper terminal
means if i press any key in the edit box that char send to another system
by com port, and whatever returns from another system append in edit box
again repeat the same things.

for eg:

I press d ('d' immediately send to another system through com port
         and com port send 'd' back)
I press enter key ( it send 13 to another system through com port
               and returns a message which will be display in edit box)
0
 
Vinayak KumbarSr Program ManagerCommented:
Hi,

Using class wizard map the PreTranslateMessage() function and modify it as

....

if(pMsg->message == WM_KEYDOWN)
{
//Detect the char pressed by wParam;
//Send that char to the other application
}
....

By this, the character u have pressed will be printed in edit control automatically. For that u need not worry. But, when u get some string back from COM port, now u need to append that string and keep the cursor at the end so that again whatever u type will be printed at last. So, whenever u receive a string call the following function with that new string

void CAddDlg::AddText(CString cNewStr, BOOL bAppend)
{
    CString cOldStr;
    CString cFinalStr;
    m_ctrlEdit.GetWindowText(cOldStr);

    if(bAppend)//Append
    {
         cFinalStr = cOldStr + cNewStr;          
    }
    else //Prepend
    {
         cFinalStr = cNewStr + cOldStr;
    }

    m_ctrlEdit.SetSel(0, -1);
    m_ctrlEdit.ReplaceSel(cFinalStr, TRUE);
    m_ctrlEdit.SetFocus();
}

That function takes the string to add. It adds the passed string at the start or at the end depending on the flag.

Add the prototype as

void AddText(CString cNewStr, BOOL bAppend = TRUE);

to the h file as public member.

Try it out.
VinExpert
0
 
rpatnaAuthor Commented:
hi ravinesh

your comments is my real solution
thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.