VB.NET - How to substitute frame name with the string variable?

Posted on 2008-11-11
Last Modified: 2012-06-21

I pass two variables to the procedure. One is output to be printed in the form's textbox and another is a form's Name. How do I actually substitute Form1 with the frmName to make sure that the text is printed in the textbox of the form that I need?

I can implement it using Select Case or if.. then basing on frmName value, but I wonder how to do that avoiding it.

Public Sub Result(ByVal output As String, ByVal frmName As String)

        With Form1.txtServerStatus

            .Text &= vbCrLf & output

        End With

    End Sub

Open in new window

Question by:Zaurb
    LVL 2

    Expert Comment

    Dont pass the form name as a string.  Pass the form as an object of the type of form it is.  
    Public Sub Result(ByVal output As String, ByVal frmIN As MYFORMTYPE)
    Of course if you have different Form Classes you are trying to pass to this procedure you may need to subclass all of the forms to a single class so that they include a text box called txtServerStatus.

    LVL 1

    Author Comment


    I have 5 forms with the similar content and similar design, but not exactly the same functionality.
    Depending on which specific form is used for working, I pass a form name variable to a class, that does the rest of the processing. txtServerStatus is available on all forms and prints the information on steps carried out in the class.
    Since there're several users working contemporaneously they will see different information in the txtServerStatus, depending on operations they make.
    For this reason, I pass the form name to a class, so that after procedures execute, they will print the result to a specific form.

    As for this:
    Public Sub Result(ByVal output As String, ByVal frmIN As MYFORMTYPE)

    How should I implement it in my case?

    LVL 2

    Expert Comment

    You have 5 forms that are very similar with slight variations in functionality.  You would want to make a base form that contains the common objects and functionallity.  Then make the 5 specific forms by INHERITING from the base form.  This will allow the 5 specific forms to have exactly the same COMMON objects and functions and still give you the ability to modify them for their individualized functionality.
    To make a base form, simply add a new form to your project.  Design it with any common components and call it  frmBASE.   For example, on frmBase put a textbox called txtServerStatus in the appropriate place to be used for the 5 forms you need.
    To create your 5 specific forms, right click on  your project and select ADD INHERITED FORM. Name the new form and pick frmBASE from the selection list.  A new form exactly like frmBase will be added to your design editor.
    Individualize each of the 5 forms for their unique functionallity.  Then you can create functions like the one below which allows a FORM of type frmBASE as a parameter.  Within the function, all members and properties of frmBASE are accessible.
    Create Public Sub Result(ByVal Output as String, ByVal frmIn as frmBASE)
          frmIn.txtServerStatus will be accessible
    From any of the five forms you can call Result(mystring,ME)
    Hope this helps

    LVL 2

    Accepted Solution

    I thought about this problem some more.   You could use a case statement instead of inheritance.  For example
    Public Sub Result(ByVal output As String, ByVal frmName As String)
        Select Case frmName
           Case "FORM1"
                        frm1.txtServerStatus.Text &= vbCrLF & output
          Case "FORM2"

                        frm2.txtServerStatus.Text &= vbCrLF & output
    End Select
    End Sub  
    Of course to do this, frm1 and frm2 would have to be global variable names.   You could declare
         Public frm1 as FORM1 = new FORM1()
    I have done workarounds like this before as opposed to recreating all of the forms as inheritted from a base form.
    Good Luck
    LVL 1

    Author Comment

    This is actually how I do it now. And it seems to be the less complicated way to deal with this problem.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now