Passing an Argument to a Sub 
Author Message
 Passing an Argument to a Sub

I have a Form with a number of Panels. Each panel contains a large number of
labels and pictureboxes.

   When the user clicks on any label, I want the text forecolor of all the
labels in that particular panel to change to another color and all the
picture boxes in that panel to have it's border style set to none.

   Instead of writing a sub for each separate panel, I thought I'd create a
Sub and call it from any label click event in any panel. All it would need,
I thought, would be the name of the calling panel and a simple For Each/Next
loop.    But it throws an error .

  Here's what I used:

Code:

Private Sub ChangePanelTextColor(ByRef PN As Panel)
        Try
            Dim l As Label
            For Each l In PN.Controls
                l.ForeColor = System.Drawing.Color.BurlyWood
            Next

            Dim p As PictureBox
            For Each p In PN.Controls
                p.BorderStyle = BorderStyle.None
            Next
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

    End Sub

  and I called it from a Label_Click event in the usual way

      Code:
ChangePanelTextColor(<Name of Panel containing this Label>)

   Have I missed something really obvious and basic here ?  Or do I have to
go about it in a completely different way?

JDP



Sat, 06 Aug 2005 16:53:35 GMT  
 Passing an Argument to a Sub
You cannot simple do this,
            Dim l As Label
            For Each l In PN.Controls
                l.ForeColor = System.Drawing.Color.BurlyWood
            Next

or
            Dim p As PictureBox
            For Each p In PN.Controls
                p.BorderStyle = BorderStyle.None
            Next

as the For Each control In PN.Controls will retrieve all the controls in
that panel. You need to check what type it is before using it.

Your code should be rewritten like this,

Dim control As Control
For Each control In PN.Controls
    Select Case control.GetType.ToString
        Case "System.Windows.Forms.PictureBox"
            Dim p As PictureBox
            p = DirectCast(control, PictureBox)
            p.BorderStyle = BorderStyle.None
        Case "System.Windows.Forms.Label"
            Dim l As Label
            l = DirectCast(control, Label)
            l.ForeColor = System.Drawing.Color.BurlyWood
    End Select
Next


Quote:
> I have a Form with a number of Panels. Each panel contains a large number
of
> labels and pictureboxes.

>    When the user clicks on any label, I want the text forecolor of all the
> labels in that particular panel to change to another color and all the
> picture boxes in that panel to have it's border style set to none.

>    Instead of writing a sub for each separate panel, I thought I'd create
a
> Sub and call it from any label click event in any panel. All it would
need,
> I thought, would be the name of the calling panel and a simple For
Each/Next
> loop.    But it throws an error .

>   Here's what I used:

> Code:

> Private Sub ChangePanelTextColor(ByRef PN As Panel)
>         Try
>             Dim l As Label
>             For Each l In PN.Controls
>                 l.ForeColor = System.Drawing.Color.BurlyWood
>             Next

>             Dim p As PictureBox
>             For Each p In PN.Controls
>                 p.BorderStyle = BorderStyle.None
>             Next
>         Catch ex As Exception
>             MessageBox.Show(ex.ToString)
>         End Try

>     End Sub

>   and I called it from a Label_Click event in the usual way

>       Code:
> ChangePanelTextColor(<Name of Panel containing this Label>)

>    Have I missed something really obvious and basic here ?  Or do I have
to
> go about it in a completely different way?

> JDP



Sat, 06 Aug 2005 17:15:42 GMT  
 Passing an Argument to a Sub
Hi, What's the error you're getting?

--
==============================================
Happy To Help,
Tom Spink

http://dotnetx.betasafe.com >> VB, VB.NET, C#, C++ Code

One day

Quote:
> I have a Form with a number of Panels. Each panel contains a large number
of
> labels and pictureboxes.

>    When the user clicks on any label, I want the text forecolor of all the
> labels in that particular panel to change to another color and all the
> picture boxes in that panel to have it's border style set to none.

