Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
i have two textbox in my windows form. can i check the password that they are same? without clicking on the submit button or getting a popup.?
can i do that thing in regex
Posted 6-Oct-12 18:35pm
sariqkhan1.4K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Assuming you have a Form with two TextBoxes on it, textBox1, and textBox2: you want the two TextBoxes to display initial messages in plain-text, like the first one to display: "Enter Password," and the second one to display: "Re-Enter Password."
 
When the user causes either one of the TextBoxes to have 'Focus, and begins typing, you wish the initial plain-text message to disappear, and then whatever the user types will appear as your "masked" password-character, like: "*"
 
When the user hits the "Enter" key in either TextBox, you want to compare their contents: if they match, you are done; if they don't match, you want to reset the two TextBoxes to their initial state.
 
Note: there are two ways for either of the TextBoxes to get 'Focus: one is by the user clicking the mouse inside one of them (MouseDown), and the other is by the user hitting the TabKey to make them the active control that has 'Focus.
 
Now, let's look at some code:
// for convenience
private const char EnterChar = (char) Keys.Enter;
private const char EmptyChar = '\0';
private const char MaskChar = '*';
 
// the default plain-text content
private string text1 = "Enter Password";
private string text2 = "Re-Enter Password";
 
// keep track of whether each TextBox
// is showing plain-text, or is showing
// each key-stroke entered as the "mask"
// these 'flags' are used to make sure we don't
// clear the TextBoxes more than once in
// "password-entry mode"
private bool tb1Virgin = true;
private bool tb2Virgin = true;
 
private void Form1Load(object sender, EventArgs e)
{
    // not really necessary, just a habit to do this
    textBox1.Text = text1;
    textBox2.Text = text2;
    textBox1.PasswordChar = EmptyChar;
    textBox2.PasswordChar = EmptyChar;
}
 
// this will work if you remove these MouseDown EventHandlers
// but I like the idea of auto-clearing the plain-text when the
// user clicks inside the TextBoxes: rather than letting them
// click anywhere in the plain-text and start typing: then clear

private void TextBox1MouseDown(object sender, MouseEventArgs e)
{
    if (textBox1.PasswordChar == EmptyChar)
    {
        textBox1.Clear();
        textBox1.PasswordChar = MaskChar;
        tb1Virgin = false;
    }
}
 
private void TextBox2MouseDown(object sender, MouseEventArgs e)
{
    if (textBox2.PasswordChar == EmptyChar)
    {
        textBox2.Clear();
        textBox2.PasswordChar = MaskChar;
        tb2Virgin = false;
    }
}
 
// clean-up and reset if there's no match
private void PasswordTextBoxesReset()
{
    MessageBox.Show("No Match: try again");
    textBox1.PasswordChar = EmptyChar;
    textBox1.Text = text1;
    tb1Virgin = true;
    textBox2.PasswordChar = EmptyChar;
    textBox2.Text = text2;
    tb2Virgin = true;
}
 
// get an Enter/Return character: check for a match
private void TextBox1KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == EnterChar)
    {
        if (textBox1.Text != textBox2.Text)
        {
            PasswordTextBoxesReset();
        }
        else
        {
            MessageBox.Show("Password Match = " + textBox1.Text);
        }
    }
    else
    {
        // do we need to clear the plain-text ?
        if (tb1Virgin)
        {
            textBox1.Clear();
            textBox1.PasswordChar = MaskChar;
            tb1Virgin = false;
        }
    }
}
 
// get an Enter/Return character: check for a match
private void TextBox2KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == EnterChar)
    {
        if (textBox1.Text != textBox2.Text)
        {
            PasswordTextBoxesReset();
        }
        else
        {
            MessageBox.Show("Password Match = " + textBox1.Text);
        }
    }
    else
    {
        // do we need to clear the plain-text ?
        if (tb2Virgin)
        {
            textBox2.Clear();
            textBox2.PasswordChar = MaskChar;
            tb2Virgin = false;
        }
    }
}
Notes:
 
0. Note that setting the PasswordChar property of a TextBox to '\0' has the effect of making everything typed in the TextBox plain-text.
 
