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

x
?
Solved

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

Posted on 2002-03-16
9
Medium Priority
?
344 Views
Last Modified: 2013-11-20
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)
0
Comment
Question by:rpatna
[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
9 Comments
 
LVL 1

Expert Comment

by:kirthir
ID: 6871600
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
 

Expert Comment

by:GEliyahu
ID: 6873623
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
 

Author Comment

by:rpatna
ID: 6873651
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
Technology Partners: 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!

 

Expert Comment

by:GEliyahu
ID: 6873663
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
 

Author Comment

by:rpatna
ID: 6873709
hi GEliyahu
thanks for quick comment,
i'll check later
0
 

Author Comment

by:rpatna
ID: 6875987
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
 
LVL 8

Expert Comment

by:VinExpert
ID: 6888411
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
 

Accepted Solution

by:
ravinesh earned 800 total points
ID: 6892014
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
 

Author Comment

by:rpatna
ID: 6892020
hi ravinesh

your comments is my real solution
thanks.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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 this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

688 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