|
-
Mar 3rd, 2005, 10:03 AM
#1
Thread Starter
Addicted Member
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...
-
Mar 3rd, 2005, 10:10 AM
#2
Re: textbox controls as a collection on a form
In the forms constructor
just set the enabled property to False;
TextBox1.Enabled = False;
-
Mar 3rd, 2005, 10:20 AM
#3
Junior Member
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.
-
Mar 3rd, 2005, 10:29 AM
#4
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
-
Mar 3rd, 2005, 10:31 AM
#5
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.
-
Mar 3rd, 2005, 10:35 AM
#6
Junior Member
Re: textbox controls as a collection on a form
thnx that will be great help
-
Mar 4th, 2005, 03:52 AM
#7
Thread Starter
Addicted Member
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
-
Mar 4th, 2005, 04:18 AM
#8
Re: textbox controls as a collection on a form
 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:
'if you want to disable the controls you like. just call the sub controldisable
'in your form load
controldisable(new Control(){txtPaypoint,txtSchoolName,txtSchoolType,txtPopulation,txtRoom,txtAddress,txtVillage})
'as well as in enabling it.
controlenable(new Control(){txtPaypoint,txtSchoolName,txtSchoolType,txtPopulation,txtRoom,txtAddress,txtVillage})
Sub controlenable(ByVal c() As Control)
Dim cc As Control
For Each cc In c
cc.Enabled = True
Next
End Sub
Sub controldisable(ByVal c() As Control)
Dim cc As Control
For Each cc In c
cc.Enabled = False
Next
End Sub
hope that helps.
-
Mar 4th, 2005, 04:21 AM
#9
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.
-
Mar 4th, 2005, 06:06 AM
#10
PowerPoster
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:
Dim Ctl As Control
For each ctl in Me.Controls (or if in a groupbox etc -- in
GroupBox1.Controls)
If clt.Tag = "Something" then
xxxxxxxxxx
End If
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.
-
Mar 4th, 2005, 07:38 AM
#11
Thread Starter
Addicted Member
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...
-
Mar 4th, 2005, 09:36 AM
#12
PowerPoster
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.
-
Mar 4th, 2005, 08:00 PM
#13
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|