concatenating vb.net code

D

Dan Smith

I have a form with 25 buttons and on load I would like to change the text of each button to unique names. A loop would work perfectly but obviously
for i = 1 to 25
me.button(i).text = stuff
end for

doesn't work. Anyone know of a easy way of doing this?

Thanks

Submitted via EggHeadCafe - Software Developer Portal of Choice
ComponentOne Studio for ASP.NET AJAX - Free License Giveaway
http://www.eggheadcafe.com/tutorial...o-for-aspnet-ajax--free-license-giveaway.aspx
 
T

Tom Shelton

It happens that Dan Smith formulated :
I have a form with 25 buttons and on load I would like to change the text of
each button to unique names. A loop would work perfectly but obviously for i
= 1 to 25 me.button(i).text = stuff
end for

doesn't work. Anyone know of a easy way of doing this?

Thanks

Submitted via EggHeadCafe - Software Developer Portal of Choice
ComponentOne Studio for ASP.NET AJAX - Free License Giveaway
http://www.eggheadcafe.com/tutorial...o-for-aspnet-ajax--free-license-giveaway.aspx

Name your buttons Button1 through Button25:

For i As Integer = 1 To 25
Dim btn As Button = Me.Controls("Button" & i)
btn.Text = stuff
Next
 
M

mahesh likhe

you can loop through each control present in this object for basic validation you can check control type as "Button" and for next validation you can name your button control specific like btnCtrl1,btnCtrl1,...,btnCtrl25 hence it help you to find exact control

For Each ctrl As Control In Me.Controls
If ctrl.GetType().Name = "Button" Then
If left(ctrl.Name,"btnCtrl".Length) == "btnCtrl")
ctrl.Text = stuff
End If
End If
Next



Submitted via EggHeadCafe - Software Developer Portal of Choice
JustCode Visual Studio Development Add-In by Telerik
http://www.eggheadcafe.com/tutorial...sual-studio-development-addin-by-telerik.aspx
 
C

Cor

Dan the problem with this is that you can only do this like you show if the
button is in a button array.

Dim Buttons() as button =
{button1,button2,button3...........................button25)

For i = o to buttons.Count-1
me.Button(i).Text = stuff
next

Otherwise you have, because a button can placed on any other container, to
do it recursive

Have for that recursive a look at this tip
http://www.vb-tips.com/ClearTextBox.aspx

Success

Cor

"Dan Smith" wrote in message
I have a form with 25 buttons and on load I would like to change the text of
each button to unique names. A loop would work perfectly but obviously
for i = 1 to 25
me.button(i).text = stuff
end for

doesn't work. Anyone know of a easy way of doing this?

Thanks

Submitted via EggHeadCafe - Software Developer Portal of Choice
ComponentOne Studio for ASP.NET AJAX - Free License Giveaway
http://www.eggheadcafe.com/tutorial...o-for-aspnet-ajax--free-license-giveaway.aspx
 
A

Armin Zingler

Am 06.10.2010 19:20, schrieb DanS:
Please tell me I'm wrong and you're code works perfectly to do
what the OP wants.

Declare variable Buttons at class level, not as a local variable.
 
T

Tom Shelton

DanS was thinking very hard :
Why does your code not run on my PC, even after I fix the
typing errors ?

I've got a form with six Buttons, Button1 through Button6, and
when I click Button1.....


Private Sub Button1_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles Button1.Click

Dim Buttons() As Button = {Button1, Button2, Button3, Button4,
Button5, Button6}

Dim i As Integer

For i = 0 To Buttons.Count - 1
Me.Buttons(i).Text = "Changed Text" & i
Next

End Sub
Remove the Me - it should just be:

Dim Buttons() As Button = {Button1, Button2, Button3, Button4}
For i As Integer = 0 To Buttons.Count - 1
Buttons(i).Text = "Changed Text" & i
Next
 
C

Cor

Ah you should not use the "me" in Buttons.

I've copied that from your message in the code, while I was not really
looking what you wrote.

Do you really think that you are the first one using this code by the way?

However, I tested it exactly as you did with one change.

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim Buttons() As Button = {Button1, Button2, Button3, Button4,
Button5, Button6}
For i = 0 To Buttons.Count - 1
Buttons(i).Text = "DS" & CStr(i + 1)
Next
End Sub
End Class

The Buttons on the form have now the text DS1 to DS6

Cor

"DanS" wrote in message

Dan the problem with this is that you can only do this like
you show if the button is in a button array.

