Solved

C# accessing GUI object outside of the mainform class

Posted on 2011-02-23
4
792 Views
Last Modified: 2012-05-11
Experts:

I have a MainForm class and a UI class.  The UI class is meant to handle things like populating tables and handling actions of the UI.  The item I'm trying to add objects to is a list and is defined in the MainForm class as "CompsList".

I tried to access that list with the code:

this.CompsList.Items.Add(...);

I get an error on the THIS statement, and if I remove that, I get a message stating CompsList doesn't exist.

How can I access the object to add or remove items when I want to?
0
Comment
Question by:piattnd
  • 2
  • 2
4 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34962582
CompsList in MainForm would need to Public.  Then, you'd have to pass a REFERENCE to MainForm into your UI Class so that it can access it.
0
 
LVL 12

Author Comment

by:piattnd
ID: 34962700
public System.Windows.Forms.ListView CompsList;

That is the code that creates the list view in MainForm.  I tried to then use this line of code to execute the add of the list view item:

CompsList.Items.Add(listViewItem1);

Still getting an error saying CompsList isn't found.  Pardon the ignorance here, but I'm still not sure what I'm missing.  I understand what's going on, just not sure why or how you're meaning to get around it.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 34962760
I assume MainForm creates an instance of the UI Class?

One way to do it:
public partial class MainForm : Form
    {

        public UIClass uc = new UIClass();
        public System.Windows.Forms.ListView CompsList;

        public MainForm()
        {
            InitializeComponent();
            uc.MyMainForm = this; // <--- pass reference to our MainForm into UIClass
        }

    }

    public class UIClass
    {

        public MainForm MyMainForm;

        private void Foo()
        {
            MyMainForm.CompsList.Items.Add(...)
        }
    }

Open in new window

0
 
LVL 12

Author Closing Comment

by:piattnd
ID: 34963994
Worked perfectly AND I think it actually makes sense!  Great thorough answer, just a lack of brain power on my part!

Thanks.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question