|
-
May 18th, 2004, 07:18 AM
#1
Thread Starter
Junior Member
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
-
May 18th, 2004, 07:36 AM
#2
Frenzied Member
you could use a Select Case.
VB Code:
select case true
case {this box}.text = textbox1.text
{this box}.text = ""
case {this box}.text = textbox2.text
{this box}.text = ""
.
.
.
.
end select
I THINK that would be efficient enough. You could actually make a function to make it better....
VB Code:
public function IsSame(Box1 as textbox, Box2 as textbox) as boolean
if box1.text = box2.text then
box1.text = ""
return true
end if
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.
-
May 18th, 2004, 09:19 AM
#3
PowerPoster
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:
Dim array1(6,1) as Integer
Dim iSelect as integer
iSelect =val(textbox1.text)
if array1(iSelect-1,0)=0 then
Array1(iSelect-1,0)=1
else
Messagebox.show("The Number " & iSelect & " has already been selected")
textbox1.text=""
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.
-
May 18th, 2004, 09:45 AM
#4
Hyperactive Member
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:
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
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)
-
May 18th, 2004, 11:53 AM
#5
Thread Starter
Junior Member
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
-
May 18th, 2004, 05:03 PM
#6
Hyperactive Member
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)
-
May 19th, 2004, 05:16 AM
#7
PowerPoster
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.
-
May 19th, 2004, 06:48 AM
#8
Hyperactive Member
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)
-
May 19th, 2004, 08:24 AM
#9
Hyperactive Member
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
-
May 19th, 2004, 10:25 AM
#10
PowerPoster
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.
-
May 19th, 2004, 08:04 PM
#11
Hyperactive Member
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...
-
May 19th, 2004, 08:17 PM
#12
PowerPoster
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.
-
May 20th, 2004, 03:22 AM
#13
Hyperactive Member
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:
Private Sub FrmXProve2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Set property readonly=true, maxlenght=2, text="" and backcolor=white to all textboxes
'Obviously, it could be done also during the design operation
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox1.ReadOnly = True
TextBox2.ReadOnly = True
TextBox3.ReadOnly = True
TextBox4.ReadOnly = True
TextBox5.ReadOnly = True
TextBox6.ReadOnly = True
TextBox1.BackColor = System.Drawing.Color.White
TextBox2.BackColor = System.Drawing.Color.White
TextBox3.BackColor = System.Drawing.Color.White
TextBox4.BackColor = System.Drawing.Color.White
TextBox5.BackColor = System.Drawing.Color.White
TextBox6.BackColor = System.Drawing.Color.White
TextBox1.MaxLength = 2
TextBox2.MaxLength = 2
TextBox3.MaxLength = 2
TextBox4.MaxLength = 2
TextBox5.MaxLength = 2
TextBox6.MaxLength = 2
End Sub
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
Static Done As Boolean = False
Static CursPos As Byte = 0
Dim Casella As TextBox = sender
If Done = False Then
If IsNumeric(Casella.Text) Then
If Byte.Parse(Casella.Text) > 49 Then
MsgBox("Max value permitted is 49")
Done = True
CursPos = Casella.SelectionStart
Casella.Undo()
Else
Casella.ClearUndo()
Done = False
End If
End If
Else
Casella.ClearUndo()
Done = False
Casella.SelectionStart = CursPos - 1
End If
sender.readonly = True
End Sub
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
Dim casella As TextBox = sender
Dim NewText As String
If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar) Or e.KeyChar.Equals(Keys.Delete)) Then
sender.ReadOnly = True
Exit Sub
Else
If casella.SelectionStart = 0 Then
NewText = e.KeyChar & casella.Text
If NewText.Length > 2 And Char.IsDigit(e.KeyChar) Then
e.Handled = True
Exit Sub
End If
Else
NewText = casella.Text & e.KeyChar
If NewText.Length > 2 And Char.IsDigit(e.KeyChar) Then
e.Handled = True
Exit Sub
End If
End If
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
sender.ReadOnly = False
Else
MsgBox(NewText & " is already digited! Choose another value")
End If
End If
End Sub
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
sender.text = sender.Text.PadLeft(2, "0")
End Sub
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
Dim Valore As Int16 = e.KeyValue
If (Valore = 46) Then
sender.ReadOnly = False
End If
End Sub
Live long and prosper (Mr. Spock)
-
May 21st, 2004, 07:27 PM
#14
PowerPoster
Hi,
Exorcist has just posted this in another thread as code to search out each textbox on a form
VB Code:
For Each ctrl as Control in Me.Controls
if TypeOf ctrl Is Textbox then
'Do something
end if
Next
Combining that with Hinder's solution you get
VB Code:
Public Sub CheckBox(ByVal BoxToCompare As TextBox)
SyncLock Me.Controls
For Each Ctrl In Me.Controls
If TypeOf ctrl Is Textbox AndAlso Not (BoxToCompare Is Ctrl) Then
If C.Text.Trim.ToUpper = BoxToCompare.Text.Trim.ToUpper Then
BoxToCompare.Clear()
End If
End If
Next
End SyncLock
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.
-
May 22nd, 2004, 04:03 AM
#15
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|