1. The two TextBoxes have the 'MultiLine property set to 'false. Remember that the value of the 'AcceptsReturn property of the TextBoxes doesn't matter here, because they are not 'MultiLine TextBoxes. And, note, that "Enter" and "Return" are the same in .NET.
 
2. You could certainly re-factor this code, and make both textBox1 and textBox2 share the same 'MouseDown, and 'KeyPress, EventHandlers, but, for educational purposes here, each TextBox has its own EventHandlers. And, for something this simple: why bother to re-factor ?
 
3. Obviously, when there is a "match," you'll probably want to hide the TextBoxes, or switch to some other Form, or whatever.
 
4. It would be easy, of course, to "decorate" the two TextBoxes with either Labels, or use a Tool-Tip, to give the user information like the minimum number of characters to enter, whether they must enter at least one number, etc. Other nice touches are: if a match fails, give the user some appropriate feedback based on analysis of what they entered if it doesn't meet your validation criteria, like: "your password must contain at least one number," etc.
  Permalink  
v3
Comments
sariqkhan at 7-Oct-12 5:05am
   
awsome code bro
+5 for be best explanation
but can you tell me something about the program
what is this tbvirgin?
AND
a popup comes when there is capslock on? how this is done? without coding?
AND
private void TextBox2MouseDown(object sender, MouseEventArgs e)
{
if (textBox2.PasswordChar == EmptyChar)
{
textBox2.Clear();
textBox2.PasswordChar = MaskChar;
tb2Virgin = false;
}
}
what does this code will do?
AND
private void TextBox2KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == EnterChar)
{
if (textBox1.Text != textBox2.Text)
{
PasswordTextBoxesReset();
}
else
{
MessageBox.Show("Password Match = " + textBox1.Text);
;
}
}
what will this code will do?
sorry for such question but the problem is. i have to learn before implementation
:(
sariqkhan at 7-Oct-12 6:02am
   
bro i am using this coding with error provider. it checks whether password is correct?
it sets error provider on when there is wrong pasword or password doesnt match
private void button1_Click(object sender, EventArgs e)
{
 

if (textBox1.Text != textBox2.Text)
{
errorProvider1.Dispose();
errorProvider1.SetError(textBox2, "password doesnt match");
MessageBox.Show("Password Matchdoes not match = " + textBox2.Text);
textBox1.Clear();
textBox2.Clear();
}
else
{
MessageBox.Show("Password Match = " + textBox1.Text);
errorProvider1.SetError(textBox1, "");
}
i does not want this in button click event. but i want it to be like this
when i type in the first textbox and when writing in second textbox. if strig dosent match then error provider should set on.
i dont want this in button click. so what can i do?
BillWoodruff at 7-Oct-12 8:00am
   
If you don't want something in a Button Click EventHandler, remove the code :)
 
Your original question mentions neither a Button, or use of the ErrorProvider class. If you have a problem using the ErrorProvider class, please post that as a separate question here, and make sure you include enough code for readers to understand it.
 
Set a break-point in your code, just after you enter the code for the Button Click EventHandler, and examine what happens as you single-step through the code.
 
Think, read theory, study working examples, mess around with working examples, write your own implementation: "repeat until well done:" and use Visual Studio's excellent debugger: that's how you learn :)
 
Think about your strategy here: do you really want to give the user immediate feedback as they enter a mis-matching character while they type: doesn't that defeat the whole purpose of forcing duplicate secure password entry ?. To be really secure, you should disable the ability to copy from / paste into text in either TextBox: that's a whole other issue.
 
good luck, Bill
BillWoodruff at 7-Oct-12 7:41am
   
"what is this tbvirgin?" see the comments in the code which explain the purpose of these flags
 
"a popup comes when there is capslock on?" WindowsForms .NET has decided, for us, that if you are entering text into a TextBox where a PassWordChar has been set and caps-lock is on, that in all cases a warning pop-up should be shown ! :) Note that you can ignore this warning and the example still works fine.
 
