Form1 has a richtextbox1 and UserControl1 has a button1
Form1 loads and adds an instance of UserControl1.
I click the button and the richtextbox prints "hello"
//UserControl1.cs
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var parent = Parent as Form1;
parent.somemethod("hello");
}
}
//Form1.cs
public Form1()
{
InitializeComponent();
}
public void somemethod(string message)
{
richTextBox1.Text = message;
}
private void Form1_Load(object sender, EventArgs e)
{
UserControl1 uc = new UserControl1();
//flowLayoutPanel1.Controls.Add(uc);
Controls.Add(uc);
}
The problem:
I added a flowlayoutpanel to form1 and replaced Controls.Add(uc);
with flowLayoutPanel1.Controls.Add(uc);
Exception raised when i click the button:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
parent was null.
Edit: the flowlayout panel is my parent right? that's the problem?
Edit1: @Eric, if i did what you suggested, it won't work. anyways this is the full code for each file