By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,740 Members | 1,326 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,740 IT Pros & Developers. It's quick & easy.

button.Select

P: n/a
All, I appear to have a mind block at the moment. Could someone help me out
and get me over this hurdle.
1. Create a new VB.Net 2003 project with form1 open.
2. Drop a "button1" onto the form.
3. Paste in the code below.
When repeatNo is 0, I see the 6th button selected as expected. When repeatNo
is >0, it isn't. For goodness sakes, why?

Thank you
Hexathioorthooxalate

Public Class Form1
Inherits System.Windows.Forms.Form

'+ Windows Form Designer generated code
Private Class btnExtend
Inherits Button
End Class

Private Sub clearButtons()
For Each o As Object In Me.Controls
If TypeOf o Is btnExtend Then Me.Controls.Remove(o)
Next
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim btn As Button
For repeatNo As Integer = 0 To 5
clearButtons()
For buttonCounter As Integer = 0 To 9
btn = New Button
btn.Size = New Size(10, 10)
btn.Top = 20
btn.Left = buttonCounter * 15
Me.Controls.Add(btn)
If buttonCounter = 5 Then btn.Select()
Next buttonCounter
MsgBox("complete" + repeatNo.ToString())
Next repeatNo
End Sub
End Class

Nov 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Might help if I paste in the correct code to start with :( Here is a revised
copy.

Sometimes the sixth button is selected (when repeatNo=0, 2, 3, 5),
somethings it isn't (when repeatNo=1 and 4).
I'm still unsure of why, any ideas?
Hexathioorthooxalate
Public Class Form1
Inherits System.Windows.Forms.Form

'+ Windows Form Designer generated code

Private Class btnExtend
Inherits Button
End Class

Private Sub clearButtons()
For Each o As Object In Me.Controls
If TypeOf o Is btnExtend Then Me.Controls.Remove(o)
Next
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim btn As btnExtend
For repeatNo As Integer = 0 To 5
clearButtons()
For buttonCounter As Integer = 0 To 9
btn = New btnExtend()
btn.Size = New Size(10, 10)
btn.Top = 20
btn.Left = buttonCounter * 15
Me.Controls.Add(btn)
If buttonCounter = 5 Then btn.Select()
Next buttonCounter
MsgBox("complete" + repeatNo.ToString())
Me.Select()
Next repeatNo
End Sub
End Class

"hexathioorthooxalate" <ru***@REMOVESPAM.clara.co.uk> wrote in message
news:10****************@damia.uk.clara.net...
All, I appear to have a mind block at the moment. Could someone help me out and get me over this hurdle.
1. Create a new VB.Net 2003 project with form1 open.
2. Drop a "button1" onto the form.
3. Paste in the code below.
When repeatNo is 0, I see the 6th button selected as expected. When repeatNo is >0, it isn't. For goodness sakes, why?

Thank you
Hexathioorthooxalate

Public Class Form1
Inherits System.Windows.Forms.Form

'+ Windows Form Designer generated code
Private Class btnExtend
Inherits Button
End Class

Private Sub clearButtons()
For Each o As Object In Me.Controls
If TypeOf o Is btnExtend Then Me.Controls.Remove(o)
Next
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim btn As Button
For repeatNo As Integer = 0 To 5
clearButtons()
For buttonCounter As Integer = 0 To 9
btn = New Button
btn.Size = New Size(10, 10)
btn.Top = 20
btn.Left = buttonCounter * 15
Me.Controls.Add(btn)
If buttonCounter = 5 Then btn.Select()
Next buttonCounter
MsgBox("complete" + repeatNo.ToString())
Next repeatNo
End Sub
End Class


Nov 20 '05 #2

P: n/a
Cor
Hu Hexathioorthooxalate

Removing controls while leaving controls on a form goes mostly wrong.

Therefore it is better to do it in a reversmode something like this (rough
written)
\\\
dim i as integer
for i = me.controls.count -1 to 0 step -1
if typeof me.controls(i) is mycontrol then
mecontrols.removeat(i)
end if
next
///
I hope this helps a little bit?

Cor
Nov 20 '05 #3

P: n/a
Doh, of course !
Thanks
Hexathioorthooxalate
"Cor" <no*@non.com> wrote in message
news:us**************@TK2MSFTNGP11.phx.gbl...
Hu Hexathioorthooxalate

Removing controls while leaving controls on a form goes mostly wrong.

Therefore it is better to do it in a reversmode something like this (rough
written)
\\\
dim i as integer
for i = me.controls.count -1 to 0 step -1
if typeof me.controls(i) is mycontrol then
mecontrols.removeat(i)
end if
next
///
I hope this helps a little bit?

Cor


Nov 20 '05 #4

P: n/a
"hexathioorthooxalate" <ru***@REMOVESPAM.clara.co.uk> wrote...

The cause of the error has been pointed out but I'm curious that the syntax
you used

here:
For repeatNo As Integer = 0 To 5
and here: For buttonCounter As Integer = 0 To 9


works for you. Doesn't it generate a compiler warning or error?
Nov 20 '05 #5

P: n/a
It's one of the differences between VB.NET 2003 and VB.NET 2002; syntax
error in VB.NET 2002; okay in VB.NET 2003.

And there is even more coming in Whidbey!
Regards
Hexathioorthooxalate

"Tom Leylan" <ge*@iamtiredofspam.com> wrote in message
news:Ce********************@twister.nyc.rr.com...
"hexathioorthooxalate" <ru***@REMOVESPAM.clara.co.uk> wrote...

The cause of the error has been pointed out but I'm curious that the syntax you used

here:
For repeatNo As Integer = 0 To 5


and here:
For buttonCounter As Integer = 0 To 9


works for you. Doesn't it generate a compiler warning or error?


Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.