Results 1 to 15 of 15

Thread: help stopping the same values in a textbox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Posts
    21

    help stopping the same values in a textbox

    Hello everyone,

    i need some help, i have 6 text boxes, which all have numbers inputted into them.

    i need to make it so that, if a user enters the number '1' in textbox1, and then in textbox2 then try and enter the number '1' again it won't allow it.

    Basically i need to make it so that the same number cannot be inputted again.

    Any help on this would be very gratful

    Thanks

    Homer

  2. #2
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    you could use a Select Case.

    VB Code:
    1. select case true
    2.  
    3. case {this box}.text = textbox1.text
    4.  {this box}.text = ""
    5. case {this box}.text = textbox2.text
    6.  {this box}.text = ""
    7.  
    8. .
    9. .
    10. .
    11. .
    12. end select

    I THINK that would be efficient enough. You could actually make a function to make it better....

    VB Code:
    1. public function IsSame(Box1 as textbox, Box2 as textbox) as boolean
    2. if box1.text = box2.text then
    3. box1.text = ""
    4. return true
    5. end if
    6.  
    7. End function

    That's just the jist of it. When you pass your boxs' names into the function, it checks for equality and if it's a match the first box is set to blank. You could take it a bit further and throw a message to the user when 'True' is met.

  3. #3
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    The Select Case route suggested is far too long. The shortest way to do this is to use listboxes instead of textboxes.

    1. Fill an array with the strings of the required numbers
    2. Fill all the listboxes from the array.
    3. When the user selects one of the numbers, remove that number from the array and re fill those listboxes which have not yet been selected.


    If you want to use textboxes, then you can use a two dimension array; the first dimension address will correspond to the number and the second dimension will hold a marker to show if it has been selected.

    Whenever a number is selected you use something like


    Number selected 5 in textbox1

    VB Code:
    1. Dim array1(6,1) as Integer
    2. Dim iSelect as integer
    3.  
    4. iSelect =val(textbox1.text)
    5. if array1(iSelect-1,0)=0 then
    6.   Array1(iSelect-1,0)=1
    7. else
    8.   Messagebox.show("The Number  " & iSelect & " has already been selected")
    9.  textbox1.text=""
    10. end if

    If you are familiar with non zero based arrays then you could save a little on the above code.

    If you want to cover the operator changing the selection, you need to keep track of selections made by using another two dimension array, the first dimension of which will represent each individual textbox and the second will hold the actual number selected.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  4. #4
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    This is another way, good if you use not too many textboxes. I used from TextBox2 to TextBox5.
    You have to modify, for example, TextBox2_KeyPress event subroutine:

    VB Code:
    1. Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress, TextBox3.KeyPress, TextBox4.KeyPress, TextBox5.KeyPress
    2.         If Char.IsLetterOrDigit(e.KeyChar) Then
    3.             Dim NewText As String = sender.text & e.KeyChar
    4.             If (NewText = TextBox2.Text Or NewText = TextBox3.Text Or NewText = TextBox4.Text Or NewText = TextBox5.Text) Then
    5.                 e.Handled = True
    6.             End If
    7.         End If
    8.     End Sub
    This code prevents to input the wrong number and not delete previous digits if any.
    Example: If TextBox1 contains "45894"
    TextBox4 will stop your input at "4589"

    You can also add a messagebox, as Taxes rightly have suggested.
    Good Job.
    Live long and prosper (Mr. Spock)

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Posts
    21
    Thats great ill give it ago, do you know how i could limit the numbers so only numbers 1 to 49 can be entered and no more?

    Thanks

  6. #6
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Oooops...I didn't advise...you need to use fixed lenght string to avoid problems. If you need to use numbers from 1 until 49, than you need to write, for example, '03' in place of only '3'. When digits are two, there is no problem. Obviously you can test also if number is greater than 49. Sincerely....if you need a complete control, you have to limit input at only digits (this is quite easy), then you have to avoid 'Ctrl+V' way to put in your textbox something from clipboard; it's a little more complicated, but I have a quite simple solution. It depends on how much you want to protect your textboxes. Let's know what you need. If not me, surely, other more experienced friends, will be able to solve your problems. Good job and good night (It's midnight in Italy!)

    Live long and prosper (Mr. Spock)

  7. #7
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    "Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress, TextBox3.KeyPress, TextBox4.KeyPress, TextBox5.KeyPress
    If Char.IsLetterOrDigit(e.KeyChar) Then
    Dim NewText As String = sender.text & e.KeyChar
    If (NewText = TextBox2.Text Or NewText = TextBox3.Text Or NewText = TextBox4.Text Or NewText = TextBox5.Text) Then
    e.Handled = True
    End If
    End If
    End Sub"

    I have not tried it but are you sure the above works? Doesn't NewText double up each entry? (Although the actual textbox.text will be accurate the comparison will always be wrong) Also if a two digit number is being inserted for the first time, this approach will show a duplication if the first digit already exists in another textbox.

    Perhaps it is easier to validate all the textbox entries in one go when they have all been completed. Either way I think you will only be able to do this economically with my array approach (just increase the first dimension of the array to cover your highest number)
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  8. #8
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Dear Taxes, if understand what you are saying, perhaps you are underlining what I said in my second post. Anyway, I tried it about 20 seconds. Yesterday (and today, also) I had a very hard day! It should be integrated and correct in many ways. I know that a complete control on a texbox is not so easy as it could seem at a first look and generally it requests to write code in more than event routine. The easiest way, is surely to press a button when all games (on textboxes) are over and to have a final check, I agree! But you can try, also, a real time control like I proposed. It's pretty, isn't it? A complete real time control it's possible, but it is too complicate. I could write it, but it should be very difficult to pass all code and explain it. So I think you can use a combination. A not complete protection in real time and a final validation.....
    Live long and prosper (Mr. Spock)

  9. #9
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382
    Here's what I would do, works great.

    You MUST give each textbox you want to compare a Tag of "TextBox" in order for this to work...

    Code:
        Public Sub CheckBox(ByVal BoxToCompare As TextBox)
    
            Dim C As Control
            SyncLock Me.Controls
                For Each C In Me.Controls
                    If C.Tag = "TextBox" AndAlso Not (BoxToCompare Is C) Then
                        If C.Text.Trim.ToUpper = BoxToCompare.Text.Trim.ToUpper Then
                            BoxToCompare.Clear()
                        End If
                    End If
                Next
            End SyncLock
    
        End Sub

    Hope that helps

  10. #10
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi Hinder,

    That looks great

    (I have edited out this part as I was wrong. )

    Why do you use SyncLock here, it works OK without? I thought that was for use between multiple threads
    Last edited by taxes; May 19th, 2004 at 11:55 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  11. #11
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382
    Originally posted by taxes

    Why do you use SyncLock here, it works OK without? I thought that was for use between multiple threads

    Well because enumerating through any collection, threaded or not is dangerous if another portion of application try's to make changes to a collection. It's just for extra protection, you can take it out if you want but I usually use SyncLock since it doesn't hurt anything...

  12. #12
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Thanks Hinder
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  13. #13
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Intelligent use of TAG, Hinder!
    Because I started something (damned that moment ), I tried to complete the work. I said something about a real time way to control your input amd I propose an 'embrional'(is it right?) code. Then I had an hour to spent in masochistic programming and so I tried to add something. When I finished, I didn't post it, because I can't explain it well in english.Today I change my mind: perhaps it could give some help to someone, newbie like me, especially because in VB.NET there is not a native 'MaskedEdit'. I saw many threads in which someone ask how to control input in a textbox. The most sensated solutions remains yours, obviously. Mine is only an example of a particular code application, no more.
    I'm not completely sure it's 100% bug free, but It seems sufficently affordable. If someone want to try it, he has to create a form, put in it 6 textboxes, leaving them their default names, then copy the code and run.
    It SHOULD control input against CTRL+V insert intrusion, value > 49 and duplicate value, deleting only invalid input, giving an appropriate message and putting the cursor in the right consequent position (IT SHOULD, I repeat )
    I hope this code can suggest some way to face the problem of masking textbox.....as always, excuse my english
    VB Code:
    1. Private Sub FrmXProve2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         'Set property readonly=true, maxlenght=2, text="" and backcolor=white to all textboxes
    3.         'Obviously, it could be done also during the design operation
    4.  
    5.         TextBox1.Text = ""
    6.         TextBox2.Text = ""
    7.         TextBox3.Text = ""
    8.         TextBox4.Text = ""
    9.         TextBox5.Text = ""
    10.         TextBox6.Text = ""
    11.  
    12.         TextBox1.ReadOnly = True
    13.         TextBox2.ReadOnly = True
    14.         TextBox3.ReadOnly = True
    15.         TextBox4.ReadOnly = True
    16.         TextBox5.ReadOnly = True
    17.         TextBox6.ReadOnly = True
    18.  
    19.         TextBox1.BackColor = System.Drawing.Color.White
    20.         TextBox2.BackColor = System.Drawing.Color.White
    21.         TextBox3.BackColor = System.Drawing.Color.White
    22.         TextBox4.BackColor = System.Drawing.Color.White
    23.         TextBox5.BackColor = System.Drawing.Color.White
    24.         TextBox6.BackColor = System.Drawing.Color.White
    25.  
    26.         TextBox1.MaxLength = 2
    27.         TextBox2.MaxLength = 2
    28.         TextBox3.MaxLength = 2
    29.         TextBox4.MaxLength = 2
    30.         TextBox5.MaxLength = 2
    31.         TextBox6.MaxLength = 2
    32.  
    33.     End Sub
    34.  
    35.     Private Sub TestoVariato(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged, TextBox4.TextChanged, TextBox5.TextChanged, TextBox6.TextChanged
    36.  
    37.         Static Done As Boolean = False
    38.         Static CursPos As Byte = 0
    39.         Dim Casella As TextBox = sender
    40.  
    41.         If Done = False Then
    42.             If IsNumeric(Casella.Text) Then
    43.                 If Byte.Parse(Casella.Text) > 49 Then
    44.                     MsgBox("Max value permitted is 49")
    45.                     Done = True
    46.                     CursPos = Casella.SelectionStart
    47.                     Casella.Undo()
    48.                 Else
    49.                     Casella.ClearUndo()
    50.                     Done = False
    51.                 End If
    52.             End If
    53.         Else
    54.             Casella.ClearUndo()
    55.             Done = False
    56.             Casella.SelectionStart = CursPos - 1
    57.         End If
    58.  
    59.         sender.readonly = True
    60.  
    61.     End Sub
    62.  
    63.     Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress, TextBox2.KeyPress, TextBox3.KeyPress, TextBox4.KeyPress, TextBox5.KeyPress, TextBox6.KeyPress
    64.  
    65.         Dim casella As TextBox = sender
    66.         Dim NewText As String
    67.  
    68.         If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar) Or e.KeyChar.Equals(Keys.Delete)) Then
    69.             sender.ReadOnly = True
    70.             Exit Sub
    71.         Else
    72.             If casella.SelectionStart = 0 Then
    73.                 NewText = e.KeyChar & casella.Text
    74.                 If NewText.Length > 2 And Char.IsDigit(e.KeyChar) Then
    75.                     e.Handled = True
    76.                     Exit Sub
    77.                 End If
    78.             Else
    79.                 NewText = casella.Text & e.KeyChar
    80.                 If NewText.Length > 2 And Char.IsDigit(e.KeyChar) Then
    81.                     e.Handled = True
    82.                     Exit Sub
    83.                 End If
    84.             End If
    85.  
    86.             If Not (NewText = TextBox1.Text Or NewText = TextBox2.Text Or NewText = TextBox3.Text Or NewText = TextBox4.Text Or NewText = TextBox5.Text Or NewText = TextBox6.Text) Then
    87.                 sender.ReadOnly = False
    88.             Else
    89.                 MsgBox(NewText & " is already digited! Choose another value")
    90.             End If
    91.             End If
    92.  
    93.     End Sub
    94.  
    95.     Private Sub TextBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave, TextBox2.Leave, TextBox3.Leave, TextBox4.Leave, TextBox5.Leave, TextBox6.Leave
    96.  
    97.         sender.text = sender.Text.PadLeft(2, "0")
    98.  
    99.     End Sub
    100.  
    101.     Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown, TextBox2.KeyDown, TextBox3.KeyDown, TextBox4.KeyDown, TextBox5.KeyDown, TextBox6.KeyDown
    102.  
    103.         Dim Valore As Int16 = e.KeyValue
    104.  
    105.         If (Valore = 46) Then
    106.             sender.ReadOnly = False
    107.         End If
    108.  
    109.     End Sub
    Live long and prosper (Mr. Spock)

  14. #14
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Exorcist has just posted this in another thread as code to search out each textbox on a form

    VB Code:
    1. For Each ctrl as Control in Me.Controls
    2.     if TypeOf ctrl Is Textbox then
    3.        'Do something
    4.     end if
    5. Next

    Combining that with Hinder's solution you get

    VB Code:
    1. Public Sub CheckBox(ByVal BoxToCompare As TextBox)
    2.  
    3.          SyncLock Me.Controls
    4.             For Each Ctrl In Me.Controls
    5.                 If TypeOf ctrl Is Textbox  AndAlso Not (BoxToCompare Is Ctrl) Then
    6.                     If C.Text.Trim.ToUpper = BoxToCompare.Text.Trim.ToUpper Then
    7.                         BoxToCompare.Clear()
    8.                     End If
    9.                 End If
    10.             Next
    11.         End SyncLock
    12.  
    13.     End Sub
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  15. #15
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    I have used several times 'for - each' in form controls collection. Often the problem is on which control to act. Hinder solution is good, from my point of view, because you can have 8 textboxes, but only 6 have to be tested and modified. So you have, at least, to ways: The first is to compare control type and also control names, (opportunely assigned), with a list or a dinamically built string (I used this stupid method, until now), or using tag property. Perhaps there are also other ways, obviously.
    Good week end to all, dear friends.
    Live long and prosper (Mr. Spock)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width