Can I change more than one control's properties at the same time?

  • Thread starter Thread starter Mike Chan
  • Start date Start date
M

Mike Chan

Can I use some loop to change a series of control's properties at same time?

for example. I have textbox1, textbox2, textbox3, ...

If I want textbox1.text = "1", textbox2.text="2", textbox3.text="3"......

Can I use some loop method to do this?
 
Hi Mike,
Can I use some loop to change a series of control's properties at same time?
for example. I have textbox1, textbox2, textbox3, ...
If I want textbox1.text = "1", textbox2.text="2", textbox3.text="3"......
Can I use some loop method to do this?

If it is for a windowform where the controls are direct on the page you can
use this
(Watch typos I type it here but it is very simple so I do not expect them)

\\\
For each ctr as control in me.controls
if typeof ctr Is Textbox then
ctr.text = ctr.name.substring(7)
end if
next

This goes because text is a proporty from controls.
If you did want to change the " lines", than you have to write
\\\
directcast(ctr,textbox).lines = ctr. etc etc
///

I hope this helps?

Cor
 
Use following:

Dim obj As Object

Dim i As Integer

For Each obj In Me.Controls

For i = 1 To 3

If obj.name = "textbox" & i Then

obj.text = i

End If

Next

Next



Vikram
 
* "Mike Chan said:
Can I use some loop to change a series of control's properties at same time?

for example. I have textbox1, textbox2, textbox3, ...

If I want textbox1.text = "1", textbox2.text="2", textbox3.text="3"......

Can I use some loop method to do this?

Quick and dirty:

\\\
Dim t() As TextBox = {TextBox1, TextBox2, ...}
Dim i As Integer
For i = 0 to t.Length - 1
t(i).Text = (i + 1).ToString()
Next i
///
 
Hi Herfried,

Are you using this one now also?

\\
Dim t() As TextBox = {TextBox1, TextBox2, ...}
Dim i As Integer
For i = 0 to t.Length - 1
t(i).Text = (i + 1).ToString()
Next i
///

Is nice is it not?

I was in doubt which one I would supply.

Cor
 
Cor,

* "Cor said:
Are you using this one now also?

\\
Dim t() As TextBox = {TextBox1, TextBox2, ...}
Dim i As Integer
For i = 0 to t.Length - 1
t(i).Text = (i + 1).ToString()
Next i
///

Is nice is it not?

I was in doubt which one I would supply.

Sorry, I don't understand what you want to tell me.
 
Hi Herfried,

In past you where only using the for each loop.

This one came from a message an OP supplied, where we first thought it would
not work but after some investigation it did (Armin and I have been very
busy with it).

I thought you where a little bit sceptic about it, but now I see you are
advising it.

Cor
 
Hi Herfried,

Before you misunderstand it.

As the OP supplied it, it did not work, it was the investigation that did
make it work.

Cor
 
* "Cor said:
In past you where only using the for each loop.

I am a fan of 'For...Each' loops, I don't know why I didn't use a
'For...Each' loop in this "snippet".
 
Hi Herfried,

Look at the one I did use this time, that is the one you have always used.

I did wanted to be cooperative this time and used yours and now you took my
newest sample.

That is all, however I am glad, because now I know that new one is a good
one.

:-))

Cor
 
Mike,
I normally use a loop (For Each) over an array as Herfried showed.

If I need to loop more than once, I will make the array of controls a class
level variable, initializing the array in the constructor (after the call to
InitializeComponent). If I need to loop a single time then I will create the
array inline in that routine (for example the Load event).

Hope this helps
Jay
 
Cor,

* "Cor said:
Look at the one I did use this time, that is the one you have always used.
;-)

I did wanted to be cooperative this time and used yours and now you took my
newest sample.

I didn't see your question when writing my reply. I would prefer
'For...Each' too when using your approach (enumerating the controls).
 
??
I normally use a loop (For Each) over an array as Herfried showed.
That really should read "I normally use a loop (For Each) with an array,
like Herfried showed":

Reading it just now it sounded like I had a "better" method then Herfried,
where actually I'm am doing exactly what Herfried showed...

Jay
 
* "Jay B. Harlow said:
That really should read "I normally use a loop (For Each) with an array,
like Herfried showed":

Reading it just now it sounded like I had a "better" method then Herfried,
where actually I'm am doing exactly what Herfried showed...

:-)
 
Back
Top