James Carington
James Carington

Reputation: 11

Adding Methods To Windows Form?

I have 2 windows Forms, a parent and a child. The parent is the main form. The child is a dialog where the user can edit their details.

When a button is clicked on the parent form, it loads the child form. Like so:

private void add_account_Click(object sender, EventArgs e)
{
     this.account_add_edit = new Form2();
     account_add_edit.test();
     account_add_edit.ShowDialog();
}

As you can see I've created a new form, tried to call a function from the new form, and then showed the form. The problem is the method from the form is not being called. I am getting an error on the IDE that says Windows.Forms.Form does not contain a method test.

I've created the method in the child form:

public static string test(string val)
{
    this.username = val;
}

Any ideas as to what I'm doing wrong?

Upvotes: 1

Views: 4631

Answers (2)

Akram Shahda
Akram Shahda

Reputation: 14781

Use:

   Form2.test();

static members are associated directly to the class not to its instances. Than means if you need to access a static member you have to access it using it is container type.

More than that, you can not access normal members from static ones. You can only access staticmembers from their peers.

You cannot do the following inside a static method:

this.Member ...

Upvotes: 0

Menahem
Menahem

Reputation: 4144

your method is defined as static , so its not posiible to call it on an instace. you should eaither not make it static, or call it from as static:

Form2.test();

Upvotes: 2

Related Questions