>    Instead of writing a sub for each separate panel, I thought I'd create
a
> Sub and call it from any label click event in any panel. All it would
need,
> I thought, would be the name of the calling panel and a simple For
Each/Next
> loop.    But it throws an error .

>   Here's what I used:

> Code:

> Private Sub ChangePanelTextColor(ByRef PN As Panel)
>         Try
>             Dim l As Label
>             For Each l In PN.Controls
>                 l.ForeColor = System.Drawing.Color.BurlyWood
>             Next

>             Dim p As PictureBox
>             For Each p In PN.Controls
>                 p.BorderStyle = BorderStyle.None
>             Next
>         Catch ex As Exception
>             MessageBox.Show(ex.ToString)
>         End Try

>     End Sub

>   and I called it from a Label_Click event in the usual way

>       Code:
> ChangePanelTextColor(<Name of Panel containing this Label>)

>    Have I missed something really obvious and basic here ?  Or do I have
to
> go about it in a completely different way?

> JDP



Sat, 06 Aug 2005 18:23:56 GMT  
 Passing an Argument to a Sub

Thanks for both the replies.  

Tom,  The error message was a System.InvalidCastException Specified Cast
Not Valid    -    which the code from

Programatix  fixes nicely.   Thank you.

I've looked up the DirectCast keyword now in the Help and have a
(slightly) better idea of what I'm doing.
My VB6 non-OOP background is making this learning curve a lot slippier
than it might otherwise be  :-(

Cheers

JDP

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Sun, 07 Aug 2005 01:32:01 GMT  
 Passing an Argument to a Sub
You should only use DirectCast() if you're sure that the object is really of
the type specified. If not, it is always better to use CType(). Of course
DirectCast is faster.


Quote:

> Thanks for both the replies.

> Tom,  The error message was a System.InvalidCastException Specified Cast
> Not Valid    -    which the code from

> Programatix  fixes nicely.   Thank you.

> I've looked up the DirectCast keyword now in the Help and have a
> (slightly) better idea of what I'm doing.
> My VB6 non-OOP background is making this learning curve a lot slippier
> than it might otherwise be  :-(

> Cheers

> JDP

> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!



Sun, 07 Aug 2005 14:28:50 GMT  
 Passing an Argument to a Sub
We are sure, because there was a select case ensuring the correct object
type was selected.

--
==============================================
Happy To Help,
Tom Spink

http://dotnetx.betasafe.com >> VB, VB.NET, C#, C++ Code

Please respond to the newsgroups, so all can benefit.

One day

Quote:
> You should only use DirectCast() if you're sure that the object is really
of
> the type specified. If not, it is always better to use CType(). Of course
> DirectCast is faster.



> > Thanks for both the replies.

> > Tom,  The error message was a System.InvalidCastException Specified Cast
> > Not Valid    -    which the code from

> > Programatix  fixes nicely.   Thank you.

> > I've looked up the DirectCast keyword now in the Help and have a
> > (slightly) better idea of what I'm doing.
> > My VB6 non-OOP background is making this learning curve a lot slippier
> > than it might otherwise be  :-(

> > Cheers

> > JDP

> > *** Sent via Developersdex http://www.developersdex.com ***
> > Don't just participate in USENET...get rewarded for it!



Sun, 07 Aug 2005 17:10:50 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. pass an array as function/sub argument

2. Passing recordset as an argument in a sub?

3. Passing ListBox as argument to sub

4. passing arguments to sub - script not working as expected

5. Type mismatch passing argument to sub

6. pass an array as function/sub argument

7. Callback to sub/func by argument passing?

8. Callback to sub/func by argument passing?

9. Arguments : Passing Arguments containing <">

10. Passing a sub name to another sub

11. Passing as string variable from sub to sub

12. Arguments pass short 8.3 filename, how pass LFN?

 

 
Powered by phpBB® Forum Software