I have been pulling my hair out for awhile
I have created a form filled with pictureboxs. Each picturebox will contain a _click, _mousedown and _mouseup event. I wish to move these events created for the form into a class1.cs and then have form1.cs inherit these events.
I do not wish to make the picturebox1 public. so something such as Class1 classOne = new Class1(); pictureBox1.Click += new EventHandler(cs.pictureBox1_Click); will not work.
I am unsure how to make form1.cs inherit class1.cs. sounds so easy in my head but im just at a mental blank
I have tired class Form1 : Form, Class1
in Form1.Designer.cs this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click); this.pictureBox1_Click can not be accessed
if you could point me in the right direct it will be a great help! Cheers
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");
}
}
Class1 should inherit from Form, and then Form1 would inherit from Class1:
class Class1 : Form
{
//...
}
public partial class Form1 : Class1
{
//...
}
Make sure that all partial declarations inherit from Class1 and not from Form. (This would likely add an extra step for any auto-generated code, which is the classic reason for partial classes. You might need to write your own automation for that.)
What's not clear to me is what you're trying to achieve by having an intermediate type between Form and Form1. Is Class1 supposed to be a generic "base form" that multiple forms will inherit? Or are you trying to do something else? But if you want Form1 to be able to access members of Class1 without exposing them as public members, you can make them protected instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With