banner



How To Connect Two Windows Form In C#

Why practice that?

Sometimes it's useful to accept more than than just i course in a project. For example, if you need more room or you want the user to brand a selection or consummate an action that is separated from the electric current form. Dealing with multiple Windows forms might seem a flake tricky just information technology isn't.

How to create a new form?

Creating a new grade in your project is every bit easy as creating a class!

one. Right-click on your projection.
two. Select Add New Particular.

Create New Item

3. Select in the list: Windows Course.
iv. Give information technology a relevant name.

Add New Form - Ajouter nouveau formulaire

Starting the Form

Once you accept your new class, it will be treated but like a class. Yous will exist able to call it in your parent grade.

We will add a new push in our parent form that we will employ to launch the kid form. Select a button from the toolbox in your parent grade's pattern editor and elevate it on the form itself. So double-click on the button equally we've seen in the past tutorials to create its standard event which is Click.

I strongly suggest that yous rename the push by irresolute its (Name) property and alter its Text holding as well. That should give y'all something like the post-obit image.

Rename Button - Renommer bouton

To launch the kid class, you create an example of this form in the button click method in your parent form. Then telephone call the Show() method of the child form y'all take just created. The following example should analyze this argument:

C#

individual void btnChildFormLauncher_Click(object sender, EventArgs east) {      frmChild ChildForm = new frmChild();       ChildForm.Show(); }        

Visual Bones

Public Grade frmDashboard     Private Sub btnChildFormLauncher_Click(sender As Object, east Equally EventArgs) Handles btnChildFormLauncher.Click         Dim ChildForm = New frmChild()          ChildForm.Testify()     End Sub Cease Grade        

In that location is more to it!

That'south it! Simply it's non over nevertheless, you can add parameters to your child form'due south constructor to exist able to laissez passer information to this form before it's launched. For example, if you want to tell the kid form information about the current edited customer (let's say the grade is used to edit clients).

Permit'southward simplify this case and pass the class championship instead. The lawmaking below shows my second form (please note that the namespace might defer depending on your project name, same this for the form name).

C#

using System.Windows.Forms;  namespace MultipleForms {     public partial class frmChild : Form     {         public frmChild(string FormName)         {             InitializeComponent();                          //Set the grade text to the passed FormName variable             this.Text = FormName;         }     } }        

Visual Basic

Public Class frmChild     Public Sub New(ByVal FormName As String)         Me.Text = FormName     End Sub Terminate Course        

At present if you lot endeavour to build the solution, you will see an error because we're missing a parameter in the constructor in the parent form.

Missing parameter - Paramètre manquant

So, but add the parameter to the constructor and in that location you become! Let'southward test this out by running our lawmaking.

C#

private void btnChildFormLauncher_Click(object sender, EventArgs e) {     frmChild ChildForm = new frmChild("How-do-you-do Anybody!");      ChildForm.Prove(); }        

Visual Bones

Public Class frmDashboard     Private Sub btnChildFormLauncher_Click(sender As Object, e As EventArgs) Handles btnChildFormLauncher.Click         Dim ChildForm = New frmChild("Hi Anybody!")          ChildForm.Show()     Finish Sub End Class        

Lastly, you tin click on the button and it will launch the second grade with its new title just like the image below.

Second Form - Deuxième formulaire

In that location is more!

Launching a Windows grade from another Windows course is pretty easy and tin can be really useful to separate data and make the user experience easier to understand. There is in fact more to the field of study like how to create closing events for the child grade but we volition discuss most this topic in some other tutorial. If yous have any questions, feel complimentary to ask them in the comments!

Found an error in the code? Please ship me a message so I tin fix it as before long as possible!

YouTube

C#

Visual Bones

Source: https://unaura.com/handle-multiple-windows-forms-visual-basic-c/

Posted by: shipleywatiod.blogspot.com

0 Response to "How To Connect Two Windows Form In C#"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel