4 Replies - 1484 Views - Last Post: 03 December 2011 - 12:45 PM Rate Topic: -----

#1 marwaa  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 24
  • Joined: 29-November 10

how i can enter only four digits in the textbox?

Posted 03 December 2011 - 06:53 AM

The client can enter only four digits in the textbox wich represent the PIN (the text box is not allowed to enter more or less than four digits) . After the client entered four digits the textbox will be read only



My problem :When I put one digite then the text box becomes read-only, i want to enterd only 4 digite ..



Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar Like "[0_9]" Or Asc(e.KeyChar) Like 8 Then
            e.Handled = False
        Else
            e.Handled = True
        End If
      
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If Not TextBox1.Text = "" Then
            TextBox1.ReadOnly = True
        Else
            TextBox1.ReadOnly = False
        End If
    End Sub

This post has been edited by marwaa: 03 December 2011 - 06:55 AM


Is This A Good Question/Topic? 0
  • +

Replies To: how i can enter only four digits in the textbox?

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,361
  • Joined: 02-June 10

Re: how i can enter only four digits in the textbox?

Posted 03 December 2011 - 07:38 AM

You need to check the length of the text of the textbox, on the .TextChanged event.

Here's what should happen...

  • The text is changed, can be a keypress, deletion, copy/paste...
  • You check the length of the text in the box.
  • If it is less than 4, don't react
  • If it is 4 or more, trim it to 4


Try coding up that logic and let us know if you have any trouble with it.

This post has been edited by tlhIn`toq: 03 December 2011 - 07:39 AM

Was This Post Helpful? 0
  • +
  • -

#3 marwaa  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 24
  • Joined: 29-November 10

Re: how i can enter only four digits in the textbox?

Posted 03 December 2011 - 08:26 AM

:(
is not working



in public
dim length as integer=4



my code
  Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If TextBox1.Text = length Then
            ' If Not TextBox1.Text = "" Then
            TextBox1.ReadOnly = True
        Else
            MsgBox("Enter only foud digites")
            TextBox1.ReadOnly = False
        End If
    End Sub

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,361
  • Joined: 02-June 10

Re: how i can enter only four digits in the textbox?

Posted 03 December 2011 - 08:34 AM

What does "not working" mean?
Errors... crashing... unexpected behavior... not setting the textbox to read only on the 4th character

are you sure you subscribed to the .TextChanged event? If you put a breakpoint at line 2 is it hit?
Was This Post Helpful? 0
  • +
  • -

#5 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 563
  • View blog
  • Posts: 2,693
  • Joined: 24-July 11

Re: how i can enter only four digits in the textbox?

Posted 03 December 2011 - 12:45 PM

Ok. Set the text box maxLength property to 4 in the designer.
Then in the TextChanged event:
If myBox.Text.Length = 4 then
Mybox.ReadOnly = True
End If


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1