Solved

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

Posted on 2002-03-16
9
335 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 200 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Creating Labels and Frequency list style reports in SAS code 9 130
dog bark java program 15 132
Use of condition with 'serial' in ansible 2 102
Problem to event 3 123
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 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