Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have 99 buttons on a page of which I need to be able to perform a sweeping change of their .text and .tag properties. For convenience I have named the buttons "btnButton1" through "btnButton99". There are other buttons on the form I do not wish to change. I figure the easiest way to do this is through a routine as follows:

 

dim x as int16

dim ButtonName as string

 

for x = 1 to 99

ButtonName = "form.btnbutton" & ltrim(str(x)) ' set the specific button

ButtonName.text = "sometext"

ButtonName.tag = "sometag"

next x

 

Is something like this feasable in VB .NET? I realize that the variable at this time is a string and would have to have it's type changed to a control however I'm not sure if this can be done so that it is the content of the variable which is referenced as the control.

Thanks for any assistance provided and take care,

 

Hdokes

  • Leaders
Posted

   Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
       Dim x As Control
       For Each x In Me.Controls
           If TypeOf x Is Button Then
               x.Text = "new text"
           End If
       Next
   End Sub

 

if you wanted to make them all have a different name, you'd have to make a loop and a string as the button name or something. like this :

   Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
       Dim arrButtons As New ArrayList()
       Dim i As Integer
       Dim strButton As String = "Button"
       Dim btn As Control
       For i = 0 To 6
           arrButtons.Add(strButton & i)
           For Each btn In Me.Controls
               If btn.Name = arrButtons(i) Then
                   btn.Text = "item: " & i
               End If
           Next
       Next
   End Sub

Posted

Hi there,

 

Thanks very much for the examples..... I applied the 2nd example and it is working like a charm!

Thanks for any assistance provided and take care,

 

Hdokes

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...