Dim Buttons() as button =
{button1,button2,button3...........................button25)

For i = o to buttons.Count-1
me.Button(i).Text = stuff
next

Why does your code not run on my PC, even after I fix the
typing errors ?

I've got a form with six Buttons, Button1 through Button6, and
when I click Button1.....


Private Sub Button1_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles Button1.Click

Dim Buttons() As Button = {Button1, Button2, Button3, Button4,
Button5, Button6}

Dim i As Integer

For i = 0 To Buttons.Count - 1
Me.Buttons(i).Text = "Changed Text" & i
Next

End Sub

Results in this error message:

Error 1 'Buttons' is not a member of
\WindowsApplication1.Form1'. C:\Documents and Settings\DS
\Local Settings\Application Data\Temporary Projects
\WindowsApplication1\Form1.vb 10 13 WindowsApplication1

I'm guessing it's because .Net doesn't support control arrays
the way VB6 does. From what I understand, you need to add the
controls to collectionn, and then you can access them in the
collection, sort of like a control array.

Please tell me I'm wrong and you're code works perfectly to do
what the OP wants.
 
T

Tom Shelton

Tom Shelton was thinking very hard :
DanS was thinking very hard :
Remove the Me - it should just be:

Dim Buttons() As Button = {Button1, Button2, Button3, Button4}
For i As Integer = 0 To Buttons.Count - 1
Buttons(i).Text = "Changed Text" & i
Next

LOL... scratch that - do as Armin suggested - just create the
collection at the form level. Then you can use it if needed.

Also, there is the forms controls collection indexed by the name, or if
you want to search recursively, the controls collection has a find
method :)
 
C

Cor

Tom,

Not option strict ON?

:)

Cor

"Tom Shelton" wrote in message

DanS was thinking very hard :
Why does your code not run on my PC, even after I fix the typing errors ?

I've got a form with six Buttons, Button1 through Button6, and when I
click Button1.....


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim Buttons() As Button = {Button1, Button2, Button3, Button4, Button5,
Button6}

Dim i As Integer

For i = 0 To Buttons.Count - 1
Me.Buttons(i).Text = "Changed Text" & i
Next

End Sub
Remove the Me - it should just be:

Dim Buttons() As Button = {Button1, Button2, Button3, Button4}
For i As Integer = 0 To Buttons.Count - 1
Buttons(i).Text = "Changed Text" & i
Next
 
C

Cor

Has for me always one problem

If I don't do it like this and I use the load event, then the buttons are
not yet initialized.

Public Class Form1
Private Buttons() As Button
Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Buttons = {Button1, Button2, Button3, Button4, Button5, Button6}
For i = 0 To Buttons.Count - 1
Me.Buttons(i).Text = "DS" & CStr(i)
Next
End Sub
End Class

But that is not as easy to tell as it seems for you.

Cor

"Armin Zingler" wrote in message
Am 06.10.2010 19:20, schrieb DanS:
Please tell me I'm wrong and you're code works perfectly to do
what the OP wants.

Declare variable Buttons at class level, not as a local variable.
 
T

Tom Shelton

Cor expressed precisely :
Has for me always one problem

If I don't do it like this and I use the load event, then the buttons are not
yet initialized.

Public Class Form1
Private Buttons() As Button
Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Buttons = {Button1, Button2, Button3, Button4, Button5, Button6}
For i = 0 To Buttons.Count - 1
Me.Buttons(i).Text = "DS" & CStr(i)
Next
End Sub
End Class

But that is not as easy to tell as it seems for you.

You can intialize the array in the constructor. will exist after the
call to InitializeComponents - but, the controls window handle may not
be allocated. But, that won't matter in this case - you can read and
set the text before the handle is created (or after it's disposed...)
 
C

Cor

Tom,

I know all kind of ways around this;, the problem is only, that is not
direct all clear in a sample for beginners.

Cor

"Tom Shelton" wrote in message

Cor expressed precisely :
Has for me always one problem

If I don't do it like this and I use the load event, then the buttons are
not yet initialized.

Public Class Form1
Private Buttons() As Button
Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Buttons = {Button1, Button2, Button3, Button4, Button5, Button6}
For i = 0 To Buttons.Count - 1
Me.Buttons(i).Text = "DS" & CStr(i)
Next
End Sub
End Class

But that is not as easy to tell as it seems for you.

You can intialize the array in the constructor. will exist after the
call to InitializeComponents - but, the controls window handle may not
be allocated. But, that won't matter in this case - you can read and
set the text before the handle is created (or after it's disposed...)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top