0

Pass initialisation information to a form

To pass information to a form can be a tricky thing to do – especially where (as I often do) you want to open the secondary form as a dialog

with the ShowDialog() method as once the form is called execution of code on the main form is halted until the secondary form has been closed which would stop you from using:

Form2.showdialog()
Form2.Variable = "Something"
Form2.function()

To my mind the best way to do this is to use a property in the same way that you would to declare a class but within the form. Whilst setting the property you have the opportunity to enter code to the get and set methods. If whilst setting the value of the property you put in a bit of code to test whether a form is visible then you can open a form with a specific property being acted upon within one line as below:

Form2.Passed = "woah it actually worked!"

In this instance form 2 only has a single textbox which we are going to use to show the initialised value, as I’ve said before we want the form to be shown in one line of code and for the opening to be taken care of on form2. So here’s what’s going on in form 2:

VBC#
Private pPassed As String
Public Property Passed As String
    Get
        Return pPassed
    End Get
    Set(value As String)
        pPassed = value
        If Me.Visible = False Then
            Me.ShowDialog()
        End If
    End Set
End Property
 
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    TextBox1.Text = pPassed
End Sub
private string pPassed;
public string Passed
{
    get { return pPassed; }
    Set(string value)
    {
        pPassed = value;
        If (Visible == false)
        {
            Me.ShowDialog();
        }
    }
}
 
private void Form2_Load(object sender, EventArgs e)
{
    TextBox1.Text = pPassed;
}

And that’s really all you need to be able to pass values to a form on load!. The advantages here are that whilst there are other methods of passing information between forms this is completely clean in that it does not rely on publicly declared variables being set and referenced before and after form load – this has cleaned up some o my projects no end and made them far easier to work with.

Personally to keep things tidy I use the following code for each form, feel free to use the below yourself:

VBC#
Private pInitialisation As String
Public Property Initialisation As String
    Get
        Return pInitialisation
    End Get
    Set(value As String)
        pInitialisation = value
        If Me.Visible = False Then
            Me.ShowDialog()
        End If
    End Set
End Property
private string pInitialisation;
public string Initialisation
{
    get { return pInitialisation; }
    set
    {
        pInitialisation = value;
        if (Visible == false)
	{
	    ShowDialog();
	}
    }
}

JBaker

Joi is an IT Development Analyst working in London and a consummate technophile. Whilst he isn't coding he can be found reacquainting himself with his guitars, spinning poi, looking for climbing walls or chasing the latest shiny thing to cross his field of vision.

Leave a Reply

Your email address will not be published. Required fields are marked *