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... :)
Re: textbox controls as a collection on a form
In the forms constructor
just set the enabled property to False;
TextBox1.Enabled = False;
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.
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
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.
Re: textbox controls as a collection on a form
thnx that will be great help
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
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:
'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.:)
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.
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
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... :wave:
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 :wave:
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