"what does this code will do?"
private void TextBox2MouseDown(object sender, MouseEventArgs e) ...
private void TextBox2KeyPress(object sender, KeyPressEventArgs e) ...
 
see detailed comments in the code which explain what they do.
 
"i have to learn before implementation"
 
A sample implementation here is just a starting point for your own understanding, and developing your own solution. You may wish to go for a very different design based on your needs.
 
For example, you may wish to keep track of how many times a user is allowed to enter an incorrect password, and then refuse to allow them to try again after a certain number of attempts.
 
You may wish to not let the user enter text into the second textbox until they have taken some action to indicate they have finished their entry in the first textbox.
 
May I suggest, as an exercise, that you write down a complete outline (not code) of exactly how you want the two TextBoxes to look initially, how you want them to respond as the user enters candidate password characters, etc. Then implement that in code yourself, using whatever works for you in the code from the example given here.
sariqkhan at 7-Oct-12 8:14am
   
thanks bro i got it.
:)
done my work
thanks a lot
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

can i check the password that they are same
Yes
 
can i do that thing in regex
Yes. But, you don't need to, since a direct comparison is possible and will do. You are asking for Winforms, there is no client-server different place to check for it. Event can be raised in code behind for the comparison.
 
Try:
1. Put a onlostfocus event on the second textbox
2. In the event, compare the passwords directly itself (regex not required)
3. Based on the comparison result display messagebox or a message text
 
Refer: MSDN: Control.OnLostFocus Method[^]
Example: MSDN: Control.LostFocus Event[^]
  Permalink  
v3
Comments
qureshiaquib at 7-Oct-12 2:06am
   
3
Sandeep Mewara at 7-Oct-12 2:43am
   
And the reason?
sariqkhan at 7-Oct-12 2:08am
   
+5 for explanation
bro. but i dont know much about coding about how to create a onblur or on focus out event. can you provide the example.
please
Sandeep Mewara at 7-Oct-12 2:46am
   
Updated my answer with more details and sample link.
sariqkhan at 7-Oct-12 5:06am
   
thanks. bro
reallly helped that one
:)
+5
sariqkhan at 7-Oct-12 6:01am
   
bro i am using this coding with error provider. it checks whether password is correct?
it sets error provider on when there is wrong pasword or password doesnt match
private void button1_Click(object sender, EventArgs e)
{
 

if (textBox1.Text != textBox2.Text)
{
errorProvider1.Dispose();
errorProvider1.SetError(textBox2, "password doesnt match");
MessageBox.Show("Password Matchdoes not match = " + textBox2.Text);
textBox1.Clear();
textBox2.Clear();
}
else
{
MessageBox.Show("Password Match = " + textBox1.Text);
errorProvider1.SetError(textBox1, "");
}
i does not want this in button click event. but i want it to be like this
when i type in the first textbox and when writing in second textbox. if strig dosent match then error provider should set on.
i dont want this in button click. so what can i do?
sariqkhan at 7-Oct-12 8:15am
   
i got it. done with by small brain
:)
done my work
thanks a lot. your event helped me understand whats the matter with textbox. link was quite tough but at last.
it clicked
Sandeep Mewara at 7-Oct-12 8:17am
   
Super. No issues. :)
sariqkhan at 7-Oct-12 8:25am
   
can you provide me information about datagrid view?
how to filter the data present in data grid view?
i mean i have a date column in my table and i want user to search by particular date or month or year. how can i do that?
can you help me please?
Sandeep Mewara at 7-Oct-12 8:40am
   
Try this:
http://www.codeproject.com/Articles/19182/Datagridview-with-filtering-capability
 
If you face issues, post a new question.
sariqkhan at 7-Oct-12 8:49am
   
thanks thanks.
:)
the best day of mine
thanks a lot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 383
1 Richard Deeming 180
2 ChauhanAjay 176
3 Sergey Alexandrovich Kryukov 171
4 Yogesh Kumar Tyagi 165
0 Sergey Alexandrovich Kryukov 6,252
1 OriginalGriff 5,498
2 CPallini 2,473
3 Richard MacCutchan 1,627
4 Abhinav S 1,530


Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 7 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100