Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 1998-10-25
14
Medium Priority
?
2,476 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
[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
  • 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
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!

 
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 400 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
 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

610 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