Solved

How to switch CEdit (Show password/Hide password) ?

Posted on 1998-10-25
14
1,925 Views
Last Modified: 2013-11-20
Hi,
I have a dialog box with a password CEdit field. I want to implement a checkbox with one can choose if one want see the  password as asteriks or see the password as it is. How can I send a message to the CEdit field to switch between password and normal text input ? Or must I Create the CEdit dynamically with ES_PASSWORD-style and then if the user want to see the password, I have to delete the CEdit und create it again without ES_PASSWORD-style. Is there an easier solution ?

Bye,
atari
0
Comment
Question by:atari
  • 5
  • 3
  • 3
  • +2
14 Comments
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1323819
All you should have to do is call the SetPasswordChar member function.

1) Make sure you have a CEdit control variable attached to the resource with Class Wizard

2) Call SetPasswordChar at the necessary place within your program.

3) voila!

Here is some other information on the SetPasswordChar function:

Call this function to set or remove a password character displayed in an edit control when the user types text. When a password character is set, that character is displayed for each character the user types.

This member function has no effect on a multiple-line edit control.

When the SetPasswordChar member function is called, CEdit will redraw all visible characters using the character specified by ch.

If the edit control is created with the ES_PASSWORD style, the default password character is set to an asterisk (*). This style is removed if SetPasswordChar is called with ch set to 0.

As you can see, the SetPasswordChar function will take care of the problem for you.

If you have any further questions regarding this problem, please feel free to ask.  Thanks!
0
 

Expert Comment

by:gvnmsunil
ID: 1323820
Hello Atari,

for toggling the style of edit control in a dialog box, you have to do
Add a handler for BN_CLICKED event of check box control and in that handler  write the following
              // in the GetDlgItem, you have to pass the appropriate id's of the
              // checkbox button and edit control
               CButton* pBtn = ( CButton* ) GetDlgItem( IDC_... );
               CEdit* pEdit = ( CEdit* ) GetDlgItem( IDC_.... );
               if ( pBtn->GetState() == 0 )
                        pEdit->ModifyStyle( ES_PASSWORD, 0 );
              else
                        pEdit->ModifyStyle( 0, ES_PASSWORD );

I think this will definetly solve your problem.

Thank You
                 
0
 
LVL 1

Author Comment

by:atari
ID: 1323821
I think, gvnmsunil has given the correct answer, because he had understand my question, so I want to give him the points. Please gvnmsunil answer this question again.

Thanks,
bye,
atari
0
 
LVL 1

Expert Comment

by:jdyer
ID: 1323822
I think that thresher_shark provided an adequate and equivalent answer.

Regards,
  jdyer
0
 

Accepted Solution

by:
gvnmsunil earned 100 total points
ID: 1323823
Hello Atari,

for toggling the style of edit control in a dialog box, you have to do
Add a handler for BN_CLICKED event of check box control and in that handler  write the following
              // in the GetDlgItem, you have to pass the appropriate id's of the
              // checkbox button and edit control
               CButton* pBtn = ( CButton* ) GetDlgItem( IDC_... );
               CEdit* pEdit = ( CEdit* ) GetDlgItem( IDC_.... );
               if ( pBtn->GetState() == 0 )
                        pEdit->ModifyStyle( ES_PASSWORD, 0 );
              else
                        pEdit->ModifyStyle( 0, ES_PASSWORD );

I think this will definetly solve your problem.

Thank You
           
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1323824
That's what I thought, hence the "If you have any further questions, please feel free to ask."  I don't know, if atari wants to give gvnmsunil the points, that's fine with me.
0
 

Expert Comment

by:gvnmsunil
ID: 1323825
Actually, according to official documentation of Microsoft, thresher_shark's answer is the shortest method for your problem. Though mine and thresher_shark's answer's are right, I think he is the correct person to get points because his answer is good and also on the first come first served basis, he should get the points.

Regards,
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:atari
ID: 1323826
Sorry folks, don't worry, I'm a german programmer who has problems with the english language. I didn't read exactly the online-help for explaining SetPasswordChar and so I didn't see that there also can be a parameter ch=0 !!!! to display the original ascii-data the user types in. I thought that this function only changes the asterisk to any other ascii-character. Sometimes I read only very quick in the online-help, because many things aren't described as they should be. Today looked for WNDCLASS-classname for a CEdit-control, because I needed it as a parameter for edit->CreateEx()-function. Searched a lot of time until I found out that the classname was _T("edit") and not as I thought _T("CEdit").

Sorry about that,
don't panic,
you have earned your points, thresher_shark.

Oh I need glasses, gvnmsunil's had answered this question. What shall I do now ?

Sorry thresher_shark, today in the evening, I will ask you a question only for you to give you the points. Please wait !!!!


To gvnmsunil's answer: ModifyStyle(ES_PASSWORD) didn't work for me, it only works with window styles ModifyStyle(WS_VISIBLE and so on. As bool-result I get only 0 for ModifyStyle.
That's another problem, that I didn't understand, but I think that would be a new question.

Bye,
atari
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1323827
Wow, thanks Atari, that's very kind of you, I will look for the question.
0
 
LVL 1

Author Comment

by:atari
ID: 1323828
Hi thresher shark,
I have so much programming work to do, so I will ask you the question first in this evening.

So please wait a little bit, I have to buy new points.

Bye,
atari
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1323829
There is no hurry, take your time :-)
0
 

Expert Comment

by:sirhaden
ID: 36909516
Niether of these suggestions work with a Visual Studio 2010 project...
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 36909540
This thread is from 1998.  I imagine things might have changed :)
0
 

Expert Comment

by:sirhaden
ID: 36909570
Yes, the docs don't state to include the escape for the 0, so this works...

      if(bShowPassword)
      {
            m_Edit.SetPasswordChar(L'\0');
            m_Edit.RedrawWindow();
      }
      else
      {
            m_Edit.SetPasswordChar(L'*');
            m_Edit.RedrawWindow();
      }
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

15 Experts available now in Live!

Get 1:1 Help Now