Ivan Ivan - 3 months ago 12
C# Question

C# winforms return empty form when i open as dialog

I have problem when i try to show dialog. When i pass

this
argument as parameter form is empty.

What i do:

In my
GroupsForm.cs
form i create this method:

private void addGrupuBtn_Click(object sender, EventArgs e)
{
using (var add = new GroupAdd(this))
{
add.ShowDialog();
}
}


In
GroupAdd.cs
form i try to inject
GroupsForm.cs
via constructor

public partial class GroupAdd : Form
{
private GrupeForm grupeForm;

public GroupAdd()
{
InitializeComponent();
}

public GroupAdd(GrupeForm groups) {
grupeForm = groups;
}
}


When i call default constructor without
this
all controls inside form is rednered.

This work ok.

using (var add = new GroupAdd())

Answer

You forgot to call InitializeComponent() in your constructor, that's why it doesn't work.

Modify your code to this:

    public GroupAdd(GrupeForm groups) { 
        grupeForm = groups;
        InitializeComponent();
    }