C # Inherit class inside form

I stretch my hair for a while

I created a form filled with image files. Each frame will contain the _click, _mousedown and _mouseup event. I want to move these events created for the form to class1.cs, and then inherit these form1.cs events.

I do not want to make Picturebox1 publicly available. so something like Class1 classOne = new Class1 (); pictureBox1.Click + = new EventHandler (cs.pictureBox1_Click); will not work.

I am not sure how to make form1.cs inherit from class1.cs. sounds so easy in my head, but im just in the mental blank

I'm tired of class Form1: Form, Class1

in Form1.Designer.cs this.pictureBox1.Click + = new System.EventHandler (this.pictureBox1_Click); this.pictureBox1_Click is not available

If you could point me in the right direction, that would be a big help! Greetings

Form1.cs
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
}

Class1.cs 
class Class1
{

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("pb1 click");
    }
 }
+3
source share
7 answers

Inheritance defines the is-a relationship, so if Form1 is not class 1, then you do not approach it in the right way. You really have several options, depending on what you are trying to design:

  • Pass the form to the class instance via a public method and let the class attach an event handler (this seems to make more sense in these cases if there are several forms that share the behavior).

  • Keep the usual behavior in the form itself (as a special type of form, PictureClickingForm), and then let other forms inherit from it. Methods can now be protected.

. , .

+3

Class1 Form, Form1 Class1:

class Class1 : Form
{
    //...
}

public partial class Form1 : Class1
{
    //...
}

, partial Class1, Form. (, , partial. , .)

, , - , , Form Form1. Class1 " ", ? - ? , Form1 Class1, public, protected.

+2

, Class1.cs,

public partial class Form1
{
    private void pictureBox1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("pb1 click");
    }
 }
+1

Picture Box, Class1 , .

, . .

0

Form1 Class1? - :

//Form1.cs
public partial class Form1 : Form
{
    Class1 class1;

    public Form1()
    {
        InitializeComponent();
        class1 = new Class1();
        this.pictureBox1.Click += new EventHandler(class1.pictureBox1_Click); 
    }
}

//Class1.cs 
class Class1
{

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("pb1 click");
    }
}

, , GUI. , , .

0

Form1 , Form2 Form1. ( , - Class1.cs) Form1.designer.cs Form1, . , ( !), !).

, , .

. Form1 abstract virtual Form1 , Form2.

?

0

You can always use interfaces. The fact is that you can inherit only one class, but many interfaces ... Therefore, I assume that you will need such interfaces - pass throught methods or something else. Far from the interface, you can always put your class as a parameter to the constructor of the form of the second class (using the 'this' keyword) so that you cannot pass values, methods from previous forms pass to another (for example, events).

0
source

All Articles