button.Select

  • Thread starter Thread starter hexathioorthooxalate
  • Start date Start date
H

hexathioorthooxalate

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
 
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
 
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
 
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?
 
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,
As Hexathioorthooxalate pointed out it is new in VS.NET 2003, along with the
new bit shift operators << & >>.

Both For & For Each support giving the type of the variable on the for line:

For index As Integer = 0 to 100
Next

Dim list As ArrayList

For Each item As Object In list
Next

It makes for cleaner shorter code, as you do not need to define the variable
& then give the for loop, you can do both in one statement.

The bit shift operators << & >> allow you to shift integers left or right,
the bit shift operators are generally more efficient than either
multiplication & division or exponents.

<Flags()> _
Enum BitFlags
Value1 = 1 << 0
Value2 = 1 << 1
Value3 = 1 << 2
Value4 = 1 << 3
Value5 = 1 << 4
End Enum

Hope this helps
Jay
 
Jay B. Harlow said:
As Hexathioorthooxalate pointed out it is new in VS.NET 2003, along with the
new bit shift operators << & >>.

Both For & For Each support giving the type of the variable on the for line:

For index As Integer = 0 to 100
Next

Dim list As ArrayList

For Each item As Object In list
Next

It makes for cleaner shorter code, as you do not need to define the variable
& then give the for loop, you can do both in one statement.

I didn't seem to get Hex's reply so I'm glad you did but the reason I asked
is only because my version of VB.Net generated a warning/error. I didn't
try to run it, I just defined the variables and the warning disappeared.
Curious.

Bit shifting is standard C code, not a problem.
 
Back
Top