Unloading a button at runtime

L

Lou

I need to unload a button that was created during runtime but keep getting
an error that
"Option Strict disallows operands of Type object for operator"=". Use the is
operator.

Here is my code
======================================

'Remove all old buttons

For i = 0 To Me.Controls.Count - 1

If TypeOf (Me.Controls(i)) Is Button Then

If Me.Controls(i).Tag = "MyButton" Then

End If



End If

Next i

==============================

Thanks

-Lou
 
A

Armin Zingler

Lou said:
I need to unload a button that was created during runtime but keep
getting an error that
"Option Strict disallows operands of Type object for operator"=".
Use the is operator.

Here is my code
======================================

'Remove all old buttons

For i = 0 To Me.Controls.Count - 1

If TypeOf (Me.Controls(i)) Is Button Then

If Me.Controls(i).Tag = "MyButton" Then

End If



End If

Next i

==============================

The type of the Tag property is System.Object. You can not use the "="
operator to compare any object with a String. If you know that all Tag
properties reference a String, you can use:

If Me.Controls(i).Tag.ToString = "MyButton" Then


Armin
 
C

Cor Ligthert [MVP]

But you see how nice I have showed your method to everybody, sometimes
telling it is the method Armin Zingler.

:)

Cor
 
L

Lou

This does not work:

Dim i As Integer

Dim ctrl As Button



For i = 0 To PanelMiddle.Controls.Count

If TypeOf (PanelMiddle.Controls(i)) Is Button Then

ctrl = CType(PanelMiddle.Controls(i), Button)

If ctrl.Tag.ToString = btn.Key.ToString Then

PanelMiddle.Controls(i).Text = btn.Key

End If

End If

Next i
 
A

Armin Zingler

Lou said:
This does not work:

"not work" means what? Maybe a NullReferenceException?
Dim i As Integer

Dim ctrl As Button



For i = 0 To PanelMiddle.Controls.Count

For i = 0 To PanelMiddle.Controls.Count - 1

If TypeOf (PanelMiddle.Controls(i)) Is Button Then

ctrl = CType(PanelMiddle.Controls(i), Button)

If ctrl.Tag.ToString = btn.Key.ToString Then

PanelMiddle.Controls(i).Text = btn.Key

End If

End If

Next i

Did you consider "If you know that all Tag properties reference a String".
Is this the case?


Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top