locked
textbox that only accepts enter RRS feed

  • Question

  • As mentioned in the title i would like textboxt to accept only enter button when user enter the code in which picture.i have created the code given but when i type something in textbox it passes "if" and then goes to else messagebox

    what should i do ?

     

     

    private
    
     void
    
     CaptchaText_KeyDown(object
    
     sender, KeyEventArgs e)
     {
     _result = CaptchaText.Text;
    
     if
    
     (CaptchaText.Text == _result && e.KeyValue=13)
      {
    
      SonAc.Show();//opens another form
    
    
      this
    .Hide();
      }
    
      else
    
    
      {
      MessageBox.Show("The code invalid"
    
    );
      DrawBox();//refreshes code in the picture
    
    
      }
     }
    

     

     

    Friday, May 7, 2010 2:23 PM

Answers

  • change code to keyPress event. And CaptchaText.Text == _result this condition will always true. As your per current code, if you type anything  other than 'ENTER' Key it will go to else part.


    Thanks Mike -------- Mark best answers as Answer ----------
    • Proposed as answer by Helen Zhou Monday, May 10, 2010 6:46 AM
    • Marked as answer by Helen Zhou Friday, May 14, 2010 2:02 AM
    Friday, May 7, 2010 3:42 PM
  • Hello,

    you should store the random number that you are displaying, for instance on your custom picturebox or even as a field on the form and compare user input with it (the _result field looks like you might be storing it there but if so, you can't overwrite it with textbox text each time). here is an example:

    	private void captchaText_KeyDown(object sender, KeyEventArgs e)
    	{
    		// compare user input with random text only if ENTER has been pressed
    		if (e.KeyCode == Keys.Return || e.KeyCode == Keys.Enter)
    		{
    			if (captchaPictureBox.RandomText == captchaText.Text) // where captchaPictureBox.RandomText is the text displayed on the picturebox
    			{
    				SonAc.Show(); //opens another form
    				this.Hide();
    			}
    			else
    			{
    				MessageBox.Show("The code invalid");
    				DrawBox();//refreshes code in the picture
    			}
    		}
    	}
    

    And please use </> button when you post code so that it's displayed in readable way.

    Best regards,
    Vladimir

     

    • Proposed as answer by Helen Zhou Monday, May 10, 2010 6:46 AM
    • Marked as answer by Helen Zhou Friday, May 14, 2010 2:03 AM
    Friday, May 7, 2010 4:31 PM

All replies

  • private
    
     void
    
     CaptchaText_KeyDown(object
    
     sender, KeyEventArgs e)
     {
     _result = CaptchaText.Text;
    
     if
    
     ((CaptchaText.Text == _result) && (e.KeyValue==13))
     {
    
     SonAc.Show();//opens another form
    
    
     this.Hide();
     }
     else
     {
     MessageBox.Show("The code invalid");
     DrawBox();//refreshes code in the picture
    
    
     }
     }
    
    Value comparison should be done with '==' not '='
    Thanks Mike -------- Mark best answers as Answer ----------
    Friday, May 7, 2010 2:38 PM
  • thank you but i wrote the value comparison "=" by mistake its not the exact problem.
    Friday, May 7, 2010 3:04 PM
  • change code to keyPress event. And CaptchaText.Text == _result this condition will always true. As your per current code, if you type anything  other than 'ENTER' Key it will go to else part.


    Thanks Mike -------- Mark best answers as Answer ----------
    • Proposed as answer by Helen Zhou Monday, May 10, 2010 6:46 AM
    • Marked as answer by Helen Zhou Friday, May 14, 2010 2:02 AM
    Friday, May 7, 2010 3:42 PM
  • Hello,

    you should store the random number that you are displaying, for instance on your custom picturebox or even as a field on the form and compare user input with it (the _result field looks like you might be storing it there but if so, you can't overwrite it with textbox text each time). here is an example:

    	private void captchaText_KeyDown(object sender, KeyEventArgs e)
    	{
    		// compare user input with random text only if ENTER has been pressed
    		if (e.KeyCode == Keys.Return || e.KeyCode == Keys.Enter)
    		{
    			if (captchaPictureBox.RandomText == captchaText.Text) // where captchaPictureBox.RandomText is the text displayed on the picturebox
    			{
    				SonAc.Show(); //opens another form
    				this.Hide();
    			}
    			else
    			{
    				MessageBox.Show("The code invalid");
    				DrawBox();//refreshes code in the picture
    			}
    		}
    	}
    

    And please use </> button when you post code so that it's displayed in readable way.

    Best regards,
    Vladimir

     

    • Proposed as answer by Helen Zhou Monday, May 10, 2010 6:46 AM
    • Marked as answer by Helen Zhou Friday, May 14, 2010 2:03 AM
    Friday, May 7, 2010 4:31 PM
  • Thanks for answer but i didnt use captchapicturebox it doesnt still work here is my code:

     

     

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing.Text;
    
    
    namespace CaptchaSample
    {
     
     public partial class FormMain : Form
     {
     
    
     Son SonAc = new Son();
    
     public FormMain()
     {
     InitializeComponent();
     }
    
     private void FormMain_Load(object sender, EventArgs e)
     {
     DrawBox();
     
     }
    
     
     private void DrawBox()
     {
    
     Image _im = new Bitmap(pictureBox1.Width, pictureBox1.Height);
     Graphics _g = Graphics.FromImage(_im);
     PrivateFontCollection _pfc = new PrivateFontCollection();
     _pfc.AddFontFile("C:\\latin.ttf");
     _g.Clear(Color.FromArgb(0, 0xff, 0xff, 0xff));
     _g.DrawString(GetRandomText(3),
     new Font(_pfc.Families[0], 24F),
     Brushes.Black,
     new PointF(0, 0));
     pictureBox1.Image = _im;
     
     }
     string _result = string.Empty;
    
     string alpha = "AzByCxDwEvFuGtHsIrJqKpLoMnNmOlPkQjRiShTgUfVeWdXcYbZa1234567890";
     
     private string GetRandomText(int Length)
     {
    
     string _sonuc = string.Empty;
     _result=_sonuc;
     Random r = new Random();
     for (int i = 0; i < Length; i++)
     _result += alpha.Substring(r.Next(alpha.Length),1);
     return _result;
    
    
     
     
     
    
     }
    
     private void button1_Click(object sender, EventArgs e)
     {
     DrawBox();
     }
    
     private void pictureBox1_Click(object sender, EventArgs e)
     {
     
    
     }
    
     
    
     private void Cikis2_Click(object sender, EventArgs e)
     {
     DialogResult Secim = new DialogResult();
    
     Secim = MessageBox.Show("Çıkmak istediğinizden emin misiniz?", "Çıkış", MessageBoxButtons.YesNo);
    
     if (Secim == DialogResult.Yes)
     {
    
     Application.Exit();
    
     }
     }
    
     private void captchaText_KeyDown(object sender, KeyEventArgs e)
    
    
     {
      if (e.KeyCode == Keys.Return || e.KeyCode == Keys.Enter)
     {
     if (_result ==CaptchaText.Text)  SonAc.Show(); //opens another form
      this.Hide();
     }
     else
     {
      MessageBox.Show("The code invalid");
      DrawBox();//refreshes code in the picture
     }
     }
     }

     

    Friday, May 7, 2010 4:47 PM
  • Why didn't you try the example I posted above ... don't make any changes ... the point is that you test input only if user pressed ENTER.

    	private void captchaText_KeyDown(object sender, KeyEventArgs e)
    	{
    		if (e.KeyCode == Keys.Return || e.KeyCode == Keys.Enter)
    		{
    			if (_result == CaptchaText.Text)
    			{
    				SonAc.Show(); //opens another form
    				this.Hide();
    			}
    			else
    			{
    				MessageBox.Show("The code invalid");
    				DrawBox();//refreshes code in the picture
    			}
    		}
    	}
    

     

     

    Friday, May 7, 2010 7:21 PM
  • Is the event raised when you press the enter button ?

     

    The documentation says this: Certain keys, such as the TAB, RETURN, ESCAPE, and arrow keys are handled by controls automatically. To have these keys raise the KeyDown event, you must override the IsInputKey method in each control on your form.

    http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown.aspx

    Saturday, May 8, 2010 7:55 PM
  • Thank you i have found a solution
    Saturday, May 8, 2010 8:22 PM
  • Please mark the best replies as answers, or if none of them helped you, please post the solution (in any form that you can) for the benefit of other community members who may face a similar problem in the future.

    thanks,
    Vladimir

    Monday, May 10, 2010 10:08 AM