Qing Ryder

Qing Ryder

  • NA
  • 202
  • 17.3k

c# Accessing Methods and objects in a User control

Dec 15 2019 2:52 PM
I have two Forms called Dashboard and Form1, on Dashboard, I have 3 User Controls, (AgricInput,worksheet, and hyper_parameters).
 
I wanted to access some objects and methods of AgricInput that worksheet depends on, and i did that using the following syntax
 
AgricInput agro = (AgricInput)this.Parent.Controls.Find("AgricInput1", true)[0];. Then I was able to call some methods like agro.bunifuCustomDataGrid1.Rows.Count. However, now I am faced with a challenge, which is accessing methods and objects of worksheet that Form1 (which is another form on my application) depends on. I tried replicating what I used before which is Dashboard f1 = new Dashboard(); worksheet work = (worksheet)f1.Parent.Controls.Find("worksheet1", true)[0]; but each time the application runs, am faced with System.NullReferenceException: 'Object reference not set to an instance of an object.'. I don't know what am doing wrong here.
 
I also tried using worksheet work = (worksheet)this.Parent.Controls.Find("worksheet1", true)[0]; and worksheet work = this.Parent.Controls.Find("worksheet1", true).FirstOrDefault as worksheet;. Still facing same error.

Answers (1)