Public Function Help RRS feed

  • Question

  • Hello, is there a way to make a public function (or whatever else it would be. Public function might be wrong.) that will have code in it? I'm not sure how to explain it. So lets say I have a public function called CrazyColors, I make the function and it has code in it that will change all my labels colors. So than I can put CrazyColors anywhere I want to use the code without having to type it all gain. Can someone explain how to do that?
    Sunday, April 23, 2017 9:23 PM

All replies

  • Hi

    Here is some code that may be helpful.

    This example uses a Public Sub to change the BackColor of Controls of required type in a container Control.

    In this case, I have a GroupBox with a bunch of Labels in it, and a button to invoke the color change.

    A Module called 'Change' has this code:

    Option Strict On
    Option Explicit On
    Module Change
        Public Sub BC(cont As Control, t As Type, col As Color)
            For Each c As Control In cont.Controls
                If c.GetType() Is t Then
                    c.BackColor = col
                End If
        End Sub
    End Module

    In the main Form1 Button1 Click event, I have

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

      ' call the Sub with the Container, the Type for the
       ' control to change the BC of and the required color.

    BC(GroupBox1, GetType(Label), Color.Red) End Sub

    Regards Les, Livingston, Scotland

    • Edited by leshay Friday, May 5, 2017 11:35 PM
    Sunday, April 23, 2017 10:18 PM
  • So than I can put CrazyColors anywhere I want to use the code without having to type it all gain. Can someone explain how to do that?

    This might be what you mean:

    A class library is a separate DLL that can be added to any project.

    If you only want the method available within a single project, you can use a module:

    If you want to change the colours of all your labels, then your sub would take a form as one of the arguments and the code would iterate over all the controls in the form, selecting the labels and changing their colour.

    Sunday, April 23, 2017 10:27 PM
  • Hello,

    You could create a class that implements IExtenderProvider. The following example after building the class and placing it on a form, all current labels fore color will be red (as I set it in CanExtend function. 

    Note ProvideProperty defines the property and each property has a Get and Set with the property name after Get and Set for this to work.

    To set one label a different color

    CrazyLabelExtender1.SetChangeForeGroundColor(Label3, Color.Green)

    To change all, in this case I pass Me for the form and Blue for the color, all labels on that form including those on a panel or group box will change to blue.

    CrazyLabelExtender1.ChangeAll(Me, Color.Blue)

    The class

    Imports System.ComponentModel
    <ProvideProperty("ChangeForeGroundColor", GetType(Control))>
    Public Class CrazyLabelExtender
        Inherits Component
        Implements IExtenderProvider
        Public Function CanExtend(ByVal extendee As Object) As Boolean _
            Implements IExtenderProvider.CanExtend
            If TypeOf extendee Is Label Then
                ' default color
                CType(extendee, Label).ForeColor = Color.Red
                Return True
                Return False
            End If
        End Function
        ''' <summary>
        ''' Get foregorund color for current control
        ''' </summary>
        ''' <param name="myControl"></param>
        ''' <returns></returns>
        Public Function GetChangeForeGroundColor(ByVal myControl As Control) As Color
            Return CType(myControl, Label).ForeColor
        End Function
        ''' <summary>
        ''' Set foreground color for current control
        ''' </summary>
        ''' <param name="myControl"></param>
        ''' <param name="value"></param>
        Public Sub SetChangeForeGroundColor(ByVal myControl As Control, ByVal value As Color)
            CType(myControl, Label).ForeColor = value
        End Sub
        ''' <summary>
        ''' recurse sender (e.g. form or a panel etc.) and set ForeGrounColor to ForColor
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="ForColor"></param>
        Public Sub ChangeAll(sender As Control, ForColor As Color)
            Dim ctrl As Control = sender.GetNextControl(sender, True)
            Do Until ctrl Is Nothing
                If TypeOf ctrl Is Label Then
                    CType(ctrl, Label).ForeColor = ForColor
                End If
                ctrl = sender.GetNextControl(ctrl, True)
        End Sub
    End Class

    A design time

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, April 24, 2017 12:53 AM
  • You describe it in a way that you get all kind of replies, but it is possible non of those answers your question. 

    What you describe can be:

    1. A function to change things on a whatever that is on screen
    2. A real public function which means that the function is not hidden from outside your class (program part)
    3. A static function which means that it can always be used without instancing
    4. A complete function which can be referenced and than do things which are general done (in the way you describe impossible because it is for all UI techniques different)

    Be aware in some of those you don't write your scoop, (Then kind of technique where you want to use it)

    Therefore, please make it more clean and start with telling if it is related to VB programming or just a general kind of question?


    Monday, April 24, 2017 9:59 AM