Update Control on User Control from Main Form

KingCundy

Background

I have a main form that has a tableLayoutPanel. Within that I have three panel, a header, footer and left side bar. In the remaining space I add and remove usercontrols this one in the example is called ctrlmanagepreset.

Within these usercontrols I have controls. Namely a Listsbox s, that i'm trying to add items too.

I am getting the items from an xml file that does contain items and reading them in to an object list. The name of each object is then added to the listbox.

All of the Controls are accessable as I've made them public. I think it might be due to the way i create and add them?

Question

Why aren't the Listboxes updating, showing the added items?

Code

Button click event that creates usercontrol

 public void btnManage_Click(object sender, EventArgs e)
        {
            tableLayoutPanel.Controls.Add(new ctrlManagePresets () { Dock = DockStyle.Left }, 1, 1);
            PopulateCreateJob();

        }

Method that Populates Listbox

 public void PopulateCreateJob()
        {

            ctrlManagePresets ctrlmanagepresets = new ctrlManagePresets();

            //read in contents of xml file
            if (File.Exists(JoblistXmlFilepath))
            {

                XmlSerializer deserializer = new XmlSerializer(typeof (List<Favourite>));
                TextReader reader = new StreamReader(JoblistXmlFilepath);

                //create list of old fave objects

                var xmlList = (List<Favourite>) deserializer.Deserialize(reader);

                reader.Close();

                if (xmlList.Count > 0)
                {
                    foreach (Favourite t in xmlList)
                    {
                        //add favourite objects to combobox
                        try
                        {
                            ctrlmanagepresets.lbCreateJob.Items.Add(t.Name);
                        }
                        catch
                        {
                            MessageBox.Show(@"There is an object with no name in the XML.", @"Message",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                        }
                    }
                }
                ctrlmanagepresets.lbCreateJob.Refresh();
            }
            else
            {
               ctrlmanagepresets.lbCreateJob.Items.Add(@"Settings File Not Found");
               ctrlmanagepresets.lbCreateJob.Enabled = false;
               ctrlmanagepresets.lbCreateJob.BackColor = Color.DarkRed;
            }
    }
Steve

You are not adding the items to the instance of the control that you add to your tableLayoutPanel.

Just make your PopulateCreateJob return the instance that is built and intialized with the xml data

public void btnManage_Click(object sender, EventArgs e)
{
    ctrlManagePresets ctrl = PopulateCreateJob();
    ctrl.Dock = DockStyle.Left; 
    tableLayoutPanel.Controls.Add(ctrl, 1, 1);
}


public ctrlManagePresets PopulateCreateJob()
{
    ctrlManagePresets ctrlmanagepresets = new ctrlManagePresets();
    // current code that initialize the instance of your control
    ....

    // return the control instance initialized to the caller
    return ctrlmanagepresets;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unable to update main form control from subclass

From Dev

Dynamically created User Control detecting button click from main Form

From Dev

Control a subform from main form

From Dev

Using a control from the main form on subforms

From Dev

How do I read a property from my main form in a user control

From Dev

issue with Calling user control public functions from main form in ASP.net C#

From Dev

How to detect events from an added template user control inside main form

From Dev

How to Get Data From User Control on Form

From Dev

User Control calling a method from the parent form

From Dev

call an event in user control using a button in the main form c#

From Dev

NullReferenceException when subscribing to an event in the user control on the main form

From Dev

Resize User Control in Form

From Dev

How to Send Instance from Main Windows to User Control

From Dev

Handling a button click inside a user control from the main window

From Dev

Raise a event on the User control from the Main Window in WPF

From Dev

Passing Variables from User Control to User Control

From Dev

How to access user control page dropdownlist control id from main page's javascript function?

From Dev

Request.Form getting values from another user control

From Dev

Update control on main form via Background Worker with method in another class VB.Net

From Dev

Update control on main form via Background Worker with method in another class VB.Net

From Dev

How best to call a public sub routine declared in a form used as the source object a subform control from the main form?

From Dev

How best to call a public sub routine declared in a form used as the source object a subform control from the main form?

From Dev

Change main window implementation to user control

From Dev

accessing a user control from masterpage

From Dev

getting a value from user control

From Dev

Angular 2 update ngFor for one form control

From Dev

Access user control name in MainWindow from within user control

From Dev

Access user control name in MainWindow from within user control

From Dev

Change User Control from another User Control Winforms C#

Related Related

  1. 1

    Unable to update main form control from subclass

  2. 2

    Dynamically created User Control detecting button click from main Form

  3. 3

    Control a subform from main form

  4. 4

    Using a control from the main form on subforms

  5. 5

    How do I read a property from my main form in a user control

  6. 6

    issue with Calling user control public functions from main form in ASP.net C#

  7. 7

    How to detect events from an added template user control inside main form

  8. 8

    How to Get Data From User Control on Form

  9. 9

    User Control calling a method from the parent form

  10. 10

    call an event in user control using a button in the main form c#

  11. 11

    NullReferenceException when subscribing to an event in the user control on the main form

  12. 12

    Resize User Control in Form

  13. 13

    How to Send Instance from Main Windows to User Control

  14. 14

    Handling a button click inside a user control from the main window

  15. 15

    Raise a event on the User control from the Main Window in WPF

  16. 16

    Passing Variables from User Control to User Control

  17. 17

    How to access user control page dropdownlist control id from main page's javascript function?

  18. 18

    Request.Form getting values from another user control

  19. 19

    Update control on main form via Background Worker with method in another class VB.Net

  20. 20

    Update control on main form via Background Worker with method in another class VB.Net

  21. 21

    How best to call a public sub routine declared in a form used as the source object a subform control from the main form?

  22. 22

    How best to call a public sub routine declared in a form used as the source object a subform control from the main form?

  23. 23

    Change main window implementation to user control

  24. 24

    accessing a user control from masterpage

  25. 25

    getting a value from user control

  26. 26

    Angular 2 update ngFor for one form control

  27. 27

    Access user control name in MainWindow from within user control

  28. 28

    Access user control name in MainWindow from within user control

  29. 29

    Change User Control from another User Control Winforms C#

HotTag

Archive