Solved

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

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

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

14 Experts available now in Live!

Get 1:1 Help Now