Ivan
Ivan

Reputation: 5238

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())

Upvotes: 0

Views: 1121

Answers (2)

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33391

You should call parameterless (default) ctor as well to initialize your controls.

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

Upvotes: 3

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();
    }

Upvotes: 3

Related Questions