Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am working on a project looks like Outlook interface, The customer loaded in the left side with user control and the right side of the project is tabControl without tabpage,
By click on the user control with button for the desire customer the tabcontrol should add on it a new tabpage with the customer details.

What I have tried:

<pre> public SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["CustomersApp.Properties.Settings.CustomersConnectionString"].ConnectionString);

// Load data button in Form1 
   private void button1_Click(object sender, EventArgs e)
        {
            SqlCommand cm = new SqlCommand("select TOP(20) *from customers", cn);

            try
            {
                cn.Open();
                SqlDataAdapter da = new SqlDataAdapter(cm);
                DataTable dt = new DataTable();
                da.Fill(dt);
                foreach (DataRow dr in dt.Rows)
                {
                    customer_test1 pt = new customer_test1();
                    pt.UC_fullname = dr["customer_name"].ToString();
                    pt.UC_patientid = dr["customer_id"].ToString();
                   
                    flowLayoutPanel1.Controls.Add(pt);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

// code in user control with a button

Form1 ld;
 private void AddNewTab(string tabName)
        {
            if (InvokeRequired)
            {
                Invoke(new MethodInvoker(delegate { AddNewTab(tabName); }));
            }
            else
            {
                TabPage tp = new TabPage(tabName);
                ld.tabControl1.TabPages.Add(tp);
            }
        }

 private void button1_Click(object sender, EventArgs e)
        {
            AddNewTab("test1");
            
        }

By run the application and press the button of customer there is an error appear "System.NullReferenceException:'Objecte reference not set to an instance of an object'
Posted
Updated 28-Jan-20 15:08pm

1 solution

The UserControl code should know absolutely nothing of the container that holds it nor other controls, other than the controls it contains.

The UserControl should expose an event that the parent can subscribe to that the parent can then decide what to do with the notification and how to do it, like creating a tab page in another control.
 
Share this answer
 
Comments
hanymet 29-Jan-20 8:44am    
I drag and drop the user control ,
I have expose event handler for user control and try to add the tabpage in the tabcontrol result is OK :

// this is my code in user control
public event EventHandler UCButtonClick;

private void button1_Click_1(object sender, EventArgs e)
{
if (UCButtonClick != null)
UCButtonClick(sender, e);
}

// my code at the form
private void userControl11_UCButtonClick(object sender, EventArgs e)
{
TabPage tb = new TabPage("test1");
tabControl1.TabPages.Add(tb);
}

But the problem is from the code of loading customers as you can see, it load every customer in new user control(Not drag and drop user control), So how can i link the click event handler to every user control to open the customer details in new tabpage.
Dave Kreskowiak 29-Jan-20 11:01am    
You complete misread what I said. You don't expose EventHandlers, you expose an Event. If your code needs to tell its parent container to create a new tab, your code raises an event to tell the parent container to do that.

EventHandlers are for executing some code when an event is raised that you subscribe to.

Events are what other code subscribes to in order to receive notifications of something happening or that needs to happen.

Read https://docs.microsoft.com/en-us/dotnet/standard/events/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900