Results 1 to 5 of 5

Thread: [02/03][Resolved] declaration issue--4th post onwards

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    490

    Resolved [02/03][Resolved] declaration issue--4th post onwards

    i am trying to shorten my code, so instead of having errorprovider for each textbox i want to programatically reduce using something like the following.
    although i dont get any errors, it doesnt do the work either.please help
    cheers
    For Each txerr As Control In firstMdiChild.Controls
    If TypeOf txerr Is TextBox Then
    'here i want to declare many textboxes , two in this example.
    'if i declare one it works, but as soon as i declare more it doesn't work.
    Dim errpro() As String = {"TextBox6", "TextBox7"}
    'here i am comparing the name of the textbox with errpro
    If txerr.Name Is errpro Then
    ErrorProvider1.SetError(txerr, "Cannot leave blank")
    Else
    ErrorProvider1.SetError(txerr, "")
    End If
    End If
    Next
    Last edited by indiewolf; Oct 22nd, 2007 at 07:56 AM. Reason: resolved
    visual basic.net (2008)

    .net framework 3.5


  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] why is this not correct ?

    vb.net Code:
    1. If txerr.Name Is errpro Then
    txterr.Name is a String and errpro is an array of String. That's like saying "if this egg is this egg carton". That can never be true.

    Even if they were both Strings you'd should still be using the '=' operator, not the 'Is' operator. You want to know if the strings contain the same text, not whether they are the same object.

    What you actually want to do is test whether the string is an element of the array, for which you would use the Array.IndexOf method. If IndexOf returns -1 then the value is not in the array, otherwise its index is returned.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    490

    Thumbs up Re: [02/03] why is this not correct ?

    brilliant reply.
    that was exactly i was looking for. array.index of ruleth!!

    cheers buddy
    visual basic.net (2008)

    .net framework 3.5


  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    490

    Re: [02/03] why is this not correct ?

    one more thing.

    i have come to this problem many a times.

    suppose i have a code like the following
    Code:
    For Each txerr As Control In firstMdiChild.Controls
                If TypeOf txerr Is TextBox Then
                    Dim errpro() As String = {"TextBox6", ", "TextBox56", _
     "TextBox57", "TextBox58", "TextBox59","ComboBox1", _
                   "ComboBox2", "ComboBox5", "ComboBox6", _
                   "ComboBox7", ", "ComboBox11"}
    now since i have declared it as form.controls , it restricts me to only textboxes on the form.
    textboxes inside groupboxes, comboboxes etc aren't handled.
    so why can't i write something like
    Code:
    for each txerr as control in firstmdichild.controls,firstmdichild.groupbox1.controls
    if type of txerr is textbox  and combobox then
    please suggest some solution
    cheers
    visual basic.net (2008)

    .net framework 3.5


  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] declaration issue--4th post onwards

    Because that's not legal syntax. If you understand how the language works then you understand why. The For Each loop is a construct that wraps the use of an IEnumerator object. When you iterate over a collection using a For Each loop you are implicitly calling the GetEnumerator method of that collection and and repeatedly calling its MoveNext method. One For Each loop cannot do that for two collections. If you want to iterate over two collections then you use two For Each loops. That said, if you want to visit nested controls then you should either use recursion or the GetNextControl method, whose use I've demonstrated in the VB.NET CodeBank forum.

    Apart from that, this line makes no sense either:
    vb.net Code:
    1. if type of txerr is textbox  and combobox then
    It's obvious what you're trying to do but even if the language did work that way that would still be wrong. How can a control be a TextBox AND a ComboBox? You should at least ne using OR rather than AND, but you're simply choosing to ignore how logical operators work. They require a Boolean expression on both sides. (TypeOf txerr Is TextBox) is a Boolean expression but (ComboBox) isn't. What you want to do is quite simple but you have to do it within the constraints of acceptable VB syntax:
    vb.net Code:
    1. If TypeOf txerr Is TextBox OrElse TypeOf txerr Is ComboBox Then
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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