PC Review


Reply
Thread Tools Rating: Thread Rating: 2 votes, 1.00 average.

concatenating vb.net code

 
 
Dan Smith
Guest
Posts: n/a
 
      6th Oct 2010
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/tutorials...-giveaway.aspx
 
Reply With Quote
 
 
 
 
Tom Shelton
Guest
Posts: n/a
 
      6th Oct 2010
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/tutorials...-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

--
Tom Shelton


 
Reply With Quote
 
mahesh likhe
Guest
Posts: n/a
 
      6th Oct 2010
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/tutorials...y-telerik.aspx
 
Reply With Quote
 
Cor
Guest
Posts: n/a
 
      6th Oct 2010
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 news:(E-Mail Removed)...

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/tutorials...-giveaway.aspx

 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      6th Oct 2010
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.

--
Armin
 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      6th Oct 2010
DanS was thinking very hard :
> "Cor" <(E-Mail Removed)> wrote in
> news:4cac4363$0$8913$(E-Mail Removed):
>
>> 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
>

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

--
Tom Shelton


 
Reply With Quote
 
Cor
Guest
Posts: n/a
 
      6th Oct 2010
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
news:Xns9E0987C6DEAEBthisnthatroadrunnern@216.196.97.131...

"Cor" <(E-Mail Removed)> wrote in
news:4cac4363$0$8913$(E-Mail Removed):

> 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.











>
> 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
> news:(E-Mail Removed)...
>
> 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/tutorials...98ce1f-2b5d-4e
> c8-b6d5-a1049651514e/componentone-studio-for-aspnet-ajax--fr
> ee-license-giveaway.aspx
>
>


 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      6th Oct 2010
Tom Shelton was thinking very hard :
> DanS was thinking very hard :
>> "Cor" <(E-Mail Removed)> wrote in
>> news:4cac4363$0$8913$(E-Mail Removed):
>>
>>> 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
>>

> 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

--
Tom Shelton


 
Reply With Quote
 
Cor
Guest
Posts: n/a
 
      6th Oct 2010
Tom,

Not option strict ON?

:-)

Cor

"Tom Shelton" wrote in message
news:i8icti$moi$(E-Mail Removed)...

DanS was thinking very hard :
> "Cor" <(E-Mail Removed)> wrote in
> news:4cac4363$0$8913$(E-Mail Removed):
>> 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
>

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

--
Tom Shelton

 
Reply With Quote
 
Cor
Guest
Posts: n/a
 
      6th Oct 2010
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 news:(E-Mail Removed)...

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.

--
Armin

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fixing VBA code to better suit concatenating needs!! =?Utf-8?B?U2FuZHdpY2hlczI=?= Microsoft Excel Programming 1 15th Mar 2006 02:32 AM
Problem concatenating strings in VBA module code =?Utf-8?B?RWxpc2hh?= Microsoft Access VBA Modules 2 28th Dec 2005 01:21 AM
concatenating and formatting area code and phone number columns =?Utf-8?B?c2hlcnJp?= Microsoft Excel Worksheet Functions 4 1st Sep 2005 09:59 PM
concatenating =?Utf-8?B?TWluZHk=?= Microsoft Excel Worksheet Functions 10 11th Jan 2004 05:49 PM
Equivalent of _ used in VB as concatenating code statement Richard Chen Microsoft C# .NET 5 18th Dec 2003 02:22 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:22 AM.