Results 1 to 13 of 13

Thread: cleanning all textboxes on a form with a single code block (Resolved)

  1. #1

    Thread Starter
    Frenzied Member maged's Avatar
    Join Date
    Nov 2002
    Location
    Egypt
    Posts
    1,040

    cleanning all textboxes on a form with a single code block (Resolved)

    hi all,

    I used to use this segment in VB6 to clear all textboxes on a form, but it seems to have difficulties running on VB.Net

    VB Code:
    1. dim ctl as control
    2. for each ctl in controls
    3. if typeof ctl is textbox then ctl.text = ""
    4. next


    Can anyone advise me, what is the equivelant code in VB.Net

    \THX in Advance


    BST RGDS
    Last edited by maged; Jul 28th, 2004 at 05:19 AM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    VB Code:
    1. Dim x As TextBox
    2.         For Each x In Me.Controls
    3.             x.Text = ""
    4.         Next

  3. #3
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416
    VB Code:
    1. dim c as control
    2. for each c in me.controls
    3. if typeof c is textbox then
    4. 'do the trick
    5. end if
    6. next

  4. #4
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552

    Re: cleanning all textboxes on a form with a single code block

    Originally posted by maged
    hi all,

    I used to use this segment in VB6 to clear all textboxes on a form, but it seems to have difficulties running on VB.Net

    VB Code:
    1. dim ctl as control
    2. for each ctl in controls
    3. if typeof ctl is textbox then ctl.text = ""
    4. next


    Can anyone advise me, what is the equivelant code in VB.Net

    \THX in Advance


    BST RGDS
    ya code runs on my time machine mate

  5. #5
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    Originally posted by mendhak
    VB Code:
    1. Dim x As TextBox
    2.         For Each x In Me.Controls
    3.             x.Text = ""
    4.         Next
    does this varry from 1.0 to 1.1? got a specified cast error. i'm using 2k2.

  6. #6
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Sorry mendhak, that is not like you


    The correct code is

    VB Code:
    1. Dim x As Control
    2.         For Each x In Me.Controls
    3.             If TypeOf x Is TextBox Then
    4.                 CType(x, TextBox).Text = ""
    5.             End If
    6.         Next
    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.

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

    Loks like we are all posting as the other is typing
    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

    Thread Starter
    Frenzied Member maged's Avatar
    Join Date
    Nov 2002
    Location
    Egypt
    Posts
    1,040
    well, thx for your reply


    regarding the first solution, it is not working .
    An exception of ( invalid cast exception is raised).


    but regarding the second solution, it doesn't raise exception but also , it doesn't clear the textboxes


    i am going thermally crazy


    Any Sugesstions

    THX for your precious time

    BST RGDS

  9. #9
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Strange, it worked the first time.(Yes, really!!!)


    Now why wouldn't the second one work? It's perfectly alright.

    Try stepping through?

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

    You are not reading your posts fast enough, 'cos you obviously have not tried my suggestion which was

    VB Code:
    1. --------------------------------------------------------------------------------
    2.         Dim x As Control
    3.         For Each x In Me.Controls
    4.             If TypeOf x Is TextBox Then
    5.                 CType(x, TextBox).Text = ""
    6.             End If
    7.         Next

    Sorry you have been slightly mislead by a couple of guys, but they are not normally like that. They must hav had a late night
    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
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by mendhak
    Strange, it worked the first time.(Yes, really!!!)


    Now why wouldn't the second one work? It's perfectly alright.

    Try stepping through?
    Any minute now mendhak, you are going to wake up from your VB6 harkback dream
    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.

  12. #12
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

  13. #13

    Thread Starter
    Frenzied Member maged's Avatar
    Join Date
    Nov 2002
    Location
    Egypt
    Posts
    1,040
    hallllo alll



    well i figured out what was the problem


    the problem is that my textboxes is grouped into panels, panels works as a form inside the form, so when it scans the controls on the main form. it doesn't find a thing except panels.

    Anyway your code is right but i have modified it to a more powerfull and generic sub that will solve this problem even if there is a frame inside a frame inside a panel inside a group box . Etc.

    it works fine for me, but you deserve to share it with me as your helped in it a lot

    VB Code:
    1. Public Sub ClearAll(ByVal frm As Control)
    2.         Dim OutCtl As Control
    3.         For Each OutCtl In frm.Controls
    4.             If OutCtl.Controls.Count > 0 Then
    5.                 ClearAll(OutCtl)
    6.             Else
    7.                 If OutCtl.GetType.IsAssignableFrom(GetType(TextBox)) Then
    8.                     OutCtl.Text = ""
    9.                 End If
    10.             End If
    11.         Next
    12.     End Sub



    THX ALL FOR Your Time


    It is resolved now

    BST RGDS

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