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

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
LVL 1
atariAsked:
Who is Participating?
 
gvnmsunilCommented:
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
 
thresher_sharkCommented:
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
 
gvnmsunilCommented:
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
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
atariAuthor Commented:
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
 
jdyerCommented:
I think that thresher_shark provided an adequate and equivalent answer.

Regards,
  jdyer
0
 
thresher_sharkCommented:
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
 
gvnmsunilCommented:
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
 
atariAuthor Commented:
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
 
thresher_sharkCommented:
Wow, thanks Atari, that's very kind of you, I will look for the question.
0
 
atariAuthor Commented:
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
 
thresher_sharkCommented:
There is no hurry, take your time :-)
0
 
sirhadenCommented:
Niether of these suggestions work with a Visual Studio 2010 project...
0
 
thresher_sharkCommented:
This thread is from 1998.  I imagine things might have changed :)
0
 
sirhadenCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.