Results 1 to 13 of 13

Thread: textbox controls as a collection on a form

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2004
    Posts
    134

    textbox controls as a collection on a form

    hi i am using winforms with vb.net.. i have like 12 textboxes on my winform and i would like that when the page loads the textboxes enabled property is set to false. i know that i can set the enabled property of each textbox on the winform from the properties window to false .. i would like to know how it is done programatically.. thanks in advance...

  2. #2
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: textbox controls as a collection on a form

    In the forms constructor
    just set the enabled property to False;
    TextBox1.Enabled = False;

  3. #3
    Junior Member
    Join Date
    Feb 2005
    Posts
    19

    Re: textbox controls as a collection on a form

    Hey,
    by the way a question:
    is it possible to do an action like (set Textbox1,...,Textbox20)-Property enabled to False at one time.
    Or schould man do this for every control elem.

  4. #4
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: textbox controls as a collection on a form

    either do it one at a time or create a loop.
    something like
    foreach(TextBox t in Me.Controls)
    t.Enabled = false
    Next

  5. #5
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: textbox controls as a collection on a form

    another idea would be to create a new class which extends the TextBox class
    but in it's constructor sets enabled to false.
    So every new Textbox you create of your type will start off disabled.

  6. #6
    Junior Member
    Join Date
    Feb 2005
    Posts
    19

    Re: textbox controls as a collection on a form

    thnx that will be great help

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Dec 2004
    Posts
    134

    Re: textbox controls as a collection on a form

    hi.. i know that i can set all of the textboxes in my form enabled property to false but like to put them in a collection. i like the idea of the example given above but me a bit stuck on it.. my textboxes names are as follows.
    txtPaypoint,txtSchoolName,txtSchoolType,txtPopulation,txtRoom,txtAddress,txtVillage etc...
    could you tell me how i can do this... so automatically when the form loads it enables the to false...
    are my textboxes name wrong? should they be like txt1, txt2,txt3 so then i can use an array?
    would really appreciate some help.. thanks in advance

  8. #8
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: textbox controls as a collection on a form

    Quote Originally Posted by sonvel
    hi.. i know that i can set all of the textboxes in my form enabled property to false but like to put them in a collection. i like the idea of the example given above but me a bit stuck on it.. my textboxes names are as follows.
    txtPaypoint,txtSchoolName,txtSchoolType,txtPopulation,txtRoom,txtAddress,txtVillage etc...
    could you tell me how i can do this... so automatically when the form loads it enables the to false...
    are my textboxes name wrong? should they be like txt1, txt2,txt3 so then i can use an array?
    would really appreciate some help.. thanks in advance
    you can that in this way.
    VB Code:
    1. 'if you want to disable the controls you like. just call the sub controldisable
    2. 'in your form load
    3. controldisable(new Control(){txtPaypoint,txtSchoolName,txtSchoolType,txtPopulation,txtRoom,txtAddress,txtVillage})
    4. 'as well as in enabling it.
    5. controlenable(new Control(){txtPaypoint,txtSchoolName,txtSchoolType,txtPopulation,txtRoom,txtAddress,txtVillage})
    6.  
    7.  
    8. Sub controlenable(ByVal c() As Control)
    9.         Dim cc As Control
    10.         For Each cc In c
    11.             cc.Enabled = True
    12.         Next
    13.     End Sub
    14.  
    15.     Sub controldisable(ByVal c() As Control)
    16.         Dim cc As Control
    17.         For Each cc In c
    18.             cc.Enabled = False
    19.         Next
    20.     End Sub

    hope that helps.

  9. #9
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: textbox controls as a collection on a form

    You textboxes are already in a collection, Form.Controls. but this also contains all the other controls on the form.
    The For Each Textbox... will work regardless of the textboxes names if it is implemented in the forms contructor after the InitialiiseComponent sub.

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

    Re: textbox controls as a collection on a form

    Hi,

    You can also single out individual textbox groups for separate action if you wish. There are 2 ways to do this.

    1. use the Tag property of the TextBox.

    VB Code:
    1. Dim Ctl As Control
    2.   For each ctl in Me.Controls     (or if in a groupbox etc --  in
    3.                                             GroupBox1.Controls)
    4.      If clt.Tag = "Something" then
    5.           xxxxxxxxxx
    6.      End If
    7.   Next

    If necessary you could use If TypeOf ctl Is textBox And ctl.tag
    = "Something"


    2. Dim ctl As Control

    For Each ctl in Me.Controls
    If Microsoft.VisualBasic.Left(ctl.name,7) ="txtAbcd" Then
    xxxxxxxxxxxxxx
    End If
    Next
    Last edited by taxes; Mar 4th, 2005 at 08:02 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

    Thread Starter
    Addicted Member
    Join Date
    Dec 2004
    Posts
    134

    Re: textbox controls as a collection on a form

    heya guys this is great thanks loads really appreciate it.. its working fine and i found it very very useful..thanks.. take care...

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

    Re: textbox controls as a collection on a form

    Hi,

    Wossname has just pointed out in another thread that Microsoft.VisualBasic.left has been replaced in VB.NET with, in the example in my previous post,

    If ctl.name.Substring(1,7) ="txtAbcd" Then

    Shorter and more correct
    Last edited by taxes; Mar 4th, 2005 at 06:51 PM.
    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
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: textbox controls as a collection on a form

    Hi,

    Correction to my last post. Substring is zero based so it should be:

    If ctl.name.Substring(0,7) ="txtAbcd" Then
    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.

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