Click here to Skip to main content
15,889,863 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: How To Make New Form? Pin
Navneet Hegde15-Mar-07 2:06
Navneet Hegde15-Mar-07 2:06 
AnswerRe: How To Make New Form? Pin
manni_n15-Mar-07 9:27
manni_n15-Mar-07 9:27 
Questionhow to assign integer value in datagrid template column Pin
Member 387988114-Mar-07 23:47
Member 387988114-Mar-07 23:47 
AnswerRe: how to assign integer value in datagrid template column Pin
Marcus J. Smith15-Mar-07 2:41
professionalMarcus J. Smith15-Mar-07 2:41 
Questionvalidation of textoxes... Pin
manni_n14-Mar-07 23:20
manni_n14-Mar-07 23:20 
AnswerRe: validation of textoxes... Pin
The ANZAC14-Mar-07 23:36
The ANZAC14-Mar-07 23:36 
GeneralRe: validation of textoxes... Pin
manni_n15-Mar-07 5:38
manni_n15-Mar-07 5:38 
GeneralRe: validation of textoxes... Pin
TwoFaced15-Mar-07 10:43
TwoFaced15-Mar-07 10:43 
The first posters code failed because 't' was declared as a textbox. If the control collection contains something other then a textbox the cast will fail. 't' should have been a control. Also instead of hardcoding textbox1 you can use the same technique you learned from your first validation post. Use directcast to cast the sender object as a textbox. This will allow you to handle all your textboxes from this single procedure. Try the following
Dim txtSender As TextBox = DirectCast(sender, TextBox)

'Loop through all controls on the form
'If the form isn't the parent use ParentName.Controls instead of me.controls
For Each ctrl As Control In Me.Controls
    'Is ctrl a texbox and NOT the textbox that raised this method
    If TypeOf ctrl Is TextBox AndAlso ctrl IsNot txtSender Then
        If DirectCast(ctrl, TextBox).Text = txtSender.Text Then
            'Add code to raise Error
            Exit For
        End If
    End If
Next
I know you were told you could add handlers for multiple textboxes by doing Handles textbox1.textchanged, textbox2.textchange etc. But if you have a 100 textboxes that's just tedious especially when you have to do that for multiple procedures. The following code will loop through the controls and add handlers for you when the form loads. In this example I added a handler for the textchanged event for all the textboxes found on the form but you can add whatever handlers you want. I also added the textbox to a collection so you don't have to loop through the controls later but can loop through the collection of just textboxes.
Private TextBoxCollection As List(Of TextBox)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    For Each ctrl As Control In Me.Controls
        If TypeOf ctrl Is TextBox Then
            TextBoxCollection.Add(DirectCast(ctrl, TextBox))
            AddHandler ctrl.TextChanged, AddressOf txtBox_TextChanged
        End If
    Next
End Sub
With the addition of the collection the first bit of code could be changed to this.
Dim txtSender As TextBox = DirectCast(sender, TextBox)

'Loop through all the textboxes
For Each txt As TextBox In TextBoxCollection
    'If txt isn't the control that raised this method and the values
    'match raise an error
    If txt IsNot txtSender AndAlso txt.Text = txtSender.Text Then
        'Add code to raise Error
        Exit For
    End If
Next

Generalthanks Pin
manni_n15-Mar-07 11:52
manni_n15-Mar-07 11:52 
GeneralRe: thanks Pin
TwoFaced15-Mar-07 12:27
TwoFaced15-Mar-07 12:27 
GeneralRe: thanks Pin
manni_n15-Mar-07 12:45
manni_n15-Mar-07 12:45 
GeneralRe: thanks Pin
TwoFaced15-Mar-07 12:49
TwoFaced15-Mar-07 12:49 
GeneralRe: thanks Pin
manni_n15-Mar-07 12:55
manni_n15-Mar-07 12:55 
GeneralRe: thanks Pin
TwoFaced15-Mar-07 12:59
TwoFaced15-Mar-07 12:59 
Generalperfect... Pin
manni_n15-Mar-07 13:12
manni_n15-Mar-07 13:12 
Generalcheck it... Pin
manni_n15-Mar-07 13:23
manni_n15-Mar-07 13:23 
GeneralRe: check it... [modified] Pin
TwoFaced15-Mar-07 14:07
TwoFaced15-Mar-07 14:07 
GeneralRe: check it... Pin
manni_n16-Mar-07 1:05
manni_n16-Mar-07 1:05 
GeneralRe: check it... Pin
TwoFaced16-Mar-07 6:30
TwoFaced16-Mar-07 6:30 
GeneralRe: check it... Pin
manni_n16-Mar-07 8:34
manni_n16-Mar-07 8:34 
GeneralRe: check it... Pin
TwoFaced16-Mar-07 10:13
TwoFaced16-Mar-07 10:13 
Generalcool... [modified] Pin
manni_n16-Mar-07 11:06
manni_n16-Mar-07 11:06 
GeneralRe: cool... Pin
TwoFaced16-Mar-07 12:11
TwoFaced16-Mar-07 12:11 
QuestionChecked List Box Pin
taherjaorawala14-Mar-07 21:46
taherjaorawala14-Mar-07 21:46 
AnswerRe: Checked List Box Pin
manni_n14-Mar-07 23:13
manni_n14-Mar-07 23:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.