Click here to Skip to main content
15,885,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have a button formumda 50 pieces.
Imagen changes made to them in a sub form. I have to use certain parts of the program.
However, I do not want to change them one by one. I want to do a for loop in the next cycle.
1. Buttons from the 50th Images of the button will change to. But somehow I could not work from within. I'll be glad if you help. Yours sincerely


I wrote the code that is changing, but what the text nor the forecolor

But directly button30.text = "test" if I say no problem


Dim mybutton As New Button


             For tt 1 to 50

                 mybutton.Name = "Button"&tt

                 With mybutton
                     .Text = "test"
                     .Name = "Button" & tt
                     .ForeColor = Color.Blue
                 End With
 next
             End If
Posted
Updated 18-Jan-11 1:13am
v2
Comments
OriginalGriff 18-Jan-11 9:00am    
Don't reply to people with the "Add and Answer" button - they do not get an email, so they don't know you have done it!
Instead, use the "Add Comment feature".
I have deleted your answers, and modified my own.

You can loop through the Controls list for the form, and check "is this a button"?
VB
For Each c As Control In Controls
    Dim b As Button = TryCast(c, Button)
    If b IsNot Nothing Then
       ....
    End If
Next



"sorry i am not understand


For Each c As Control In Controls
Dim b As Button = TryCast(c, Button)
If b IsNot Nothing Then

For tt 1 to 50

mybutton.Name = "Button"&tt

With mybutton
.Text = "test"
.ForeColor = Color.Blue
End With
next



End If
Next


in this way will beDid he??

or


AddHandler Button1.Click, AddressOf secilenbuton
AddHandler Button2.Click, AddressOf secilenbuton
AddHandler Button3.Click, AddressOf secilenbuton
AddHandler Button4.Click, AddressOf secilenbuton
.
.
.
.
.
AddHandler Button50.Click, AddressOf secilenbuton

presentation, I ask,
How can I write the following definition as a short"




No, when you do the For Each loop, every time you get a value of "b" that is not Nothing, it it already a button.
You do not need the "For tt" loop any more.

VB
For Each c As Control In Controls
    Dim b As Button = TryCast(c, Button)
    If b IsNot Nothing Then
        With b
            .Text = "test"
            .ForeColor = Color.Blue
        End With
    End If
Next
 
Share this answer
 
v2
Comments
Espen Harlinn 18-Jan-11 6:47am    
5+ Nice answer, wonder what his building though ...
OriginalGriff 18-Jan-11 6:50am    
50 buttons, all named "Buttonxx"? I don't know, but I don't want to maintain it!
OriginalGriff 18-Jan-11 8:58am    
THe OP wrote:
"sorry i am not understand


For Each c As Control In Controls
Dim b As Button = TryCast(c, Button)
If b IsNot Nothing Then

For tt 1 to 50

mybutton.Name = "Button"&tt

With mybutton
.Text = "test"
.ForeColor = Color.Blue
End With
next



End If
Next


in this way will beDid he??

or


AddHandler Button1.Click, AddressOf secilenbuton
AddHandler Button2.Click, AddressOf secilenbuton
AddHandler Button3.Click, AddressOf secilenbuton
AddHandler Button4.Click, AddressOf secilenbuton
.
.
.
.
.
AddHandler Button50.Click, AddressOf secilenbuton

presentation, I ask,
How can I write the following definition as a short"
I think you this will help. You just have to find the control by name.

For tt 1 to 50
    Dim mybutton As Button = Me.Controls.Find("Button" & tt,True)(0)
    With mybutton
        .Text = "test"
        .Name = "Button" & tt
        .ForeColor = Color.Blue
    End With
next
 
Share this answer
 
v2
Comments
vozkan58 18-Jan-11 9:05am    
For tt 1 to 50
Dim mybutton As Button = Me.Controls.Find("Button"&tt,True)
With mybutton
.Text = "test"
.Name = "Button"&tt
.ForeColor = Color.Blue
End With
next

To underline the section error is;

Value of type'1-dimensional array of system.windows.forms.controls cannot be converted to system.windows.forms.button
vozkan58 18-Jan-11 9:06am    
For tt 1 to 50
Dim mybutton As Button = Me.Controls.Find("Button"&tt,True) 'THIS LINE
With mybutton
.Text = "test"
.Name = "Button"&tt
.ForeColor = Color.Blue
End With
next

To underline the section error is;

Value of type'1-dimensional array of system.windows.forms.controls cannot be converted to system.windows.forms.button
Prerak Patel 18-Jan-11 9:55am    
Sorry, it is like this...
Dim mybutton As Button = Me.Controls.Find("Button"&tt,True)(0)
vozkan58 18-Jan-11 10:23am    
THANK YOU,
That is all folks
Prerak Patel 18-Jan-11 10:27am    
You are welcome. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900