Replacing special characters in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Madmartigan
    New Member
    • Dec 2006
    • 23

    Replacing special characters in C#

    Hi all

    I am trying to figure out how to replace a passwordchar of '*' in a windows form textbox, with a valid character entry entered by a user.

    Using Sharpdevelop and the textbox is already instantiated with a randomly generated word. I'm now trying to capture a keypress event in a second textbox and if any of the characters entered matches that of textbox1 I'd like to reveal the character in place of the passwordchar '*".

    Code:
    void TextBox2KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
            {
            	
            char l = e.KeyChar;
            string upper = l.ToString().ToUpper();
            l = upper[0];
    		
    
                   if (TheGuessManager.GotALetter(l) == true)
    	      {
    	      // this works but it changes every PassWordChar to the      
                          // correctly guessed letter					      textBox1.PasswordChar = e.KeyChar;
    				
    	      }
            }
  • Madmartigan
    New Member
    • Dec 2006
    • 23

    #2
    Hi

    I have attempted this with the follwoing method but am still having no joy.

    I have two textboxes. One contains a databinding for a random word that is generated. The other textbox is linked to the first through keypress events(see below).

    The characters are hidden(password char set to '*') in textbox1. I would like the hidden characters to change from '*' to the respective keypress event(character ) in textbox2, if the character is contained in the hidden word in textbox2.

    Is the property of passwordchar on a textbox able to be manipulated on individual characters in that textbox?

    This is for a Hangman game I am trying to develop.

    Thanks for any help.


    Code:
    void TextBox2KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
            {
            	
            	char l = e.KeyChar;
    			
    			if (UserGuess.GotALetter(l) == true)
    			{
    				int intResult;
    				string str1 = textBox1.Text;
    				intResult = str1.IndexOf(l);
    				
    				
    				StringBuilder sb = new StringBuilder(str1);
    				
    				 
    				for (int i = 0; i < sb.Length; i++)
    				{	
    					sb.Replace('*', l, intResult, 1);
    			    	string str = sb.ToString();
    					this.textBox1.Text = str;
    				}
    			}
    }

    Comment

    • LorenVS
      New Member
      • Mar 2008
      • 8

      #3
      Hello,

      Alright, I think I understand what you're trying to do... I'm not quite sure how your form is set up, so here is what I believe your function should look like:

      I believe the problem you are having is because you are trying to use passwordchar, which is less than a great idea, since you're trying to have some characters visible. A better bet would be to initial fill the entire textbox with '*'s manually and disable the textbox...

      Code:
      string password = "* **** * *****";
      string decrypted = "I LOVE C SHARP";
      
      protected void Initialize() // or some other startup function
      {
             PasswordTB.Text = password;
             PasswordTB.Enabled = false;
      }
      protected void KeyPress(object s, KeyPressEventArgs e)
      {
             char PressedLetter = e.KeyChar; //Or whatever the property is
      
             for(int i = 0; i < decrypted.Length; i++)
             {
                  if(decrypted[i] == PressedLetter)
                        password[i] = PressedLetter;
             }
      
             PasswordTB.Text = password;
      }
      You could then update your text box's accordingly

      Comment

      • Madmartigan
        New Member
        • Dec 2006
        • 23

        #4
        Loren, You are a champ!!!

        I've been so taken in by trying to change the property of PasswordChar that I didn't think of an alternative. Anyhow, I've now amended the code as below and it works a treat. Thanks a million....been stuck on this for 3 days :)

        My next step is to get the second loop to work the same as the first if the user opts to play a second game. Feel like a weight has been taken off. Owrooha.

        Code:
               private void Hidden_Load(object sender, System.EventArgs e) 
                {
                	textBox1.DataBindings.Add("text", UserGuess.GetSecretWord(), null);
              		
                	StringBuilder sa = new StringBuilder(textBox1.Text);
                	
                	for(int i = 0; i < sa.Length; i++)
                	{
                		sa[i] = '*';
                		
                	}
                	
                	textBox1.Text = sa.ToString();
                	textBox1.Enabled = false;
                	
                }
        
               void TextBox2KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
                {
                	
                	char l = e.KeyChar;
        			
        			if (UserGuess.GotALetter(l) == true)
        			{
        				// get the password length and characters '*'
        				StringBuilder sb = new StringBuilder(textBox1.Text);
        				StringBuilder sc = new StringBuilder(this.UserGuess.GetSecretWord());
         				
               			for(int i = 0; i < sc.Length; i++)
              			 {
                   			 if(sc[i] == l)
                         	 sb[i] = l;
               			 }
         
               			textBox1.Text = sb.ToString().ToUpper();
        					
        
                  }
        Had to use StringBuilder instaed cause it kept complaining that the index was read only. Fair game. Thanks again.

        Comment

        Working...