|
-
April 29th, 1999, 06:50 AM
#1
Edit control, Question...
Hi,
I have a little problem with Edit control, I have one edit control for the user to enter one password, so I have:
****
And I want that when the user set the focus to that edit control and writes the first letter the edit control deletes its content and shows only the letter that the user has entered with the keyboard, I have tried somethings, but don't works, here is what I tried:
-> OnSetFocus, SetSel all the string ->But don't works, I don't know why... ( this is the ideal thing, but I don't know how to make it work)
-> When the user presses ( on change edit) the first time, I try to find de different character, and I delete the string and put the character ( the problem is that not always I'm able to know what character is the new), and another problem is that put the character, but the cursor is not in the end of the edit control, so the if the user wants to write fast ( "ABC"), it will do "BCA").
... Anybody can help me ? Thanks, Bye !
Braulio
-
April 29th, 1999, 07:14 AM
#2
Re: Edit control, Question...
You can use Notification-Message, EN_SETFOCUS.
if you choose EN_SETFOCUS Messages in ClassWizard, then OnSetfocusXXX() function will appear.
In this function, type code like this
this->SetDlgItemText(IDC_EDIT1,"");
Is this what you want? I hope so.....
by nfuox
-
April 29th, 1999, 07:45 AM
#3
Thanks, But...
Thank But the problem then is that everytime the user put the focus on this password field its the password deleted, and I would like to do it only when the user wants to change the password ( presses one key on it), the best idea is to select all the text in OnSetFocus, but I have tried and I don't know why but it don't selects nothing ( only selects all the text by default, if the user presses double click or if he go with the tab key, with one click with the mouse it selects nothing ( put the focus, executes SetSel but nothing happens).
Can you help me a little bit more..., Thanks, Bye !
Braulio
-
April 29th, 1999, 08:03 PM
#4
Re: Edit control, Question...
SetSel should change...
can you show us how you call SetSel in your code please
Sally
-
April 30th, 1999, 01:28 AM
#5
Re: Edit control, Question...
Hi !
I have tried in a lot of ways:
SetSel(0,Length_of_the_string)
SetSel(0,-1)
This things in normal cases works rights, but I think that the problem with do this on set focus, is that if it's presses with the mouse, after do this setfocus, then the application deletes what is selected.
Finally one of our friends of codeguru, has give me one solution, that is not so pretty as put the "blue" selected line, but it's very interesting, here goes:
BOOL CUserDlg::PreTranslateMessage(MSG* pMsg)
{
CEdit *pEdit = (CEdit *) GetDlgItem(IDED_PASSWORD);
if (pMsg->message == WM_CHAR) {
if (GetFocus() == pEdit && m_bNewFocusOnEditPwd) {
pEdit->SetWindowText("");
m_bBlankPassword = FALSE;
m_bPasswordChanged = TRUE;
m_bNewFocusOnEditPwd = FALSE;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
With this, when the user tries to tpye something the first time that has setted the focus on it, the content of the edit control is deleted, and the new character that the user has typed is setted ( because we empty the content before this message is dispatched "?").
Thanks for your interest, Thanks, Bye !
Braulio
-
April 30th, 1999, 01:56 AM
#6
Re: Edit control, Question...
Is this a CEdit object created in your dialog with the resource editor, or do you create it at run time?
If you're using the resource editor, here's what I'd do:
Add the edit control to the dialog and give it the ID value IDED_PASSWORD and set it's style to Password.
Now run ClassWizard and under the member variables create a member variable for your edit control of type control and name it something like m_CtrlPassword. This will add a public member to your dialog class' header file, like:
CEdit m_CtrlPassword;
Now in ClassWizard, select the Message Maps tab and select IDED_PASSWORD in the Object ID's list and find the entry in Messages for EN_SETFOCUS and press Add Function, accept or change the suggested function name. This should create a function body for you - press Edit Code to go to the function body.
void CUserDlg::OnSetfocusPassword()
{
m_CtrlNewPath.SetSel(0, -1);
}
And that should do it.
Rail
Recording Engineer/Software Developer
Rail Jon Rogut Software
[email protected]
http://home.earthlink.net/~railro/
-
April 30th, 1999, 02:01 AM
#7
Re: Edit control, Question...
Oops... sorry I copied some test code from my system and didn't rename the variable:
void CUserDlg::OnSetfocusPassword()
{
m_CtrlNewPath.SetSel(0, -1);
}
should read:
void CUserDlg::OnSetfocusPassword()
{
m_CtrlPassword.SetSel(0, -1);
}
In the example I posted.
Rail
Recording Engineer/Software Developer
Rail Jon Rogut Software
[email protected]
http://home.earthlink.net/~railro/
-
April 30th, 1999, 02:08 AM
#8
Re: Edit control, Question...
I have tried this but don't works ( when the user sets the focus with the mouse with one single click), I don't know why, I have tried a more complicated thing, that don't shows the blue line but works ( it's not 100 % a solution but...), is:
BOOL CUserDlg::PreTranslateMessage(MSG* pMsg)
{
CEdit *pEdit = (CEdit *) GetDlgItem(IDED_PASSWORD);
if (pMsg->message == WM_CHAR) {
if (GetFocus() == pEdit && m_bNewFocusOnEditPwd) {
pEdit->SetWindowText("");
m_bBlankPassword = FALSE;
m_bPasswordChanged = TRUE;
m_bNewFocusOnEditPwd = FALSE;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
I have learned a lot from this code, but is not the perfect solution, thanks, Bye !
Braulio
-
April 30th, 1999, 02:17 AM
#9
Re: Edit control, Question...
If the code I posted doesn't work, then something's a little strange...?? Have you changed the background colour or placed a bitmap on your dialog? If you use the code I posted, then you can't also have the PreTranslateMessage code in there as well...
The only time I've seen something similar to what you're describing is when I placed a bitmap on my dialog box and the background brush of the dialog or edit control was changed and the background wasn't being repainted correctly -- to overcome that, I had to adjust the brush accordingly.
Good luck.
Rail
Recording Engineer/Software Developer
Rail Jon Rogut Software
[email protected]
http://home.earthlink.net/~railro/
-
April 30th, 1999, 06:58 PM
#10
Re: Edit control, Question...
I tried your exact code and it doesn't work on my machine either!!! It works if you TAB to the edit field, but not if you click with your mouse.
-
April 30th, 1999, 08:40 PM
#11
Re: Edit control, Question...
Okay,
I slightly misunderstood what he really wanted (that's what happens when I answer posts late at nite). To do what he wants requires that he subclass the CEdit control and look for WM_LBUTTONDOWN.
I've created a quick example project to show how to do it at my MFC Examples web page on my site:
http://home.earthlink.net/~railro/mfc_link.html
Look at Example 14.
Regards.
Rail
Recording Engineer/Software Developer
Rail Jon Rogut Software
[email protected]
http://home.earthlink.net/~railro/
-
May 1st, 1999, 08:25 PM
#12
Re: Edit control, Question...
Are you saying that OnSetFocus is ONLY called when someone TABs to a field and not when one clicks with the mouse in a field?
Sally
-
May 2nd, 1999, 12:21 AM
#13
Re: Edit control, Question...
No... whenever you click on an edit control, it receives focus - and when you Tab to the edit control it receives focus. However, to capture ths WM_LBUTTONDOWN message for the edit control, which differentiates between tabbing into the edit control vs. clicking in the edit control to set focus to the edit control... you have to subclass the edit control.
Rail
Recording Engineer/Software Developer
Rail Jon Rogut Software
[email protected]
http://home.earthlink.net/~railro/
-
May 2nd, 1999, 01:54 AM
#14
Re: Edit control, Question...
-
May 2nd, 1999, 02:37 AM
#15
The Better Solution
OK I tried your code and it worked OK, except that it didn't allow for normal text selection with the mouse. So I came up with a slightly diffrent method that only selects the text the first time the controll recieves focus. First you will need to change the OnLButtonDown handler in the CFocusEdit class like so
void CFocusEdit::OnLButtonDown(UINT nFlags, CPoint point)
{
if(this != GetFocus())
SetFocus();
else
CEdit::OnLButtonDown(nFlags, point);
}
Then you will need to impliment the OnSetfocus() for the edit controll like before
void CAboutDlg::OnSetfocusEdit1()
{
CEdit* pEC = (CEdit*)GetDlgItem(IDC_EDIT1);
pEC->SetSel(0, -1);
}
Like I said this allows the edit controll to be selected in full the first time it recieves focus, but also allows normal mouse selection by the user if needed.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|